Memory allocated with malloc does not persist outside function scope?
Posted
by PM
on Stack Overflow
See other posts from Stack Overflow
or by PM
Published on 2010-03-25T05:16:08Z
Indexed on
2010/03/25
5:23 UTC
Read the original article
Hit count: 145
Hi,
I'm a bit new to C's malloc function, but from what I know it should store the value in the heap, so you can reference it with a pointer from outside the original scope. I created a test program that is supposed to do this but I keep getting the value 0, after running the program. What am I doing wrong?
int f1(int * b) {
b = malloc(sizeof(int));
*b = 5;
}
int main() {
int * a;
f1(a);
printf("%d\n", a);
return 0;
}
© Stack Overflow or respective owner