compiled but when running its reporting crash dumpCode:#include<stdio.h> void allocate(int *,int); int main(){ int* p =NULL; allocate(p,100); printf("%d",*p); } void allocate(int* p ,int x){ p = (int*)malloc(sizeof(int)); *p = x; return 0; }
compiled but when running its reporting crash dumpCode:#include<stdio.h> void allocate(int *,int); int main(){ int* p =NULL; allocate(p,100); printf("%d",*p); } void allocate(int* p ,int x){ p = (int*)malloc(sizeof(int)); *p = x; return 0; }
Last edited by naren26051991; 08-04-2012 at 09:00 AM.
Consider this program:
Why is the output 0 even though I assigned 1 to x in the assign function?Code:#include <stdio.h> void assign(int x); int main(void) { int x = 0; assign(x); printf("%d\n", x); return 0; } void assign(int x) { x = 1; }
Once you understand why, it should become clear to you that in your allocate function, this:
does not change the value of p in the main function. Hence, you should either be passing a pointer to a pointer, or returning the pointer.Code:p = (int*)malloc(sizeof(int));
By the way, since you declared allocate as having void return type, you should not be returning 0. Also, please indent your code properly.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
This program as such compiles and runs fine.
BUT i guess you try to use "p" after that line "allocate(p,100)" and of course dereferencing NULL fails because your function doesn't actually change the pointer value.
What you want is:
At last don't forget to include stdlib.h for malloc.Code:int main(void) { ... allocate(&p, 100); ... } void allocate(int **p, int x) {...}
PS: I wrote the reply at the same time than laserlight so sorry for the redundant info.
Last edited by root4; 08-04-2012 at 09:06 AM.
The variable 'p' in main is different from 'p' in allocate. Any changes to 'p' in allocate are not seen by main.
There are a few other problems I saw in your code as well:
Your main function doesn't have a return statement.
allocate does have a return value but it shouldn't.
You should not cast the return value of malloc, and you should include <stdlib.h> for malloc's prototype.
tell me how actually this thing works
actually i am passing the pointer type variable to allocate(p,100)
i am allocating memory through malloc function and
when i am testing it through printf in the allocate function its working but when i am testing the memory allocated through printf in main function its compiling but again crash dump reported pls explain this concept clearly
Both main and allocate have variables named 'p'. They are two different variables. Changing p in allocate does not affect p in main. Function calls in C are pass by value, which means the value of main's p (NULL) is passed in to allocate as its own copy, which is also named p. Changing p in allocate changes only allocate's copy of p.
Okay, I will say it plainly then: your code fails because you assign the result of malloc to a local variable. As such, the pointer named p in the main function is not changed, i.e., it is still a null pointer.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Passing a pointer to an int would be fine if you just wanted to modify the value of an int variable in main. However you want to modify the value of a pointer to an int, so you have to pass a pointer to a pointer to an int, in order to modify it.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"