* Using realloc()
@ 2005-06-11 6:07 James Colannino
2005-06-11 8:08 ` Steve Graegert
0 siblings, 1 reply; 2+ messages in thread
From: James Colannino @ 2005-06-11 6:07 UTC (permalink / raw)
To: linux-c-programming
Hey everyone. I have a question about realloc(). I was hoping to use
realloc() to allocate more memory to an already existing string, but the
problem was, I didn't know if the original information would stay intact
or not after running realloc(). I wrote the following program to test this:
#include <stdio.h>
int main() {
char *string;
size_t size = sizeof(char);
string = (char *)malloc(size);
string[0] = 'a';
string[1] = '\0';
printf ("Before realloc(): %p: %s\nsize: %d\n", string, string, size);
size = size * 5;
string = (char *)realloc(string, size);
printf ("After realloc(): %p: %s\nsize: %d\n", string, string, size);
return 0;
}
The string data stays the same after running realloc(), so at least in
this circumstance it worked like I had hoped. However, when printing
the pointer to the screen, I saw that it didn't change, and I know that
sometimes realloc() needs to start at a new address in memory which
means the pointer changes, so my question is, will the original data
stay intact if this happens?
Thanks in advance :)
James
--
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/
"A well regulated militia being necessary to the security of a free
state, THE RIGHT of the people to keep and bear arms SHALL NOT BE
INFRINGED." --United States Constitution, Second Ammendment
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: Using realloc()
2005-06-11 6:07 Using realloc() James Colannino
@ 2005-06-11 8:08 ` Steve Graegert
0 siblings, 0 replies; 2+ messages in thread
From: Steve Graegert @ 2005-06-11 8:08 UTC (permalink / raw)
To: James Colannino; +Cc: linux-c-programming
On 6/11/05, James Colannino <james@colannino.org> wrote:
> Hey everyone. I have a question about realloc(). I was hoping to use
> realloc() to allocate more memory to an already existing string, but the
> problem was, I didn't know if the original information would stay intact
> or not after running realloc(). I wrote the following program to test this:
>
> #include <stdio.h>
>
> int main() {
>
> char *string;
> size_t size = sizeof(char);
>
> string = (char *)malloc(size);
> string[0] = 'a';
> string[1] = '\0';
>
> printf ("Before realloc(): %p: %s\nsize: %d\n", string, string, size);
>
> size = size * 5;
> string = (char *)realloc(string, size);
>
> printf ("After realloc(): %p: %s\nsize: %d\n", string, string, size);
>
> return 0;
> }
>
> The string data stays the same after running realloc(), so at least in
> this circumstance it worked like I had hoped. However, when printing
> the pointer to the screen, I saw that it didn't change, and I know that
> sometimes realloc() needs to start at a new address in memory which
> means the pointer changes, so my question is, will the original data
> stay intact if this happens?
James,
The data will stay the same. realloc() takes care. A new pointer is
returned only if there is not enough contiguous unused memory
available at the starting address given by the pointer to realloc().
Just increase the factor in line
size = size * 5;
to something like 500000 and you will observe the scenario you just described.
Kind Regards
\Steve
--
Steve Graegert <graegerts@gmail.com>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176) 21 24 88 69
Office: +49 (9131) 71 26 40 9
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-06-11 8:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-11 6:07 Using realloc() James Colannino
2005-06-11 8:08 ` Steve Graegert
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).