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
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.
- 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...) :
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*)