473,606 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc error

Under what condition will we see a segmentation fault in the malloc
library routine? Specifically, a failure in call to "chunk_allo c()"
from within malloc.

I have a multi-threaded c++ program that occasionally core-dumps in
"malloc" when running on Linux 2.4 (it always works fine on Solaris
5). The stack trace shows the final call to "chunk_allo c".

I would like to re-create this bug within a debugger. However, the
segmentation fault happens so infrequently that I think I need to have
a better idea of how a problem like this can arise before I can
re-create it.

Many thanks in advance for any ideas.
Jul 22 '05 #1
4 1644
afarah wrote:
Under what condition will we see a segmentation fault in the malloc
library routine? Specifically, a failure in call to "chunk_allo c()"
from within malloc.

I have a multi-threaded c++ program that occasionally core-dumps in
"malloc" when running on Linux 2.4 (it always works fine on Solaris
5). The stack trace shows the final call to "chunk_allo c".

I would like to re-create this bug within a debugger. However, the
segmentation fault happens so infrequently that I think I need to have
a better idea of how a problem like this can arise before I can
re-create it.

Many thanks in advance for any ideas.


Multi-threading is OT here simply because C++ program model is a single
process on a single CPU with all parts of the program executing in
a sequence. Intrinsics of C library are too, because they are very
compiler-specific. Differences between OSes are also beyond the scope
of this newsgroup. Perhaps you will find better help in a forum for
your compiler or OS. Try comp.os.linux.d evelopment.apps or gnu.g++.help.

In general a library routine fails when it gets invalid input, and there
is no invalid input for malloc, IIRC. Any number you pass in is going to
be interpreted as an unsigned integral value, and the system will either
allocate the requested amount, in which case a pointer to it is returned,
or fail to do so, in which case it will return a null pointer.

In most cases unexpected segmentation faults are due to memory corruption
that happened some time before the code that breaks is executed. You may
need some memory checking tools (like Purify) to see what's going wrong
with the program's memory. Of course, program's being multithreaded
doesn't really help...

Good luck!

Victor
Jul 22 '05 #2
afarah wrote:
Under what condition will we see a segmentation fault in the malloc
library routine? Specifically, a failure in call to "chunk_allo c()"
from within malloc. If there is a bug in the malloc library.

More common though, you have a nasty bug which does something very
bad, probably corrupting the heap,stack or otherwise invoke undefined
behavior.
I have a multi-threaded c++ program that occasionally core-dumps in
"malloc" when running on Linux 2.4 (it always works fine on Solaris
5). The stack trace shows the final call to "chunk_allo c".

I would like to re-create this bug within a debugger. However, the
segmentation fault happens so infrequently that I think I need to have
a better idea of how a problem like this can arise before I can
re-create it.

Get a hold of valgrind, and run your program in its memory checker tool.
Jul 22 '05 #3
afarah wrote:
Under what condition will we see a segmentation fault in the malloc
library routine? Specifically, a failure in call to "chunk_allo c()"
from within malloc.

To amplify on what Victor said. The most common problem with malloc()
and free() exploding is that on a previous allocation you did something
bad.

The most common ones are:

1. Freeing something not malloced, or freeing the same value twice.
2. (I vote for this one) Writing out of the bounds of a previous malloc.
Common malloc implementations put control information for the space
management intermixed with the allocations. Write a few bytes outside
of a malloc'd region is almost sure to corrupt malloc's internal data.
Jul 22 '05 #4
afarah wrote:
Under what condition will we see a segmentation fault in the malloc
library routine? Specifically, a failure in call to "chunk_allo c()"
from within malloc.

I have a multi-threaded c++ program that occasionally core-dumps in
"malloc" when running on Linux 2.4 (it always works fine on Solaris
5). The stack trace shows the final call to "chunk_allo c".

I would like to re-create this bug within a debugger. However, the
segmentation fault happens so infrequently that I think I need to have
a better idea of how a problem like this can arise before I can
re-create it.

Many thanks in advance for any ideas.


It's an app bug.

Try running the app under valgrind with and set the environment variable
GLIBCXX_FORCE_N EW (or GLIBCPP_FORCE_N EW depending on version of GCC).

Or try runnung under efence.
Jul 22 '05 #5

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

Similar topics

34
6420
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have started messing around in Plan 9, and that sort of thing is totally no go there :). Is this correct to allocate memory for my struct? It works on my computer, but I'm suspicious that I'm doing it wrong. --
231
23070
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
25
5060
by: H.A. Sujith | last post by:
If malloc fails what should I do? 1. Exit imediately. 2. Print an error message (or put a log entry) and exit. 3. Print an error message (or put a log entry) and continue execution (after possibly recovering from the error). Printing an error message might be difficult in a graphical environment. --
27
4251
by: Chess Saurus | last post by:
I'm getting a little bit tired of writing if (a = malloc(...) == NULL) { // error code } I mean, is it really possible that a malloc call could fail, except in the case of running out of virtual memory? -Chess
35
2665
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
1
2407
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can someone take a look and tell me how to fix it - please dont be like the guy I spoke to today and tell me that I am not allocating the memory correctly and then walk off. Please, if possible provide me with
19
2458
by: ceo | last post by:
hi, why do i get the following error? do i need to explicitly typecast the pointer returned by malloc to char *? thanks, ceo # cat malloc.cpp #include <stdio.h>
20
10738
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
68
15665
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
25
2237
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking. --- #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct {
0
8015
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
8430
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
8094
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
6770
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5966
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
5465
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
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
0
1296
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.