473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with realloc ()

#include <stdlib.h>
#include <stdio.h>
int main(){
char *ptr = "hello";
ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char ));
printf ("\n %s", ptr);
return 0;
}
___________________________________
The above program while execution dumps a stack trace and exits. This
is the detailed error message.

*** glibc detected *** ./try: realloc(): invalid pointer: 0x080484b0
***
======= Backtrace: =========
/lib/libc.so.6(realloc+0x38e)[0x94aa3e]
/lib/libc.so.6[0x94ab81]
/lib/libc.so.6(realloc+0x3c)[0x94a6ec]
../try[0x80483af]
/lib/libc.so.6(__libc_start_main+0xdc)[0x8f6f2c]
../try[0x8048301]
======= Memory map: ========
00295000-00296000 r-xp 00295000 00:00 0 [vdso]
008c0000-008d9000 r-xp 00000000 08:02 163372 /lib/ld-2.5.so
008d9000-008da000 r-xp 00018000 08:02 163372 /lib/ld-2.5.so
008da000-008db000 rwxp 00019000 08:02 163372 /lib/ld-2.5.so
008e1000-00a18000 r-xp 00000000 08:02 163373 /lib/libc-2.5.so
00a18000-00a1a000 r-xp 00137000 08:02 163373 /lib/libc-2.5.so
00a1a000-00a1b000 rwxp 00139000 08:02 163373 /lib/libc-2.5.so
00a1b000-00a1e000 rwxp 00a1b000 00:00 0
00cc7000-00cd2000 r-xp 00000000 08:02 163382 /lib/
libgcc_s-4.1.1-20061011.so.1
00cd2000-00cd3000 rwxp 0000a000 08:02 163382 /lib/
libgcc_s-4.1.1-20061011.so.1
08048000-08049000 r-xp 00000000 08:08 288064 /home/subh/try
08049000-0804a000 rw-p 00000000 08:08 288064 /home/subh/try
08a5c000-08a7d000 rw-p 08a5c000 00:00 0
b7fd4000-b7fd5000 rw-p b7fd4000 00:00 0
b7ff0000-b7ff1000 rw-p b7ff0000 00:00 0
bfd49000-bfd5f000 rw-p bfd49000 00:00 0 [stack]
Aborted
__________________________________________________ __
I don't know what is going wrong. Please explain.

Thanks
Anirbid

Feb 17 '07 #1
3 11188
On Feb 17, 10:49 am, "anirbid.baner...@gmail.com"
<anirbid.baner...@gmail.comwrote:
#include <stdlib.h>
#include <stdio.h>
int main(){
char *ptr = "hello";
ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char ));
printf ("\n %s", ptr);
return 0;}

___________________________________
The above program while execution dumps a stack trace and exits.
And rightfully so.

You must call realloc () and free () only for pointers that have been
previously allocated with a successful call to malloc, calloc or
realloc and that have not been free'd yet, or for null pointers.

Feb 17 '07 #2
an**************@gmail.com said:
#include <stdlib.h>
#include <stdio.h>
int main(){
char *ptr = "hello";
ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char ));
printf ("\n %s", ptr);
return 0;
}

Christian Bau has already answered your question about the above code,
so I won't repeat what he's said. But I would like to take the
opportunity to add a few comments of my own:

1) never cast the return value of realloc. It's not necessary or
desirable, and can have the unhappy side effect of suppressing a
diagnostic message should you ever forget to #include <stdlib.h>
(which, on this occasion, you did not).

2) don't use the same pointer object to catch realloc's return value.
Since realloc returns NULL if it is unable to resize the memory block,
you run the risk of overwriting your only pointer to the original
block. Here's how to do it properly:

char *ptr = malloc(n * sizeof *ptr);
if(ptr != NULL)
{
/* okay, we have a memory block. Let's pretend we've used it
for a bit, and we discover it's not enough... */

if(not enough memory)
{
char *tmp = realloc(ptr, newcount * sizeof *ptr);
if(tmp != NULL)
{
/* ptr is now indeterminate, but tmp is valid, so we can
do this... */
ptr = tmp;
tmp = NULL;
}
else
{
/* tmp is invalid, but ptr is still a good pointer to the
memory we already had */
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 17 '07 #3
an**************@gmail.com wrote:
#include <stdlib.h>
#include <stdio.h>
int main(){
If you're not going to use command line arguments, then int main(void)
might be better.
char *ptr = "hello";
ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char ));
realloc can only resize memory which has previously been allocated by
malloc or calloc. Similarly, free can only release memory obtained
from malloc, calloc or realloc. Specifically, the pointer value that
you pass to realloc or free must be the same value that malloc or
calloc returned or a previous call of realloc returned. In the above
case, you violate these requirements, hence leading to undefined
behaviour.

An exception involves passing a null pointer to realloc or free. In
that case realloc behaves like malloc, while free does nothing.

Also, in C, you don't need to cast the value returned by malloc,
calloc or realloc. sizeof(char) is always one, hence in the statement
above redundant. I would write it as:

ptr = realloc(ptr, 10 * sizeof *ptr);

Notice that I dropped the uneccessary cast to size_t. Also doing
sizeof *ptr keeps the statement correct even when you might happen to
change the type of ptr. If realloc fails it returns a null pointer,
but the original memory whose resizing was requested remains
unchanged. Thus unless you want a memory leak you'll need to backup
the original pointer before passing it to realloc or pass a temporary
to realloc and asign to the original pointer only if successful.
printf ("\n %s", ptr);
Supply a trailing newline to the printf string or call fflush(stdout)
immediatly after the printf, otherwise the output may be buffered and
may fail to appear when expected.

<snip>

Feb 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2920
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
3
2072
by: Thomas Christmann | last post by:
Hi! Sorry for the weird topic, I don't know how to describe it better... I have a little problem here I can't wrap my mind around. If I do: ------------------------------------- #define DWORD...
86
4060
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
13
2011
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to...
9
2481
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
29
7850
by: marvinla | last post by:
Hello! I'm a beginner in C, and I'm having trouble with a pointer-to-pointer reallocation. This piece of code works well, but Valkyrie warns some parts (pointed below), and is breaking my real...
9
3789
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
10
1940
by: Igal | last post by:
hay, i'm doing this program. having problem wiht realloc in the function that reads data structures into array (pointer - bp2), this happens after reading the second record. when call to realloc....
15
1971
by: Igal | last post by:
hay, i have this werid problem with my book adding function, this how it looks book* AddBook(book *bp, unsigned *size) { .... //then i use realloc to allocate space for the new item in the bp...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.