free problem

A

Andres Steinhilber

Hi there..I would know why I'm having a problem when I'm trying to
free a pointer..
this is the code:

#include<stdio.h>
#include<stdlib.h>

int *reserv_mem(void);

int main()
{
int var = 9;
int *ptr = reserv_mem();
printf("\n enter value : ");
ptr = &var;
free(ptr); //HERE IS THE ERROR
return 0;
}

int *reserv_mem(void)
{
int *temp = NULL;
temp = (int *) malloc(sizeof(int));
return temp;
}
 
L

Lew Pitcher

Hi there..I would know why I'm having a problem when I'm trying to
free a pointer..
this is the code:

#include<stdio.h>
#include<stdlib.h>

int *reserv_mem(void);

int main()
{
int var = 9;

Here, you allocate the var variable, and give it a value of 9.
int *ptr = reserv_mem();

Here, you allocate the ptr variable, and give it a value (ultimately) of the
address of some storage that you allocate with malloc()

printf("\n enter value : ");
ptr = &var;

Here, you change the value of ptr, and make ptr take the value of
the /address/ of the var variable.
free(ptr); //HERE IS THE ERROR

Here, you ask free() to act on a block of memory addressed by the value in
ptr. You will remember that you previously gave ptr the value of
the /address/ of a local variable. Of course, free() can't free local
storage; it only frees malloc()ed storage.
return 0;
}

int *reserv_mem(void)
{
int *temp = NULL;
temp = (int *) malloc(sizeof(int));
return temp;
}

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
B

Barry Schwarz

Hi there..I would know why I'm having a problem when I'm trying to
free a pointer..

First, you must realize that the only values you are allowed to pass
to free are NULL and the value returned by the malloc family of
functions.
this is the code:

#include<stdio.h>
#include<stdlib.h>

int *reserv_mem(void);

int main()
{
   int var = 9;
   int *ptr = reserv_mem();

ptr now contains the value returned by the malloc call in reserv_mem.
This is a suitable value for passing to free.
   printf("\n enter value : ");
   ptr = &var;

Here you discard the value that ptr contained (thus creating a memory
leak) and assign ptr the address of a defined (not allocated) object.
This is not an acceptable value for free.
   free(ptr); //HERE IS THE ERROR

And since the value is not acceptable free did you a favor by
reporting an error. Be thankful it behaved civilly and didn't invoke
one of the more pernicious manifestations of undefined behavior.
   return 0;

}

int *reserv_mem(void)
{
   int *temp = NULL;
   temp = (int *) malloc(sizeof(int));

Get rid of the cast. It doesn't help and its only purpose is to
prevent the compiler from warning you about a potential problem if you
forget to include stdlib.h.
 
A

Andres Steinhilber

First, you must realize that the only values you are allowed to pass
tofreeare NULL and the value returned by the malloc family of
functions.



ptr now contains the value returned by the malloc call in reserv_mem.
This is a suitable value for passing tofree.


Here you discard the value that ptr contained (thus creating a memory
leak) and assign ptr the address of a defined (not allocated) object.
This is not an acceptable value forfree.


And since the value is not acceptablefreedid you a favor by
reporting an error.  Be thankful it behaved civilly and didn't invoke
one of the more pernicious manifestations of undefined behavior.


Get rid of the cast.  It doesn't help and its only purpose is to
prevent the compiler from warning you about a potentialproblemif you
forget to include stdlib.h.

Ok, thanks a lot to both of you, I understood my error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top