473,387 Members | 2,436 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,387 software developers and data experts.

Control never returns from malloc()

Hi,

I have some code that never returns from a call to malloc(), does
anyone have any ideas? There is plenty of free memory on the system.
This is running in Linux kernel 2.4.20. The code has been run on
several different Linux machines. This same code runs without any
problems on Mac OS X.

Here is the GDB log:
(gdb) step
1160 hungarian_schedule =
malloc(SCHEDULER_NUM_SLOT*simParams.NUM_LAMBDAS*si zeof(int));
(gdb) print 180*simParams.NUM_LAMBDAS*sizeof(int)
$7 = 1440
(gdb) step

Program received signal SIGINT, Interrupt.
0x42074d44 in malloc_consolidate () from /lib/tls/libc.so.6
(gdb)

I hit CTRL-C to stop the code and that is when it displays that
interrupted in malloc_consolidate().

Any help would be greatly appreciated.

Thanks,

Michael

Jul 23 '05 #1
9 1719
"Michael McGarry" <mi*************@gmail.com> writes:
I have some code that never returns from a call to malloc(), does
anyone have any ideas? There is plenty of free memory on the system.


There is probably a bug in your code. For example, you might be
writing beyond the end of an allocated block. Try a memory
debugger, e.g. valgrind on x86/Linux.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Jul 23 '05 #2

Michael McGarry wrote:
Hi,

I have some code that never returns from a call to malloc(),
Code which you keep a secret from those who might be able to debug it.
does anyone have any ideas?


Show us the code?

Brian

Jul 23 '05 #3
GDB shows that the execution stays in malloc(), could it still be
something related to my writing beyond the end of an allocated block?
Why would the code work on Mac OS X?

Jul 23 '05 #4


mi*************@gmail.com wrote:
GDB shows that the execution stays in malloc(), could it still be
something related to my writing beyond the end of an allocated block?
Why would the code work on Mac OS X?


yes... depending on the malloc/free implementation this can happen.
Writing beyond the end of a malloc'd block results in undefined behavior,
which can even differ between different compilers on the same OS. On the
MAC os if you are indeed doing this the specific undefined behavior is
that it appears to work, although that doesn't mean that it won't cause
other problems down the road. With GCC if you are doing this the
undefined behavior is that it throws a signal...

David
Jul 23 '05 #5
In article <11**********************@l41g2000cwc.googlegroups .com>,
<mi*************@gmail.com> wrote:
:GDB shows that the execution stays in malloc(), could it still be
:something related to my writing beyond the end of an allocated block?
:Why would the code work on Mac OS X?

Because in Mac OS X, you stomp on something different; or
you stomp on the same place but it doesn't happen to have
anything important there because the libraries are different
sizes/locations; or perhaps the Mac OS X allocator chains its
free blocks differently.
For tough problems like this, I highly recommend the product
named Purify, the original company for which has been bought
twice and so is now officially called "IBM Rationale Purify".

It's definitely not a product you need every day, but if you
have stray pointers, or unrealized uninitialized variables
[including from structure copies], or if you are wandering
off the end of an array, then Purify can really save your
fundament. It is not cheap or free, but if you are doing
production coding then it pays for itself in very little time
indeed. There's a free trial period for it.
--
Reviewers should be required to produce a certain number of
negative reviews - like police given quotas for handing out
speeding tickets. -- The Audio Anarchist
Jul 23 '05 #6
Michael McGarry wrote:

I have some code that never returns from a call to malloc(), does
anyone have any ideas? There is plenty of free memory on the system.
This is running in Linux kernel 2.4.20. The code has been run on
several different Linux machines. This same code runs without any
problems on Mac OS X.


Sure, line 1233 in foobar.c is writing over the malloc internal
data. Cut your code down to a minimum that exhibits the problem,
not over 100 to 200 lines, compilable, and publish it here if you
haven't found the problem yourself. We are not genies released
from the bottle.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Jul 23 '05 #7
In article <11**********************@l41g2000cwc.googlegroups .com>,
mi*************@gmail.com says...
GDB shows that the execution stays in malloc(), could it still be
something related to my writing beyond the end of an allocated block?
It could be almost anything. It's hard for anyone to guess without
seeing code. Normally, whittling the code down to a small compilable
program that reproduces the behavior is helpful, but in this case,
I suspect (just by intuition if nothing else) that the more you take
away, the harder it will be to reproduce. Have you turned up all
the warning levels as high as you can?

Do you check the return code from malloc() and friends everywhere
they are used? Do you check for NULL pointers before you follow
them? Etc.
Why would the code work on Mac OS X?


Undefined behavior means it could work fine on 10 other platforms
and still fail on your current one. It doesn't have to make sense,
unfortunately.

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Jul 23 '05 #8

"Michael McGarry" <mi*************@gmail.com> schreef in bericht
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi,

I have some code that never returns from a call to malloc(), does
anyone have any ideas? There is plenty of free memory on the system.
This is running in Linux kernel 2.4.20. The code has been run on
several different Linux machines. This same code runs without any
problems on Mac OS X.

Here is the GDB log:
(gdb) step
1160 hungarian_schedule =
malloc(SCHEDULER_NUM_SLOT*simParams.NUM_LAMBDAS*si zeof(int));
(gdb) print 180*simParams.NUM_LAMBDAS*sizeof(int)
$7 = 1440
(gdb) step


How big is
SCHEDULER_NUM_SLOT*simParams.NUM_LAMBDAS*sizeof(in t)
?
Jul 23 '05 #9
Thanks, all your comments are very helpful. I guess I need to track
down where my pointers are going beyond the memory allocated. Are there
any free tools that are good for aiding in this endeavor?

Thanks,

Michael

Jul 23 '05 #10

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

Similar topics

1
by: Michele Locati | last post by:
Hi to everybody I hope this is the right place to post this question. I have a problem I can't solve by myself. Maybe someone here can help me... I need to access the content of a status bar...
10
by: Michael McGarry | last post by:
Hi, I have some code that never returns from a call to malloc(), does anyone have any ideas? There is plenty of free memory on the system. This is running in Linux kernel 2.4.20. The code has...
82
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am...
6
by: Peter Michaux | last post by:
Suppose I have implemented a language with garbage collection in C. I have wrapped malloc in my own C function. If malloc returns NULL then I can run the garbage collector and then try malloc...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.