473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Weird realloc() error

Hey. I must have an array that can be resized dynamically. I have coded
an implementation of it using malloc/realloc, but I am getting a
runtime error as seen below in GDB:

*** glibc detected *** realloc(): invalid next size: 0x08054828 ***

Program received signal SIGABRT, Aborted.
[Switching to Thread -1218516048 (LWP 14211)]
0xffffe410 in __kernel_vsyscall ()
(gdb) bt
#0 0xffffe410 in __kernel_vsyscall ()
#1 0xb7e2f9b1 in raise () from /lib/tls/i686/cmov/libc.so.6
#2 0xb7e312c9 in abort () from /lib/tls/i686/cmov/libc.so.6
#3 0xb7e636ea in __fsetlocking () from /lib/tls/i686/cmov/libc.so.6
#4 0xb7e6bba2 in free () from /lib/tls/i686/cmov/libc.so.6
#5 0xb7e6ca66 in realloc () from /lib/tls/i686/cmov/libc.so.6
#6 0x0804901f in sockstate_handle_outgoing (arg=0x804c008) at
sockstate.c:75
#7 0xb7f3a361 in start_thread () from
/lib/tls/i686/cmov/libpthread.so.0
#8 0xb7ecfbde in clone () from /lib/tls/i686/cmov/libc.so.6
(gdb)

The code is as follows:

ufds = malloc(sizeof(struct pollfd)*numconns); /* Where ufds is a
pointer to a pollfd, and numconns is the number of sockets that will be
stored in ufds. */

[...]

if( the number of sockets has changed ) {
ufds = realloc(ufds, sizeof(struct pollfd)*numconns); /* numconns
is set to the new number, which could be higher or lower. This is the
line GDB is reporting (sockstate.c:75) */
}

Anyone have any ideas on how to resolve this problem?

Thanks,
Alex

Feb 21 '06 #1
4 3979
al*****@gmail.com writes:
Hey. I must have an array that can be resized dynamically. I have coded
an implementation of it using malloc/realloc, but I am getting a
runtime error as seen below in GDB:
[...]
if( the number of sockets has changed ) {
ufds = realloc(ufds, sizeof(struct pollfd)*numconns); /* numconns
is set to the new number, which could be higher or lower. This is the
line GDB is reporting (sockstate.c:75) */
}

Anyone have any ideas on how to resolve this problem?


The line being reported as in error is probably not the real
problem. The root problem is probably due to overwriting memory
elsewhere in your program.

My guess is that you are using GNU/Linux on x86. In that case, I
recommend running your program under valgrind. Chances are that
it will pinpoint the real problem.
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
Feb 21 '06 #2
On 20 Feb 2006 20:04:31 -0800, al*****@gmail.com wrote in comp.lang.c:
Hey. I must have an array that can be resized dynamically. I have coded
an implementation of it using malloc/realloc, but I am getting a
runtime error as seen below in GDB:

*** glibc detected *** realloc(): invalid next size: 0x08054828 ***

Program received signal SIGABRT, Aborted.
[Switching to Thread -1218516048 (LWP 14211)]
0xffffe410 in __kernel_vsyscall ()
(gdb) bt
#0 0xffffe410 in __kernel_vsyscall ()
#1 0xb7e2f9b1 in raise () from /lib/tls/i686/cmov/libc.so.6
#2 0xb7e312c9 in abort () from /lib/tls/i686/cmov/libc.so.6
#3 0xb7e636ea in __fsetlocking () from /lib/tls/i686/cmov/libc.so.6
#4 0xb7e6bba2 in free () from /lib/tls/i686/cmov/libc.so.6
#5 0xb7e6ca66 in realloc () from /lib/tls/i686/cmov/libc.so.6
#6 0x0804901f in sockstate_handle_outgoing (arg=0x804c008) at
sockstate.c:75
#7 0xb7f3a361 in start_thread () from
/lib/tls/i686/cmov/libpthread.so.0
#8 0xb7ecfbde in clone () from /lib/tls/i686/cmov/libc.so.6
(gdb)

The code is as follows:

ufds = malloc(sizeof(struct pollfd)*numconns); /* Where ufds is a
pointer to a pollfd, and numconns is the number of sockets that will be
stored in ufds. */

[...]

if( the number of sockets has changed ) {
ufds = realloc(ufds, sizeof(struct pollfd)*numconns); /* numconns
is set to the new number, which could be higher or lower. This is the
line GDB is reporting (sockstate.c:75) */
}

Anyone have any ideas on how to resolve this problem?


There is a 99% or better probability that you are writing past the end
of the allocated memory between the call to malloc() and the call to
realloc().

By the way, NEVER use the same pointer to receive the return of
realloc(). If the reallocation fails due to insufficient memory or
some other reason, it returns NULL and the original memory is still
allocated. But you have overwritten the original pointer with NULL,
and can no longer free it, thus creating an instant memory leak.

You should do this:

struct follfd *ufds, *temp;

ufds = malloc( /*... */ );

if (!udfs)
{
/* error handling */
}

/* ... */

temp = realloc(udfs, /* ... */);

if (!temp)
{
/* error handling */
free(udfs);
}
else
{
udfs = temp;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 21 '06 #3
Jack Klein <ja*******@spamcop.net> writes:
[...]
By the way, NEVER use the same pointer to receive the return of
realloc(). If the reallocation fails due to insufficient memory or
some other reason, it returns NULL and the original memory is still
allocated. But you have overwritten the original pointer with NULL,
and can no longer free it, thus creating an instant memory leak.


*Unless* your response to an allocation failure will always be to
abort the program, perhaps after some cleanup code that doesn't need
to refer to the leaked object.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #4
On Tue, 21 Feb 2006 05:33:26 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:
Jack Klein <ja*******@spamcop.net> writes:
[...]
By the way, NEVER use the same pointer to receive the return of
realloc(). If the reallocation fails due to insufficient memory or
some other reason, it returns NULL and the original memory is still
allocated. But you have overwritten the original pointer with NULL,
and can no longer free it, thus creating an instant memory leak.


*Unless* your response to an allocation failure will always be to
abort the program, perhaps after some cleanup code that doesn't need
to refer to the leaked object.


My clean up code often attempts to save the contents of the original
block to a file before exiting, in an attempt to allow for later
salvage of the data.

There are also quite a few cases where the original operation can be
carried out, albeit more slowly, in the existing amount of memory, or
by manually swapping out to a file.

In general, it's never a good idea to lose the original pointer on a
reallocation failure. It is far more benign to have it in the
instances where you might not need it, to need it and not have it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 21 '06 #5

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

Similar topics

9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
27
by: Deephay | last post by:
Greetings all, I have a program that used the realloc() function to change the allocated size of a buffer, the program works with some arguments, but with some other arguments, it will show me...
7
by: Jonathan Shan | last post by:
Hello all, I am trying to run a program which has dynamic array of type struct. The program works until the line which uses realloc function to allocate more memory. I have tried to reproduce...
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
31
by: banansol | last post by:
Hi, I just want to get this right. A call to realloc() will return NULL on error and the original memory is left untouched, both when requesting a larger or a smaller size that the original,...
9
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
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....
35
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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,...
0
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...

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.