Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > strange problem

Reply
Thread Tools

strange problem

 
 
junw2000@gmail.com
Guest
Posts: n/a
 
      05-28-2006
The following code can be compiled. But When I run it, it causes
"Segmentation fault".

#include <iostream>

int main(){
char **c1;

*c1 = "HOW"; // LINE1

std::cout <<"1"<<std::endl;

++(*c1);
*c1 = "ARE";

std::cout <<"2"<<std::endl;
++(*c1);
*c1 = "YOU";
std::cout <<"3"<<std::endl;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;

}

Why LINE1 causes "Segmentation fault"?

What is the correct way to write the above code?

Thanks a lot.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-28-2006
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> The following code can be compiled. But When I run it, it causes
> "Segmentation fault".
>
> #include <iostream>
>
> int main(){
> char **c1;
>
> *c1 = "HOW"; // LINE1
>
> std::cout <<"1"<<std::endl;
>
> ++(*c1);
> *c1 = "ARE";
>
> std::cout <<"2"<<std::endl;
> ++(*c1);
> *c1 = "YOU";
> std::cout <<"3"<<std::endl;
> std::cout <<*c1<<std::endl;
> (*c1)++;
> std::cout <<*c1<<std::endl;
> (*c1)++;
> std::cout <<*c1<<std::endl;
>
> }
>
> Why LINE1 causes "Segmentation fault"?


Dereferencing an uninitialized pointer - Undefined Behavior.


> What is the correct way to write the above code?


Rather, what's a better way to use your compiler? Up the warning
levels. Ensure that you get a clean compile (no warnings), always.

To output the text "HOW ARE YOU", do

std::cout << "HOW ARE YOU" << std::endl;

Just avoid pointers, they're dangerous.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Luke Meyers
Guest
Posts: n/a
 
      05-28-2006
(E-Mail Removed) wrote:
> The following code can be compiled. But When I run it, it causes
> "Segmentation fault".


Yes. The english name for that is "invalid memory access."

> #include <iostream>
>
> int main(){
> char **c1;


This is a variable which holds a memory address. If it held a valid
address (it doesn't, unless by chance), the contents of the memory at
said address would be another memory address. If *that* address were
valid, it would be the location of a variable of type char.

A little confusing? Yes. Easy to mix up and make a mistake, resulting
in a seg fault? Yup.

> *c1 = "HOW"; // LINE1


Right, so here you're dereferencing a pointer which does not hold a
valid memory address. Hence, seg fault. If *c1 held a valid pointer
(to a variable of type char*), the result of this operation would be to
make *c1 point to the area in static memory containing the string
literal "HOW."

> What is the correct way to write the above code?


Use std::string. It exists to prevent you from feeling the pain you
now feel.

Luke

 
Reply With Quote
 
junw2000@gmail.com
Guest
Posts: n/a
 
      05-28-2006
Thanks.
Certainly, std::string is safer. I am trying to learn
pointer-to-pointer to char.
If using C language, how to initialize c1 to three strings---"HOW",
"ARE", "YOU"?

Thanks again.

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      05-28-2006
junw2000 wrote:

> Certainly, std::string is safer.


This newsgroup always instructs students to start with the Standard library
stuff.

> I am trying to learn
> pointer-to-pointer to char.
> If using C language, how to initialize c1 to three strings---"HOW",
> "ARE", "YOU"?


Start with three pointers to characters:

char const * strings[] = { "HOW", "ARE", "YOU" };

And point to the first one:

char const * * p = strings;

Now p[1] points to "ARE", and p[1][1] points to 'R'.

You might be able to Google for "aggregate initializers" from here.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      05-28-2006
(E-Mail Removed) wrote:
> Certainly, std::string is safer. I am trying to learn
> pointer-to-pointer to char.


Sure. The best way, in my opinion, to learn to use char** correctly is
to first learn about using, for example, int**. Integers are easier to
deal with because you don't have the added complications of string
literals, string lengths, null termination, etc. It may sound like I'm
being perversely intransigent, but the fact is that the fundamental
things you're failing to understand, in the code you posted, are issues
which pertain to all pointer-to-pointer types. The reason you're
failing to understand them has a lot to do with the fact that you're
trying to jump straight into the complexities of char** without
understanding those fundamentals.

> If using C language, how to initialize c1 to three strings---"HOW",
> "ARE", "YOU"?


This isn't a C newsgroup. If you want help with C, try comp.lang.c
(actually, I see that you already did so).

Luke

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      05-28-2006
Luke Meyers wrote:

>> If using C language, how to initialize c1 to three strings---"HOW",
>> "ARE", "YOU"?

>
> This isn't a C newsgroup. If you want help with C, try comp.lang.c
> (actually, I see that you already did so).


Mr. Manners reminds the Gentle Poster that some neophytes have not yet
learned to write, "if using C-style C++..."

Note the transition was from std::string; the poster was trying to
distinguish from the C++ Standard Library.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
LuB
Guest
Posts: n/a
 
      05-28-2006

(E-Mail Removed) wrote:
> The following code can be compiled. But When I run it, it causes
> "Segmentation fault".
>
> #include <iostream>
>
> int main(){


char* d1 = "";
char** c1 = &d1;

>
> *c1 = "HOW"; // LINE1
>
> std::cout <<"1"<<std::endl;
>
> ++(*c1);
> *c1 = "ARE";
>
> std::cout <<"2"<<std::endl;
> ++(*c1);
> *c1 = "YOU";
> std::cout <<"3"<<std::endl;
> std::cout <<*c1<<std::endl;
> (*c1)++;
> std::cout <<*c1<<std::endl;
> (*c1)++;
> std::cout <<*c1<<std::endl;
>
> }



As the previous posters implied, this approach is not recommended.

-LuB

 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      05-28-2006
LuB schrieb:
>>#include <iostream>
>>
>>int main(){

>
>
> char* d1 = "";
> char** c1 = &d1;


Are you sure you want to do this?

>
>> *c1 = "HOW"; // LINE1
>>
>> std::cout <<"1"<<std::endl;
>>
>> ++(*c1);


Advances the pointer after d1.

>> *c1 = "ARE";


Will do bad things.

>> std::cout <<"2"<<std::endl;
>> ++(*c1);
>> *c1 = "YOU";


Even worser.

>> std::cout <<"3"<<std::endl;
>> std::cout <<*c1<<std::endl;
>> (*c1)++;
>> std::cout <<*c1<<std::endl;
>> (*c1)++;
>> std::cout <<*c1<<std::endl;
>>
>>}

>
>
>
> As the previous posters implied, this approach is not recommended.


And does not work. You should better use std::string and std::vector.

Thomas
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      05-28-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> The following code can be compiled. But When I run it, it causes
> "Segmentation fault".
>
> #include <iostream>
>
> int main(){
> char **c1;


Okay, c1 is a pointer to a pointer of char.

> *c1 = "HOW"; // LINE1


Here you are saying to change the contents of where c1 points to point to
the string literal "HOW". But, c1 is not yet pointing to valid memory.
It's pointing anywhere since it was never initialized. So you are
attempting to change memory that your program doesn't "own", hence the
segmentation fault.

What it looks like you are trying here is to have c1 to be an array of
pointers to char. So you need to set aside the memory for the pointers.

This would work:

char* c1[3];
c1[0] = "HOW";

because now c1 is an array of 3 pointers to char, and the memory has been
allocated for 3 pointers. Also, though, you could now reference these by
your char** such as;

char* c0[3];
char** c1 = c0;
*c0 = "HOW";

because c1 now points to memory that has been allocated (by the declaration
of c0 which allocated the space for 3 pointers).


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AVG Email Scanner activating at strange times with strange IP addresses dennispublic@hotmail.com Computer Support 1 08-26-2006 04:27 AM
strange problem John Wireless Networking 4 02-14-2005 05:40 PM
Problem installing modules on redhat 9 - strange connection problem championsleeper Perl Misc 0 01-26-2005 12:19 PM
Strange error! Compiler problem? Or code problem?!!!!!!! Pedro Miguel Carvalho C++ 2 10-25-2004 02:06 PM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM



Advertisments