473,569 Members | 2,701 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(reall oc+0x38e)[0x94aa3e]
/lib/libc.so.6[0x94ab81]
/lib/libc.so.6(reall oc+0x3c)[0x94a6ec]
../try[0x80483af]
/lib/libc.so.6(__lib c_start_main+0x dc)[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 11202
On Feb 17, 10:49 am, "anirbid.baner. ..@gmail.com"
<anirbid.baner. ..@gmail.comwro te:
#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
2923
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command...
3
2077
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 unsigned long #include <stdio.h> #include <malloc.h>
86
4083
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 that was freed by realloc(), but the only way explicitly mentioned in the C89 standard to free memory via realloc() is to realloc() it down to 0...
13
2020
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 specify the the size, but rather keep him/her typing and after each keyboard hit, the function getch() determines whether he/she entered the ENTER key to...
9
2490
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 *after* calling increase_arrays(), I received segmentation fault. I tried to step it through gdb, and I found out that after calling...
29
7860
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 code. #include <stdio.h> #include <stdlib.h>
9
3792
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
1949
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. i can't figure out what's wrong, think it's soming got to do with freeing bp2. and something called "corruption of the heap". book*...
15
1983
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 pointer bp = (book*)realloc(bp, sizeof(book)); .... }
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.