This question already has answers here:
Is free() zeroing out memory?
(7 answers)
Closed 5 years ago.
int main()
{
int *p,*q;
p=(int *)malloc(sizeof(int));
*p=5;
free(p);
}
When I examine the memory address allocated to p after the execution of the free(p) statement, I observe that the memory content is 0. Is this the right behavior because I have read that free does not initialize the memory to 0?
The content of *p after free(p) returns is undefined. It may be 0, 42 or anything else.
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int *p, *x;
p = malloc(sizeof(int));
*p = 5;
// the following will print "0x7fd0cac00350: 5" or similar
printf("%p: ", p);
printf("%d\n", *p);
free(p);
printf("%p\n", p); // won't change from before
printf("%d\n", *p); // UNDEFINED BEHAVIOR
}
Related
What is the solution to the following problem:
I have a null pointer which I pass to another function (a central place for doing all allocation and some actions on the allocated memory), which should do the actual allocation on that pointer. This is what I have tried:
#include <stdio.h>
#include <stdlib.h>
void xf(int *p) {
p = malloc(sizeof (int));
printf("xf: %p\n", p);
*p = 123;
}
int *pf() {
int *p = NULL;
xf(p);
printf("pf: %p\n", p);
return p;
}
int main()
{
int *a = pf();
printf("===%p %p===\n", NULL, a);
printf("%d\n", *a);
return 0;
}
but returned pointer of pf() is still the null pointer and not the in xf allocated memory address. The above prints something like:
xf: 0x56392c0122a0
pf: (nil)
===(nil) (nil)===
Segmentation fault
Any suggestions?
you are changing the pointer so do this
void xf(int **p) {
*p = malloc(sizeof (int));
printf("xf: %p\n", *p);
**p = 123;
}
and
int *p = NULL;
xf(&p);
This question already has answers here:
Changing address contained by pointer using function
(5 answers)
Passing pointer to function value not changing [duplicate]
(4 answers)
Closed 1 year ago.
I don't understand the behavior of this program.
By my understanding, pointer in main() should have the original address of the local variable that is, now, destroyed. Because pointer was assigned the valid address at that point.
It should just keep that address. Why is it losing that?
Here's the program
#include <stdio.h>
void fun(int* ptr)
{
int a = 5;
ptr = &a;
printf("address: %p, value: %d\n", &a, a);
// address: 0x7fff1aa00374, value: 5
printf("address: %p, value: %d\n", ptr, *ptr);
// address: 0x7fff1aa00374, value: 5
}
int main(void)
{
int* ptr = NULL;
fun(ptr);
printf("address: %p\n", ptr);
// address: (nil)
printf("address: %p\tvalue: %d\n", ptr, *ptr);
// Segmentation fault (core dumped)
return 0;
}
GCC Version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Here is a modified example:
#include <stdio.h>
void fun(int p)
{
p = 5;
printf("value: %d\n", p);
}
int main(void)
{
int p = 0;
fun(p);
printf("value: %d\n", p);
return 0;
}
If you understand why this modified example prints 5 then 0, then you also understand why your pointer version doesn't work either. Because pointers too are passed by value.
If you don't understand why this prints 5 then 0, then you need to read a beginner-level C book, functions chapter.
For better understanding of what's happening here, renaming the ptr parameter of fun() function to fun_ptr
void fun(int* fun_ptr)
^^^^^^^
When you call fun() function
int* ptr = NULL;
fun(ptr);
the value of argument ptr will be assigned to fun() function parameter fun_ptr. Its similar to this
fun_ptr = ptr;
ptr is NULL, so the fun_ptr will be assigned NULL. Whatever changes you do in fun_ptr inside fun() function will stay within fun() function block because the scope of identifier within the list of parameter declarations in a function definition has block scope which terminate when function returns.
If you want to make changes to a pointer variable in another function, you have to pass the address of that pointer variable to the calling function. In context of your program, it should be
void fun(int** ptr) {
int a = 5;
*ptr = &a;
// *ptr will be the pointer passed as argument i.e. assign anything
// to *ptr means you are making changes to pointer passed as argument
.....
.....
}
int main(void)
{
int* ptr = NULL;
fun(&ptr);
.....
.....
return 0;
}
Even after making these changes your program will have Undefined Behaviour because it is accessing address of local variable outside of its scope. Variable a is a local(automatic) non-static variable and its lifetime is limited to its scope i.e. the block in which it has been declared. Any attempt to access it outside of its lifetime lead to undefined behaviour.
To resolve this, allocate memory to pointer ptr and assign the value of a to it. Or, you can declare a as static variable. The lifetime of a static variable is entire run of the program. You can do:
#include <stdio.h>
#include <stdlib.h>
void fun1 (int** ptr) {
static int a = 5;
*ptr = &a;
printf("fun1(): address: %p, value: %d\n", (void *)&a, a);
printf("fun1(): address: %p, value: %d\n", (void *)*ptr, **ptr);
}
void fun2 (int** ptr) {
int a = 5;
*ptr = malloc (sizeof (int));
if (*ptr == NULL) {
fprintf (stderr, "Failed to allocate memory\n");
exit (EXIT_FAILURE);
}
**ptr = a;
printf("fun2(): address: %p, value: %d\n", (void *)&a, a);
printf("fun2(): address: %p, value: %d\n", (void *)*ptr, **ptr);
}
int main (void) {
int* ptr1 = NULL;
int* ptr2 = NULL;
fun1(&ptr1);
printf("main(): address: %p\n", (void *)ptr1);
printf("main(): address: %p, value: %d\n", (void *)ptr1, *ptr1);
fun2(&ptr2);
printf("main(): address: %p\n", (void *)ptr2);
printf("main(): address: %p, value: %d\n", (void *)ptr2, *ptr2);
// Once done with dynamic allocated memory, release it
free(ptr2);
return 0;
}
This is because you are passing by value, just like passing an integer, char, etc. You cannot change a parameter passed by value. In this case, the value is the address. To actually change the address of ptr would require passing a passing a double pointer (int **ptr), but that would require more changes to your code.
This question already has answers here:
How do I modify a pointer that has been passed into a function in C?
(7 answers)
Closed 3 years ago.
How can I change a heap allocated global pointer from within a function? Here is what I am working with, (call to modify_p seg faults and I can't see much in the debugger):
#include <stdio.h>
#include <stdlib.h>
int *p;
void set_p(int *p, int sz) {
p = malloc(sz * sizeof(int));
}
void modify_p(int *p) {
p[0] = 2;
}
int main(int argc, char *argv[])
{
set_p(p, 3);
modify_p(p);
//printf("%d\n", p[0]);
// should print 2
return 0;
}
Thanks
The issue is that you pass a copy of p to set_p. So the original p is never modified and never points to valid memory, and the copy is lost at the end of set_p, leaking the memory. Instead, pass a pointer to p (so a pointer to a pointer), like this:
void set_p(int **p, int sz) {
*p = malloc(sz * sizeof(int));
}
And call the function like this:
set_p(&p, 3);
Then you get the expected result.
One way would be to do this:
#include <stdio.h>
#include <stdlib.h>
int *p;
void set_p(int sz) {
p = malloc(sz * sizeof(int));
}
void modify_p() {
p[0] = 2;
}
int main(int argc, char *argv[])
{
set_p(3);
modify_p();
printf("%d\n", p[0]);
// should print 2
return 0;
}
As p is declared outside of any function, you can write directly to it. When you do set_p(p, 3);, the set_p function will have it's local copy of the variable.
If you want to change what a pointer points to, you have to pass the address of the pointer (as shown by Blaze).
This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Closed 6 years ago.
I started studying pointers and I got in some trouble with the next code:
#include <stdio.h>
int * g()
{
int a = 10;
return &a;
}
void main()
{
int *p;
p=g();
printf("%d",*p);
}
It returns the error segmentation fault. core dumped
I would really apreciate any help. Have a nice day!
You are returning the address of a local variable. When you leave the function your code does not know this variable anymore, thus the segmentation fault.
You would have to give a pointer to this function as a parameter or dynamically create memory for this variable in the heap.
e.g.
void g(int* p) {
*p = 10;
}
int main() {
int a;
g(&a);
printf("%d", a);
return 0;
}
or
int* g() {
int* p = (int*) malloc(sizeof(int));
*p = 10;
return p;
}
int main() {
int* p;
p = g();
printf("%d", *p);
free(p)
return 0;
}
This is a fairly simple question about malloc and scope. From my understanding of malloc, it allows you to dynamically allocate memory for variables etc. Shouldn't this mean that the variable remains accessible until the memory is freed? I wrote a couple lines of code but I run into this error when I try to compile.
Error message:
"malloc.c:16:37: error: use of undeclared identifier 'a'"
#include <stdio.h>
#include <stdlib.h>
int* createnum(int n)
{
int* a = malloc(sizeof(int));
*a = n;
printf("%p\n", a);
printf("%d\n", *a);
return a;
}
int main(void)
{
createnum(1);
printf("The variable is %d\n", a);
return 0;
}
You said,
Shouldn't this mean that the variable remains accessible until the memory is freed?
That is not correct. The memory is accessible if it is returned from the function like you are but not the variable. The variable is accessible only in createnum.
You want something like:
int* a = createnum(1);
Of course, you'll have to change
printf("The variable is %d\n", a);
to
printf("The variable is %d\n", *a);
since type of a is int*, not int.
Something like this!
#include <stdio.h>
#include <stdlib.h>
int* createnum(int n)
{
int* a = malloc(sizeof(int));
*a = n;
printf("%p\n", a);
printf("%d\n", *a);
return a;
}
int main(void)
{
int *p = createnum(1);
printf("The variable is %d\n", *p);
free(p);
return 0;
}