473,385 Members | 1,821 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,385 software developers and data experts.

realloc causing segfault (under Electric Fence)

kj


I am trying to diagnose a bug in my code, but I can't understand
what's going on. I've narrowed things down to this:

I have a function, say foo, whose signature looks something like:

int foo( int w, int x, int y, int z, my_struct **results )

During its execution, foo initializes *results using calloc:

( *results ) = calloc( w+1, sizeof( my_struct ) );

and proceeds to fill the resulting array of my_struct's, keeping
track of how many elements of *results it has filled in some int
variable v. At the very end, right before returning, foo uses
realloc like this

realloc( *results, v * sizeof( my_struct ) );

and then foo returns v.

In normal operation, the value of w remains constant, and v never
exceeds w+1 (an assert statement ensures this). In the particular
test runs discussed below, w+1 always equals 2, and in the realloc
statement v also equals 2, always; therefore, if I understand
realloc correctly, in these test runs the realloc should not change
the amount of memory allocated to *result. (But somehow the realloc
is doing something that Electric Fence (EF) doesn't like...)

If before and after the realloc line above I insert *identical*
printf statements, like this:

printf( "%d\n", ( *results )->some_field ); /* always OK */
realloc( *results, v * sizeof( my_struct ) );
printf( "%d\n", ( *results )->some_field ); /* segfault under EF */

both printfs execute and the expected values get printed, but if
I compile with Electric Fence (-lefence) and re-run the program,
it fails with a segfault at the second printf, right after the
realloc.

(My compiler is gcc 3.3.5; I don't know how to determine the version
of EF that we have on our system, though the man page says 1993,
so I gather it's pretty ancient.)

I am very puzzled by the results described above. What am I doing
wrong? As mentioned above, the value of v is 2, so, if realloc
worked correctly, why would the second printf produce a segfault?

Is there anything better (and preferably free or cheap) than Electric
Fence to pinpoint the problem?

Thanks!

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Aug 10 '06 #1
3 2283
kj <so***@987jk.com.invalidwrote:
I have a function, say foo, whose signature looks something like:

int foo( int w, int x, int y, int z, my_struct **results )

During its execution, foo initializes *results using calloc:

( *results ) = calloc( w+1, sizeof( my_struct ) );

and proceeds to fill the resulting array of my_struct's, keeping
track of how many elements of *results it has filled in some int
variable v. At the very end, right before returning, foo uses
realloc like this

realloc( *results, v * sizeof( my_struct ) );

and then foo returns v.
If before and after the realloc line above I insert *identical*
printf statements, like this:

printf( "%d\n", ( *results )->some_field ); /* always OK */
realloc( *results, v * sizeof( my_struct ) );
printf( "%d\n", ( *results )->some_field ); /* segfault under EF */

both printfs execute and the expected values get printed, but if
I compile with Electric Fence (-lefence) and re-run the program,
it fails with a segfault at the second printf, right after the
realloc.
Post real code. There could be several reasons for this behaviour, one
being that you call foo() wrong, another being that your realloc() call
as above is not sufficient to ensure correct behaviour but might be
right as part of a larger statement; we cannot know what is really going
on unless we also know the context in which you call these functions.

Whittle your program down to the smallest program that exhibits the
problem, but still compiles. Then copy and paste that and post it here -
do not retype, you'll introduce typos.

Richard
Aug 10 '06 #2
kj wrote:
I am trying to diagnose a bug in my code, but I can't understand
what's going on. I've narrowed things down to this:

I have a function, say foo, whose signature looks something like:

int foo( int w, int x, int y, int z, my_struct **results )

During its execution, foo initializes *results using calloc:

( *results ) = calloc( w+1, sizeof( my_struct ) );

and proceeds to fill the resulting array of my_struct's, keeping
track of how many elements of *results it has filled in some int
variable v. At the very end, right before returning, foo uses
realloc like this

realloc( *results, v * sizeof( my_struct ) );
Which is wrong. You need to care for the return value of
realloc as the documentations says.

In particular, the old piece you pass to realloc might not
be valid if realloc succeeds.
Aug 10 '06 #3

kj wrote:
I am trying to diagnose a bug in my code, but I can't understand
what's going on. I've narrowed things down to this:
>
If before and after the realloc line above I insert *identical*
printf statements, like this:

printf( "%d\n", ( *results )->some_field ); /* always OK */
realloc( *results, v * sizeof( my_struct ) );
printf( "%d\n", ( *results )->some_field ); /* segfault under EF */

both printfs execute and the expected values get printed, but if
I compile with Electric Fence (-lefence) and re-run the program,
it fails with a segfault at the second printf, right after the
realloc.
Looks like realloc is returning a new pointer. In which case the value
of *results has been invalidated. You should do something more like

results **tmp = realloc(*results, v * sizeof( my_struct ) );
if (tmp == NULL) { die_badly(); }
*results = tmp;

You say that you didn't expect realloc to change your pointer -- did
you check?
>
Is there anything better (and preferably free or cheap) than Electric
Fence to pinpoint the problem?
<OTI like Valgrind, if you have questions ask in a newsgroup where it
is on topic, maybe one of the linux groups </OT>

-David

Aug 10 '06 #4

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

Similar topics

11
by: Eitan Michaelson | last post by:
Hi, Can any one tell me what's wrong with this code? It leaks (acceding to bound checker), when it attempts to reallocate memory. The code is not pure C, but with minor adjustments any C...
20
by: Jonas | last post by:
Hi, I'm 99 % sure that Standard C guarantees to do a memory move inside realloc() in case the new, returned memory block (address) is different than the original one. Can any C expert confirm...
86
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...
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...
12
by: Andrew Clark | last post by:
Hi all, Wow, has it been a long time since I've been here. Too long. Anyway, I thing I have found the source of a segfault in my program, but I can't see anything wrong with this code (used to...
37
by: ravi.cs.2001 | last post by:
Hi all, I m relatively new to C. I have few queries related to malloc(): #1. When we perform malloc(), the memory allocated dynamically comes from the heap area of the process in concern....
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>
40
by: Dave | last post by:
Hello, I'm teaching myself C by working my way through Steve Summit's tutorial (http://www.eskimo.com/~scs/cclass/cclass.html). In one of the questions (assignment 6, exercise 7), you have to...
27
by: Kislay | last post by:
How is realloc implemented internally ? If there is not enough memory in place to allocate , is new memory allocated somewhere else and the 2 regions linked via a pointer , OR , is the old region...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.