newbie: how to detect pointer not allocated yet

B

beginner

I have the follwing code

#include <stdio.h>
#include <stdlib.h>



int main()
{
int k;
int **p;



p = (int **) malloc( 10* sizeof(int*));
// error checking



p[0] = (int *) malloc( sizeof(int));
p[1] = (int *)malloc( sizeof(int));
*(p[0]) = 1;
*(p[1]) = 2;






/* the following is ok */
printf("%d\n", *p[0]);
printf("%d\n", *p[1]);



/* the following is not ok */
k = 0;
while( p[k] )
{
printf("%d\n", *p[k]);
k++;
}



return 0;
}

I am wondering if using "while loop" in the case above is a good idea
or not.
valgrind complaint the value of p[2] has not been initialized yet

thanks
dwisianto
 
K

Kenneth Brody

beginner said:
I have the follwing code

#include <stdio.h>
#include <stdlib.h>

int main()
{
int k;
int **p;

p = (int **) malloc( 10* sizeof(int*));

/* Need to check for malloc() failure. */
if ( p == NULL )
{
fprintf(stderr,"Can't malloc 'p'.\n");
exit(EXIT_FAILURE);
}

/* Init buffer to NULL pointers */
for (k=0 ; k < 10 ; k++ )
p[k] = NULL;
// error checking

p[0] = (int *) malloc( sizeof(int));
p[1] = (int *)malloc( sizeof(int));

Check for malloc() failure on both.
*(p[0]) = 1;
*(p[1]) = 2;

/* the following is ok */
printf("%d\n", *p[0]);
printf("%d\n", *p[1]);

/* the following is not ok */
k = 0;
while( p[k] )
{
printf("%d\n", *p[k]);
k++;
}

return 0;
}

I am wondering if using "while loop" in the case above is a good idea
or not.
valgrind complaint the value of p[2] has not been initialized yet

If you initialize the buffer to NULLs, then the while loop will work
just fine. (Assuming that you are using a NULL pointer as an end-of-
list identifier, which is the case here.)

Another way would to explicitly put a NULL at the end of the list:
p[0] = (int *) malloc( sizeof(int));
p[1] = (int *)malloc( sizeof(int));
p[2] = NULL;

(Of course, you need the appropriate malloc-failure checks here as
well.)

Finally, a not-100%-portable-but-probably-works-on-all-systems-you-are-
likely-to-encounter method would be to use calloc() rather than malloc()
followed by the init loop, as this will return a bufer initialized to
all-bits-zero. This will, of course, not help on a platform where NULL
is not all-bits-zero.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

David Resnick

beginner said:
I have the follwing code

#include <stdio.h>
#include <stdlib.h>



int main()
{
int k;
int **p;



p = (int **) malloc( 10* sizeof(int*));

Cast is not needed in C, and can hide lack of inclusion of stdlib.h --
which I note you have included. Better than sizeof(int*) is
sizeof *p, as it is easier to maintain -- think what if you change
p to be a long int, etc. Comment on cast and arg to sizeof applies
below to p[0] and p[1] creation.
// error checking



p[0] = (int *) malloc( sizeof(int));
p[1] = (int *)malloc( sizeof(int));
*(p[0]) = 1;
*(p[1]) = 2;






/* the following is ok */
printf("%d\n", *p[0]);
printf("%d\n", *p[1]);



/* the following is not ok */
k = 0;
while( p[k] )
{
printf("%d\n", *p[k]);
k++;
}



return 0;
}

I am wondering if using "while loop" in the case above is a good idea
or not.
valgrind complaint the value of p[2] has not been initialized yet

thanks
dwisianto

While loop is fine, but valgrind is correct. You must initialize your
pointers. After allocating, you might set them all to NULL if you want
to use that as a sentinel (using a loop), or just keep track of how
many are in use and use that as the loop guard. I'd prefer to keep
track of how many are in use. But if you want the NULL sentinel,
you need to make the array one bigger that the maximum number of
objects, so you can have a sentinel when you have that many objects
(or you need max number of objects as an additional loop guard).

-David
 
B

beginner

thank Kenneth and David for valuable advice
I finally stop valgrind from complaining using calloc instead of malloc

dwisianto
 
A

akarl

beginner said:
thank Kenneth and David for valuable advice
I finally stop valgrind from complaining using calloc instead of malloc

If I got it right, NULL isn't nesseccarily the same as "all bits zero"
which calloc returns.


August
 
R

Robert Gamble

akarl said:
If I got it right, NULL isn't nesseccarily the same as "all bits zero"
which calloc returns.

Correct, and as Kenneth Brody pointed out, it is not 100% portable.

Robert Gamble
 

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

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top