an array of pointers, causing segmentation fault

J

Jess

char* const x = "abc"; // compile error

const char* const x = "abc"; // ok
If the first one raises compiler error, then why no error was raised
for my program above, which is

char* x[] = {"a","b","c"};
I would expect the compiler to raise an error because the code should
be:
const char* x[] = {"a","b","c"};
 
J

Jess

Constant strings in C++ are not generally writable; however, it's
perfectly acceptable to define arrays of characters that you can change:

char x[][2] = {"a", "b", "c"};

"x" should be a 2D array, whose type is effectively char*, it looks to
me exactly the same as
char* x[] = {"a", "b", "c"};

If there's a multi-dimensional array "a" (say 2D), then "a"'s type can
always be regarded as some pointer type. for the example above, "a" is
a 2D array, and it's type is char*. Is this not right?
 
J

Jeroen

Jess schreef:
Not sure if the following is correct:

int main()
{
char* x[] = {"a","b","c"};

foo(x[0]);

}

void foo(char *p)
{
*p = 'A';

}

Does this code work?

No, it should give the same segfault as the OP described. The code
snippet was only ment to illustrate that finding lines of code where a
segfault could occur can be difficult for a compiler (well, I just
thought it could be difficult...). It does not fit in the rest of the
discussion in this thread.

Jeroen
 
L

Lionel B

If the first one raises compiler error, then why no error was raised for
my program above, which is

Sorry, you took that out of context (which you've snipped); I didn't mean
it *does* raise an error, that was simply my interpretation of what
Victor Bazarov meant by "fixed" earlier in the thread, when he said "The
mere initialisation of 'x' with string literals should be the red flag.
Once it's fixed..."
^^^^^

[...]
 
M

Mumia W.

Constant strings in C++ are not generally writable; however, it's
perfectly acceptable to define arrays of characters that you can change:

char x[][2] = {"a", "b", "c"};

"x" should be a 2D array, whose type is effectively char*, it looks to
me exactly the same as
char* x[] = {"a", "b", "c"};

No, the first element of 'x' has two characters.
If there's a multi-dimensional array "a" (say 2D), then "a"'s type can
always be regarded as some pointer type. for the example above, "a" is
a 2D array, and it's type is char*. Is this not right?

No, if you pass it to a function, it's more likely to be treated as
"(char*)[2]"--a pointer to two characters.

The two constructs are fundamentally different.

This
char * x[] = {"a", "b", "c"};
creates three pointers that point to the various strings.

This
char * x[][2] = {"a", "b", "c"};
is the same as saying this,
char * x[3][2] = {"a", "b", "c"};
, and it creates no pointers. The data to create the strings is laid out
in memory, and the compiler remembers where each of the strings begin.
 
V

Victor Bazarov

Mumia said:
[..]
The two constructs are fundamentally different.

This
char * x[] = {"a", "b", "c"};
creates three pointers that point to the various strings.

This
char * x[][2] = {"a", "b", "c"};
is the same as saying this,
char * x[3][2] = {"a", "b", "c"};
, and it creates no pointers.

If I may correct this a bit...

First of all both declarations should be without the asterisk:

char x[][2] =
char x[3][2] =

Second of all, the latter declaration (for the sake of clarity) we
should probably write as

char x[3][2] = { { 'a', '\0' }, { 'b', '\0' }, { 'c', '\0' } };

V
 
J

James Kanze

This is an unfortunate language feature [that ought to be abolished
years ago!!!] which led you to believe you are allowed to change the
values of the characters to which elements of 'x' point. You have
initialised 'x' with addresses of arrays of _constant_ characters.
Then you try to change them, and that's undefined behaviour.
Is this because a string literal is always an array of const chars?
Yes.

What if I have an array of string objects, rather than string
literals?

Then it's an array of string objects. The standard certainly
allows things like:
std::string array[] = { "abc", "nothing", "etc." } ;
In this case, it's an array of string objects, and the literals
only serve to initialize the objects, and are not accessible
otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top