Does the size of an array of pointers to char matter?

Say you have char *words [84] = {"louie", "douie", ......"movie"}
I get a runtime error "Violation Reading location 0*0000000". When I reduced the number of words in the array from 84 to 30, the program ran fine. As such, I concluded there are only a certain size you can allocate to these arrays. Further input from you guys would be much appreciated
There is a limit, due to memory size, but I don't thinks it's between 30 and 84.

How many strings are you providing in your initializer list?

If you define just 4, like

char *words [84] = {"louie", "douie", "clouie, "movie"};

then the other 80 elements will be set to zero, which means they're null pointers.

So while words[0], words[1], words[2], words[3] are pointers to valid strings.

words[4] .. words[83] are all null pointers!

Are you trying to use any of the null pointers?
Last edited on
i defined all 84, and my for loop tried to access the elements starting from position 0 all the way upto 83 when the runtime error occured. I think your reason is valid as an array is a contiguous block of memory and, as such, would quickly run out of locations. A linked list would probably be a better implementation in this case.
Try declaring the array as const char *words[/*leave this empty*/]={/*...*/};
Note: 'const char *' is the correct type of string literals.
char words[][]{ /* Text */ };

words is a pointer to a pointer. ain't it?
Regarding space :

- Your array is a local variable, so it's stored on the stack
- For vc++, the default stack size is 1Mb. (which I take to be a pretty average size)
- Your array is storing pointers to string literals, which have static storage by default and are therefore not on the stack. Only the pointer are.
- The size of a char* pointer (assuming you're compiling for 32-bit) is 4 bytes.
- Hence an array of 84 char*s will take up 336 bytes
- So the array will occupy (to 2 sf) 0.032% of the available (to the stack) memory.

An array of 1Mb / 4bytes = of 262144 elements would be a different matter!

And (Assuming the = was accidentally omitted...) :

char words[][] = {"louie", "douie", "clouie, "movie"};

Is not valid C++ [1]

For multi-dimensional array definitions, C++ only allows one of the array dimensions to be left undefined.

char words[][16] = {"louie", "douie", "clouie, "movie"};

does work, allocating 16 chars for each string. But the other way round (words[4][]) does not work.

If you want an array of const strings then helios const char* version is the way to go.

I suppose the 2D version could be used to a define a pre-initialized set of buffers for later use. But a vector of strings (i.e. std::vector<std::string>) would usually be used for this purpose.

(Note that if you try and change a char in one of the strings in your array, as originally defined, you will trigger an exception, as the string is stored in const memory. Allowing char* to point at const strings is an evilness to support backwards compatibility with C. New code should always use const char*)

Andy

[1] Unless it's turning up in C++0x?
Last edited on
Topic archived. No new replies allowed.