GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Dec-2005, 02:12
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Incorrect usage of free()


Hey guys,

I wrote some code, and have a logical error. I wrote this code to demonstrate what I'm doing in my code :

This is the phase I'm getting to :
CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
	void *p,*q;

	p = malloc(10000);
	printf("Original Allocation address Prior to <realloc>: 0x%.8X\n",p);
	q = realloc(p,20000);
	printf("Original Allocation address After <realloc>: 0x%.8X\n",p);
	printf("New Allocation address : 0x%.8X\n",q);
	return (0);
}

And here I do something very bad :

CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
	void *p,*q,*dummy;

	p = malloc(10000);
	printf("Original Allocation address Prior to <realloc>: 0x%.8X\n",p);
	q = realloc(p,20000);
	printf("Original Allocation address After <realloc>: 0x%.8X\n",p);
	printf("New Allocation address : 0x%.8X\n",q);

	dummy = p;
	free(dummy);

	return (0);
}

Can someone point me to where I did something wrong ? Did I try to free something that doesn't belong to me ?

Best regards,
Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #2  
Old 08-Dec-2005, 07:22
mikhail mikhail is offline
Junior Member
 
Join Date: Mar 2004
Location: Dublin, Ireland
Posts: 73
mikhail is on a distinguished road

Re: Incorrect usage of free()


Quote:
Originally Posted by kobi_hikri
And here I do something very bad :

CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
	void *p,*q,*dummy;

	p = malloc(10000);
	printf("Original Allocation address Prior to <realloc>: 0x%.8X\n",p);
	q = realloc(p,20000);
	printf("Original Allocation address After <realloc>: 0x%.8X\n",p);
	printf("New Allocation address : 0x%.8X\n",q);

	dummy = p;
	free(dummy);

	return (0);
}

Can someone point me to where I did something wrong ? Did I try to free something that doesn't belong to me ?

Best regards,
Kobi.
I just ran that second one in Dev-C++ and got no error. Executed fine too.

What compiler are you using?
  #3  
Old 08-Dec-2005, 07:28
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: Incorrect usage of free()


It may compile fine...but it should be
Code:
p = realloc(p,20000);
instead of
Code:
q = realloc(p,20000);
logically.
You can use malloc for q again
  #4  
Old 08-Dec-2005, 08:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Incorrect usage of free()


Quote:
Originally Posted by kobi_hikri
Hey guys,

I wrote some code, and have a logical error. I wrote this code to demonstrate what I'm doing in my code :

This is the phase I'm getting to :
CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
	void *p,*q;

	p = malloc(10000);
	printf("Original Allocation address Prior to <realloc>: 0x%.8X\n",p);
	q = realloc(p,20000);
	printf("Original Allocation address After <realloc>: 0x%.8X\n",p);
	printf("New Allocation address : 0x%.8X\n",q);
	return (0);
}

And here I do something very bad :

CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
	void *p,*q,*dummy;

	p = malloc(10000);
	printf("Original Allocation address Prior to <realloc>: 0x%.8X\n",p);
	q = realloc(p,20000);
	printf("Original Allocation address After <realloc>: 0x%.8X\n",p);
	printf("New Allocation address : 0x%.8X\n",q);

	dummy = p;
	free(dummy);

	return (0);
}

Can someone point me to where I did something wrong ? Did I try to free something that doesn't belong to me ?

Best regards,
Kobi.

You can look it up on your favorite on-line language/library reference site. Such as http://www-ccs.ucsd.edu/c/.

You will find something like the following (where I have highlighted the significant part).

"realloc

void *realloc(void *ptr, size_t size);

The function allocates an object of size size, possibly obtaining initial stored values from the object whose address is ptr. It returns the address of the new object if successful; otherwise, it returns a null pointer. You can safely convert the return value to an object pointer of any type whose size is not greater than size.

If ptr is not a null pointer, it must be the address of an existing object that you first allocate by calling calloc, malloc, or realloc. If the existing object is not larger than the newly allocated object, realloc copies the entire existing object to the initial part of the allocated object. (The values stored in the remainder of the object are indeterminate.) Otherwise, the function copies only the initial part of the existing object that fits in the allocated object. If realloc succeeds in allocating a new object, it deallocates the existing object. Otherwise, the existing object is left unchanged.

If ptr is a null pointer, the function does not store initial values in the newly created object.
"

In other words, in your case realloc gives you a brand new space, pointed to by q and frees the space pointed to by p.

Regards,

Dave
  #5  
Old 08-Dec-2005, 08:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Incorrect usage of free()


Quote:
Originally Posted by alcoholic
It may compile fine...but it should be
Code:
p = realloc(p,20000);

If realloc fails for some reason, then you can never free that previously allocated block. This would be a memory leak. (A Bad Thing.)

Quote:
Originally Posted by alcoholic
instead of
Code:
q = realloc(p,20000);
logically.
You can use malloc for q again

I don't see anything ironic about it; what I see is a memory leak. If you destroy the value returned by realloc (by setting q equal to some other value), then you can't free the memory. (Still A Bad Thing.)

Regards,

Dave
  #6  
Old 08-Dec-2005, 15:44
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Incorrect usage of free()


Thank you all.
I'll re-think my approach.

Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #7  
Old 08-Dec-2005, 16:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Incorrect usage of free()


Quote:
Originally Posted by kobi_hikri
Thank you all.
I'll re-think my approach.

Kobi.

What's the problem?

If you have this:

CPP / C++ / C Code:

  p = malloc(size1);
  if (!p) {
    return 0;  /* abort or perform some appropriate recovery */
  }
  else {
    /* normal program execution continues */
  }
.
.
.
 temp = realloc(p, size2);
  if (!temp) {
    free(p);  /* or do whatever you need to do for the bad news case */
    return 0;
  }
  else {
    p = temp;
  }
.
.
.

Now the variable p still points to user's space, which has now been resized. Of course if this is done inside a function, and p is used outside of the function, the argument to the function will be a pointer to p, and the pointer will be dereferenced accordingly.

Everything is safe, and there are no memory leaks, assuming you free p before the program terminates.

Regards,

Dave
  #8  
Old 08-Dec-2005, 23:27
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Incorrect usage of free()


Quote:
Originally Posted by davekw7x
What's the problem?

If realloc works fine, and the "old" allocation is deallocated, then any reference to the "old" allocation is illegal ...
Now I get it.

Kobi,
__________________
It's actually a one time thing (it just happens alot).
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FREE phpNuke Hosting: INSTANT Activation, 22 domains to pick from, Free Support +MORE EZForum.org Free Web Hosting 3 07-May-2006 14:28
Free software ashok999 Member Announcements, Advertisements & Offers 0 28-Sep-2005 05:15
Hosting Serenity - Try us for a 10 day FREE trial today! Paradoxic Web Hosting Advertisements & Offers 2 08-May-2005 15:10
50% OFF Basic Reseller Plan - FREE Domain and DOUBLE Bandwidth - From $5 per month Dailyhosting Web Hosting Advertisements & Offers 0 16-Mar-2005 16:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.