Thursday, July 9, 2009

Question with malloc and pointer function in C porgramming?

My teacher says its "dangerous " to malloc a pointer that is Null'd. How Come? Our example goes something like this:





void function(int **array)


{


*array = malloc(100*sizeof(int)); //He said that we should assert that array != NULL


}





main()


{


int *ptr;


function (%26amp;ptr);


}





Please, anybody can explain? And by the way, is the example equivalent to something like this...? The second example doesn't give me a syntax error.


int *ptr;


ptr = NULL;


ptr = malloc(100*sizeof(int));





Is there a difference between the two?


}

Question with malloc and pointer function in C porgramming?
I think your teacher meant that it's dangerous to use a pointer that is NULL. For example:





#define NULL 0


char *pointer = NULL;


if (*pointer == 'T'){ *pointer = 't';}





might compile fine, but has a completely undefined behavior. Basically what you said is that if memory location 0 is a 'T', then replace it with a 't'. An operating system would prevent an application from reading this memory and you would always get an error.





Another example of dangerous pointer problems is using a pointer that hasn't been initialized:





char *pointer2;


if (*pointer2 == 'B'){*pointer2 = 'b';}





In this example we don't know what memory pointer2 is referencing. It could be memory location 0 or memory location 657845. It could be different every time you start your application. When the 'b' is assigned, it could work depending on what memory was referenced by the pointer. The result of writing this 'b' randomly in memory could result in a variety of problems. It could crash your program or it might just corrupt your data leaving your program to work, but generating faulty results.





I didn't get a syntax error compiling your code. It could be based on your compiler.
Reply:I think there is some confusion here. malloc will return a null pointer if it can not allocate the memory. As such you should always test after a call to malloc to see if your pointer is null. It doesn't make sense to assert that a pointer is not null before the malloc.
Reply:http://en.wikipedia.org/wiki/Pointer_(co...





Either your teacher is having memory problems, or you misunderstood. It's dangerous to dereference a NULL pointer.





When you malloc, there is no guarantee it succeeds. In fact, most of your standard library functions are built like this. printf, scanf, fgets, and so on have a return value that indicates success or failure. In the case of malloc, you should test for NULL after the malloc.





Initializing a pointer to NULL, attempting a malloc and checking for NULL is good practice. A dereference of a NULL pointer will segfault, so if you do have a problem in your program like not checking for null after a malloc, at least you'll know.





Also see http://c-faq.com/null/index.html
Reply:malloc would only return null if there was a problem. And if you try to use a null pointer (i.e. the pointer == NULL), at least in my experience, your program would crash outright. Your teacher wants you to make sure the pointer isn't null after the call to malloc so maybe your program could print a clear error message rather than just silently end right then and there.


No comments:

Post a Comment