Click here to Skip to main content
15,880,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all

I have the following array of character pointers

char** test = new char*[256];

Now this array is passed to a function that fills "some" of its locations. My questions is how to check the status of other unfilled pointers? Or what is the status of these pointers?
for example I have this:

test[0] = "hello";
test[1] = "world!";

How to check the status of any of the other 254 pointers? I tested them they are not NULL, not 0, not '\0'. My problem is I have a program that sometimes access these pointers and then crashes, no handling mechanism at all.
PIEBALDconsult 6-Jul-14 18:29pm    
they are not NULL, not 0, not '\0'

Did you initialize the array?
Mohibur Rashid 6-Jul-14 20:57pm    
test[] is a pointer of character; you need to allocate memory for these address;
for(int i=0;i<256;i++){
test[i] = new char[somelength];
then you can assign value to test[]; but this is a character array;

Just initialize the pointer array with 0 like this:
char** test = new char*[256] = {};

char* test[256] = {}

Note that since the initialization list is empty, it will be padded with 0 automatically. See here[^] for more info.

[edit]fixed intialization statement - sadly it won't work with heap variables[/edit]

After nv3 pointed it out I tried initializing the array using new, and it does indeed work with MS VisualSTudio 2010. I couldn't pinpoint a reference to this behaviour though, so I can only report what works with this compiler:
char *test[256] = nullptr;
test = new char*[256](); // note the pair of rounded brackets!
assert(test[123] == 0); // works!
delete [] test;
test = new char*[256]; // no brackets this time
assert(test[123] == 0); // will most likely fail! (compiler dependend)

Share this answer
nv3 7-Jul-14 8:36am    
Stefan, is it "new char*[256] = {}" or "new char*{256] {}"?
Stefan_Lang 8-Jul-14 2:28am    
*doh* shouldn't be a call to new at all - somehow I just copied the declaration ignoring the allocation :/

will fix it...
nv3 8-Jul-14 6:17am    
Well, I was surprised to learn that it WILL work with the new operator as well. C++03 and C++11 brought some additions. "new char*[256]()" with a pair of round braces will do a default initialization, i.e. 0. In so far, you inspired me and perhaps others to look that up. My 5.
Stefan_Lang 9-Jul-14 2:41am    
So it does work with new? I wasn't sure and didn't find a reference on that. And I don't trust MSVC to be conformant to the standard, so I didn't even bother to try.

Thanks for the tip though, I'll update my solution again.
Here is a sample to test. Access to **test will fail if not initialised (pointing to allocated memory).

void CTest::Run()
char** test = new char*[256];
FILE *f;
fopen_s (&f,"b.txt","w");
fprintf(f,"**test: 0x%x\n",test);
for(int i=0;i<256;i++)
fprintf(f,"*test %d=> 0x%x\n",i, *test);
//fprintf(f,"test[i] %d=> 0x%x\n",i, **test);//access violation

Output sample:

**test: 0x6d3f68
*test 0=> 0xcdcdcdcd
*test 1=> 0xcdcdcdcd
Share this answer
Abdallah Al-Dalleh 7-Jul-14 2:21am    
That's the point how to detect the pointer access will fail before reading ?
Hello all :D

I think it's already handled, it appears that all other unfilled locations in the char** test are given NULL after filling the array so all I had to do is to check if the value is null or not. Thanks all for the help :D
Share this answer
nv3 7-Jul-14 17:14pm    
Hello Abdallah! Good that your problem has been solved. Would you please honor one or more of the solutions by formally accepting them. This is the "thank you" for those who have spent their time to help you. -- And posting your own message as a solution is not the right way to do it and being frowned upon. A comment under your question would have been the way to do that.
Stefan_Lang 8-Jul-14 2:34am    
FYI, you cannot rely on any value to be intialized to 0 automatically. This may or may not be true, depending on the compiler, or even compiler settings. Therefore you must initialize the entire array to 0 (or nullptr) yourself. If you define the array opn the stack rather than on the heap you can use the initialization syntax I posted in solution 2. Otherwise you should run a loop over all elements and assign them to 0.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900