473,721 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation fault in mallopt/malloc call

Hello,

Maybe it's a little OT, but the fact is that I don't necessarly want to
know "how to correct?", but "why it happens?"

I have a program who "segment fault" (ok, that's "normal"... ;-) but
this time, it's not my code who "segment fault" but it's in the call of
malloc/mallopt

I've got:

with gcc 2.95.4
Program received signal SIGSEGV, Segmentation fault.
0x40085219 in malloc () from /lib/libc.so.6

with gcc 3.3.5
Program received signal SIGSEGV, Segmentation fault.
0x40094c97 in mallopt () from /lib/libc.so.6

any idea why ?

thanks by advance,

Alexandre, who is going to ask too in glibc and gcc newsgroups
Nov 14 '05 #1
7 3354
Alexandre wrote:
Hello,

Maybe it's a little OT, but the fact is that I don't necessarly want to
know "how to correct?", but "why it happens?"

I have a program who "segment fault" (ok, that's "normal"... ;-) but
this time, it's not my code who "segment fault" but it's in the call of
malloc/mallopt


This is almost 100% guaranteed to be the result of your program
corrupting your memory space. We can't guess where in your unshown code
this occurs.

Nov 14 '05 #2
Alexandre wrote:
Maybe it's a little OT, but the fact is that I don't necessarly want to
know "how to correct?", but "why it happens?"

I have a program who "segment fault" (ok, that's "normal"... ;-) but
this time, it's not my code who "segment fault" but it's in the call of
malloc/mallopt [...] any idea why ?


Your whole process has access to the very memory that malloc uses for
internal housekeeping. Typical reason for this behaviour is buffer
overflows or buffer underflows, or some other random data corruption. Try
using a memory debugger (e.g. electric fence, valgrind). It's probably not
a bug in malloc(), those routines are usually well-tested.

Uli

Nov 14 '05 #3


Alexandre wrote:
Hello,

Maybe it's a little OT, but the fact is that I don't necessarly want to
know "how to correct?", but "why it happens?"

I have a program who "segment fault" (ok, that's "normal"... ;-) but
this time, it's not my code who "segment fault" but it's in the call of
malloc/mallopt

I've got:

with gcc 2.95.4
Program received signal SIGSEGV, Segmentation fault.
0x40085219 in malloc () from /lib/libc.so.6

with gcc 3.3.5
Program received signal SIGSEGV, Segmentation fault.
0x40094c97 in mallopt () from /lib/libc.so.6

any idea why ?

thanks by advance,

Alexandre, who is going to ask too in glibc and gcc newsgroups


This is Question 7.19 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

The exact failure mechanisms vary from one malloc()
implementation to another, but two vulnerabilities are
quite common:

- Many malloc() implementations store some of their
bookkeeping information in a small amount of extra
space attached to each block of memory. If you
write outside the allocated area it is likely that
you will overwrite some of this "metadata," and the
next time malloc() or one of its companion functions
tries to use the scrambled data things will go awry.

- Many malloc() implementations try to keep the amount
of per-block information as small as possible, because
a program may create a very large number of blocks.
This means there is often not enough information stored
to allow malloc() and its friends to do much error
checking. `char *p = malloc(N); free(p); free(p);'
might very easily do something like introduce a loop
into what ought to be a straight-line linked list;
`char *p = "Hello"; char *q = realloc(p, 42);' could
scramble things quite thoroughly.

Some "debug malloc" packages try to guard against such
errors or at least make them easier to detect, but the cost
(in memory and in time) is often quite high, which is one
reason such programmer-friendly features are often absent
from "production " libraries.

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

Nov 14 '05 #4
Ulrich Eckhardt wrote:
Your whole process has access to the very memory that malloc uses for
internal housekeeping. Typical reason for this behaviour is buffer
overflows or buffer underflows, or some other random data corruption. Try
using a memory debugger (e.g. electric fence, valgrind). It's probably not
a bug in malloc(), those routines are usually well-tested.

danke für "electric fence"! das ist wunderbar! (ooops my german is a
little bit far... didn't speak it for ... zu viel zeit! (nearly 14 yrs))

ok, now I have a better idea for my problem! I will put the code in the
answear for Martin.

Alex, lothringisch im Paris.
Nov 14 '05 #5
Martin Ambuhl wrote:
This is almost 100% guaranteed to be the result of your program
corrupting your memory space. We can't guess where in your unshown code
this occurs.


thanks for your answear, but my aim was to know "how is it possible?",
and now with electric fence, I saw another place for my mistake and
modify my code and it works...
The code was too big to send...

Alexandre
Nov 14 '05 #6
On Fri, 18 Feb 2005 18:10:53 -0500, Eric Sosman wrote:
Some "debug malloc" packages try to guard against such
errors or at least make them easier to detect, but the cost
(in memory and in time) is often quite high, which is one
reason such programmer-friendly features are often absent
from "production " libraries.


so where is the problem that you see and I not?
Can you please show me some code *where* malloc_debug() or malloc_m()
has some problems?
The only problem that I see for these functions is a "degenerate case"
like:

while(1)
{unsigned *h;
h=malloc_m(9 * sizeof *h);
if(fun(h)==1) break;
free_m(h); h=0;
verifica_all_m( );
}
free_m(h);

Nov 14 '05 #7
On Fri, 18 Feb 2005 -0500, Eric Sosman <er*********@su n.com> wrote:
The way seems very well suited for numeric functions that write
arrays. If a function write a char out the array the program detect
(99%) this and point where there is the error. Today it has found an
error of that kind.
For generic function that write arrays it could not work well because
it is possible to write out of bounds but not in the boundary of
array.I'm in the kill filter?
Nov 14 '05 #8

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

Similar topics

9
29700
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the function with this statement several times and seems that it has successfully allocated the memory. and then at some iteration, it just gets this segmentation fault. The gdb gives the following message:
16
8985
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those who don't speak french arbre means tree.
5
1899
by: Ravikant | last post by:
Hello, Please check the following code .Let me know why its giving the segmentation fault while running it.First it asks to enter the name ,when entered it gives segmentation fault. But if I dont use the insert() and written everything in main(),it works fine.Please help me out. #include<stdio.h> typedef char* Name; typedef struct node
3
11436
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
6
4777
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation fault. If I just call realloc( ) once before calling malloc( ), it is OK. Why? I am trying to read some double-typed items from infile and save them
27
3357
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
59
2889
by: Christian Christmann | last post by:
Hi, I'm wondering why this program does not crash with a segmentation fault: #include <malloc.h> #include <string.h> #include <stdio.h> int main()
10
5742
by: Linny | last post by:
Hi All, I am pasting a piece of code which executes fine on 32 bit system but fails with a segmentation fault when compiled 64 bit compiler.I am using a HP-UX C compiler on PA-RISC system. This code was picked up from a document mentioning portability issues from 32 to 64 bit systems. But when I include the system file <malloc.h, the code executes fine on both the systems.
7
328
by: aarklon | last post by:
char *f() { char *s = malloc(8); strcpy(s,"good bye"); } int main(void) { printf("\n %s",*f()='A');
0
9212
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
9127
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
9056
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...
0
8002
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
6664
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
5973
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
4483
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...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2127
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.