473,657 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

realloc() hang

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 this in a simpler code, but in the simpler
code the program works fine.

Is there any reason realloc would just hang without producing any error
message?

Jonathan Shan

Jul 21 '06 #1
7 2989


Jonathan Shan wrote On 07/21/06 15:42,:
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 this in a simpler code, but in the simpler
code the program works fine.

Is there any reason realloc would just hang without producing any error
message?
No "good" reason, but plenty of "bad" ones. Most
likely, the program has made the same kind of error that
can sometimes cause the memory-management functions to
crash: called free() or realloc() on memory that wasn't
dynamically allocated or that has already been released,
stomped on the memory-manager's data structures with a
wild pointer or crazy array index, or something of that
sort.

Since it sounds as though realloc() may be in an
infinite loop, I'd be particularly suspicious of double-
freeing: releasing the same memory pointer twice. In some
implementations this may well introduce a loop into the
manager's internally-maintained lists of available space;
if realloc() is hunting through a list that looks like

+-- A -- B --+
| |
+------ C <------+

.... you can see that it might take an appreciable length
of time before it gets to the end ...

--
Er*********@sun .com

Jul 21 '06 #2
free() function is not used in the program. The memory is allocated
using malloc(). Then this pointer is passed around (by reference)
through several functions until it reaches a function called insert.
Inside insert function is a realloc() to increase the length of the
array. I have specifically checked for the address of pointer that is
being passed into realloc. It is "correct", as in it matches the
original address when declared initially.

The strangest part is that inside each function nothing is happening to
the original array. It is just simply being passed around. I have
created simpler code which works correctly. The difference between the
working and non-working code is just some functions, declarations, ...
which don't touch the array.

Backtrace using gdb is:

#0 0xb7eb0189 in pthread_setcanc eltype () from /lib/tls/libc.so.6
#1 0xb7e3bb81 in malloc_get_stat e () from /lib/tls/libc.so.6
#2 0xb7e3a33c in realloc () from /lib/tls/libc.so.6
#3 0x08048f85 in insert_to_bucke t (rvalid=0x80850 68, s1=0xb7b00690
"172.16.0.2 40", buf=0xbffcb9b4 "SNMPv2-MIB::sysDescr.0 = No Such Object
available on this agent at this OID")
at testfile.c:235
#4 0x08048b70 in print_result (status=0, sp=0xb7b004e8,
pdu=0xb7b112a8, qvalid=0xbffcbf 44) at testfile.c:105
#5 0x08048ef3 in synchronous (pvalid=0xbffcb f44) at testfile.c:209
#6 0x0804900a in main (argc=1, argv=0xbffcbfc4 ) at testfile.c:261

Jonathan Shan

Eric Sosman wrote:
Jonathan Shan wrote On 07/21/06 15:42,:
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 this in a simpler code, but in the simpler
code the program works fine.

Is there any reason realloc would just hang without producing any error
message?

No "good" reason, but plenty of "bad" ones. Most
likely, the program has made the same kind of error that
can sometimes cause the memory-management functions to
crash: called free() or realloc() on memory that wasn't
dynamically allocated or that has already been released,
stomped on the memory-manager's data structures with a
wild pointer or crazy array index, or something of that
sort.

Since it sounds as though realloc() may be in an
infinite loop, I'd be particularly suspicious of double-
freeing: releasing the same memory pointer twice. In some
implementations this may well introduce a loop into the
manager's internally-maintained lists of available space;
if realloc() is hunting through a list that looks like

+-- A -- B --+
| |
+------ C <------+

... you can see that it might take an appreciable length
of time before it gets to the end ...

--
Er*********@sun .com
Jul 21 '06 #3

Because it makes a perfectly sensible sequence of remarks
hard to follow, that's why.

Why do people dislike it so much?

Top-posting: Writing a response before rather than after
the text being responded to.

Please name a bad habit that Usenet readers dislike.

(I've rearranged your top-posting for better readability;
please don't make it necessary again.)

Jonathan Shan wrote On 07/21/06 16:24,:
Eric Sosman wrote:
>>Jonathan Shan wrote On 07/21/06 15:42,:
>>[...]
Is there any reason realloc would just hang without producing any error
message?

No "good" reason, but plenty of "bad" ones. [...]
Since it sounds as though realloc() may be in an
infinite loop, I'd be particularly suspicious of double-
freeing: releasing the same memory pointer twice. [...]

free() function is not used in the program.
If realloc() decides to "move" a piece of allocated
memory, it allocates a new piece and "frees" the old one.
So even if you never call the free() function, you may be
"free"ing memory anyhow.
The memory is allocated
using malloc(). Then this pointer is passed around (by reference)
through several functions until it reaches a function called insert.
Inside insert function is a realloc() to increase the length of the
array. I have specifically checked for the address of pointer that is
being passed into realloc. It is "correct", as in it matches the
original address when declared initially.
That in itself is somewhat suspicious. If you allocate
a piece of memory and then expand it a few times with realloc(),
there is no reason to believe the memory will still be in the
same place: realloc() may have allocated a new memory area,
copied the old contents into it, and freed the original. But
if you are just handing the same, unaltered pointer around,
that pointer is no longer valid. If you hand that same pointer
to realloc() a second time, after the memory it formerly pointed
to has been freed, that is just as bad as calling free() twice
on the same memory area.
The strangest part is that inside each function nothing is happening to
the original array. It is just simply being passed around. I have
created simpler code which works correctly. The difference between the
working and non-working code is just some functions, declarations, ...
which don't touch the array.
I'm not saying that your problem *is* a double-free, just
that the symptom is suggestive of that cause. It could perfectly
well be a "wild pointer" or "buffer overrun" issue; such errors
have a nasty habit of changing their behavior when you make
"unrelated" changes to the program -- add a printf() and a bug
suddenly appears in a completely different part of the program,
add another printf() to help track it down and the bug vanishes
again ... Errors of this kind are sometimes called "Heisenbugs "
because the observer affects the observed. And because of their
changeable symptoms, they can be bloody murder to track down ...

I'd suggest that you do a really thorough check for double-
freeing (the symptom is highly suggestive), but if that's not
the problem you'll have to work even harder. Posting some code
snippets (complete, compilable, and concise) might draw some
helpful ideas, too. Good luck!

--
Er*********@sun .com

Jul 21 '06 #4
>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 this in a simpler code, but in the simpler
code the program works fine.
If realloc() misbehaves, chances are you passed it a bad pointer
(already freed, not returned from malloc() or realloc()) or wrote
out of range *ANYWHERE IN PREVIOUSLY EXECUTED CODE*. Either one
invokes the wrath of undefined behavior.
>Is there any reason realloc would just hang without producing any error
message?
realloc() is not supposed to "produce an error message". If it
fails, it returns NULL. However, if you have invoked the wrath of
undefined behavior by scribbling on memory (the bug could be
*ANYWHERE* you have previously executed), it might produce messages
like "smegmentat ion violation -- core dumped".

Gordon L. Burditt
Jul 21 '06 #5
In article <1153520931.365 052@news1nwk>,
Eric Sosman <Er*********@su n.comwrote:
>Because it makes a perfectly sensible sequence of remarks
hard to follow, that's why.
And yet, it didn't make it hard to follow at all.

-- Richard
Jul 22 '06 #6
Gordon Burditt wrote:
If realloc() misbehaves, chances are you passed it a bad pointer
(already freed, not returned from malloc() or realloc()) or wrote
out of range *ANYWHERE IN PREVIOUSLY EXECUTED CODE*. Either one
invokes the wrath of undefined behavior.
Indeed, this is source of the problem. What I was trying to do was
create a string from known strings and integers which ranged from 0 to
255. Sprinkled about were numerous mallocs involving both struct and
char*. The fix was to remove all the pointers, and create fixed-size
arrays.

Jonathan Shan

Jul 24 '06 #7
Jonathan Shan wrote:
Gordon Burditt wrote:
If realloc() misbehaves, chances are you passed it a bad pointer
(already freed, not returned from malloc() or realloc()) or wrote
out of range *ANYWHERE IN PREVIOUSLY EXECUTED CODE*. Either one
invokes the wrath of undefined behavior.

Indeed, this is source of the problem. What I was trying to do was
create a string from known strings and integers which ranged from 0 to
255. Sprinkled about were numerous mallocs involving both struct and
char*. The fix was to remove all the pointers, and create fixed-size
arrays.


That's a shotgun fix. You got it to work without really understanding
why it was failing before. The better solution would have been track
down whatever was really causing the problem and fix that, rather than
just blindly changing code until it works.

Somewhere you had a buffer overrun or a double free or something else
that was corrupting dynamic storage.

Brian
Jul 24 '06 #8

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

Similar topics

12
2931
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
9
2349
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 incHeapSize function, which is supposed to increase the malloced space for an integer array by 1: Heap incHeapSize(Heap aHeap) { int* tempHeap; aHeap->maxSize++; tempHeap = realloc(aHeap->heapArray, aHeap->maxSize);
7
2922
by: Marlene Stebbins | last post by:
The bigint struct defines a big integer and represents it as a string of characters: typedef struct bigint { int sign; int size; int initflag; char *number; } bigint;
86
4117
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 bytes. I had always assumed it would automatically free the previous memory, but is the behaviour...
28
3864
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and realloc it for the size of the what is being written each time? In other words, what is the decision factor between deciding to use a fixed size buffer or allocating memory space and reallocing the size? I don't think the code below is optimal...
19
5720
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't care about the old data and I don't need to copy it to the new location. Is there a standard function that could do that? As far as I understand the description of the realloc() function, it _always_ copies the data to the new location, even if that...
12
2090
by: subramanian | last post by:
I have taken the following prototype from K & R. void *realloc(void *p, size_t size); Suppose p was earlier allocated by malloc. Suppose I am calling realloc with larger size value. If realloc is successful, will the return pointer be the same as p or will it be different. K & R 2nd edition says "realloc returns a pointer to the new space".
4
3500
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on malloc(0), and realloc() says it reutrns NULL on failure. (And, on failure, the old pointer has not been freed.) Is it possible for an implementation to return NULL for realloc(ptr,0)
9
3796
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>
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6175
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4171
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2739
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
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.