Question:
Question with malloc and pointer function in C porgramming?
sakuragi h
2007-05-31 08:10:59 UTC
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 (&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?
}
Five answers:
2007-05-31 09:00:40 UTC
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.
mblaine
2007-05-31 08:55:03 UTC
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.
csanon
2007-05-31 13:29:12 UTC
http://en.wikipedia.org/wiki/Pointer_(computing)



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
2007-05-31 08:24:25 UTC
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.
?
2016-10-30 12:15:22 UTC
ok conceptually programming has 2 places the place archives is saved. One is noted as the heap, the different the stack. Your base varieties are saved in the stack and the applying interrogates the stack once you attempt to get right of entry to something. Your reference varieties are saved in the heap and are basically ever accessed by potential of a pointer maintained by potential of the stack. so which you would be able to desire to declare the stack is ordered storage and the heap like its namesake is largely a set of archives. A void pointer is a pointer that has been initialized in the stack yet not yet set to handle a heap area, subsequently it somewhat is a pointer to null. as quickly as a cost has been placed into the heap and its address assigned to the pointer, then the pointer will grow to be an "common pointer" ****** Edit ******** I stand corrected, or take a seat corrected on the 2d. The above describes a null pointer not a void pointer, I defer explaination to my betters Oops tnx for correction.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...