LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2005, 01:55 PM   #1
f0rmula
Member
 
Registered: Dec 2003
Posts: 72

Rep: Reputation: 15
realloc, does it copy?


If it's not able to expand my current allocation, and has to create a different larger allocation, is the contents of the existing smaller allocation copied to the beginning of the new allocation?

I've read this but still unsure..

http://www.opengroup.org/onlinepubs/...h/realloc.html

Thanks

James
 
Old 03-22-2005, 02:01 PM   #2
drisay
Member
 
Registered: Sep 2004
Distribution: Slackware 10
Posts: 167

Rep: Reputation: 30
my understanding is yes
 
Old 03-22-2005, 02:09 PM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Yes, realloc retains the contents of the item of which memory is reallocated.

From the realloc man page:
Quote:
realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be ininitialized.
and

Quote:
realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. If size was equal to 0, either NULL or a pointer suitable to be passed to free() is returned. If realloc() fails the original block is left untouched - it is not freed or moved.
 
Old 03-22-2005, 02:30 PM   #4
f0rmula
Member
 
Registered: Dec 2003
Posts: 72

Original Poster
Rep: Reputation: 15
Brilliant.. thanks..

God know's why I didn't check the man page.. apologies..

James
 
Old 03-22-2005, 02:55 PM   #5
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
(= No worries.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
realloc and segfaulting problem SciYro Programming 2 05-30-2005 08:46 AM
Realloc trouble in C KneeLess Programming 5 11-01-2004 04:16 PM
how to copy and paste acd copy cd paschim Linux - General 2 04-05-2004 09:25 AM
q about realloc() devoyage Programming 3 09-01-2003 12:50 AM
boot error after using Copy Commander to copy debian system onto larger new drive Interceptor Linux - Hardware 7 05-04-2003 12:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration