LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-18-2008, 04:53 PM   #1
felipebil
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Rep: Reputation: 0
Question Segmentation Fault - memory problem?


Im with a problem to run a C program, maybe you help me to find the solution.
Its a simple code, but uses a lot of memory. One matrix for example have 10 million elements. The program runs normally when i reduce the size of some of that matrices, but in the wanted size doesnt work. As it run sometimes, i think the problem is memory.
I have used:

% ulimit -s unlimited

and it appears to help a little, but didnt solve the problem. Had also tried to use malloc and free to free some memory, but had no effect. I know that much bigger programs run in this computer, so i really need help because i dont know what to do.

below the variables declaration:
Code:
#define T_MIN 5000
#define T_MAX 15000
#define E_MIN 0.0
#define E_MAX 0.1
#define E_NUM 100
#define N_MAPS 1000

int main(void){

int tempo, map, e, i, start, end;
int y_maxtab[N_MAPS][T_MAX/100], k[N_MAPS], n_ymax[N_MAPS];
double eps, coup, step = (double)(E_MAX-E_MIN)/(double)(E_NUM-1);
double mean, new_x;
double alfa[N_MAPS], x0[N_MAPS], y0[N_MAPS];
double x_dat[N_MAPS], y_dat[N_MAPS][T_MAX];
double rnmed_tab[2][E_NUM], rn_tab[T_MAX];
double mcos, msin, fi,deno;
the y_dat matrix is the biggest here.

p.s.: normally i use icc to compile my programs, but in gcc i get the same errors. i had also tried to use gdb, but didnt discover nothing.
 
Old 11-18-2008, 05:03 PM   #2
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,245

Rep: Reputation: 121Reputation: 121
Is your RAM + swap space large enough for the total memory footprint of the program?
 
Old 11-18-2008, 05:29 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
IME segment violation is usually caused by writing off the end of an array. Are you absolutely sure that isn't the issue here, especially as you claim you have run bigger programs than this (I assume you mean progs with bigger array declarations)
 
Old 11-18-2008, 06:05 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I believe you're probably encountering stack overflow (in other words, I believe the "segmentation violation" is actually "stack too small", and I believe that "ulimit -s unlimited" is in fact *very* limited...)

Here are a couple of links that might help:
http://cs.nyu.edu/exact/core/doc/stackOverflow.txt
http://ubuntuforums.org/archive/index.php/t-114071.html
http://linux.about.com/library/cmd/b..._setrlimit.htm
 
Old 11-18-2008, 06:11 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Check these links http://www.linuxquestions.org/questi...-fault-672145/ or http://www.linuxquestions.org/questi...ze-in-c-673742, they may assist.
 
Old 11-18-2008, 10:39 PM   #6
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
I believe paulsm4 is right. When you statically allocate memory for an array (as you are doing), it gets "allocated" (per se) onto the stack. What you are doing isn't really a very good thing todo. Try dynamically allocating your memory via new, delete or malloc, free. Either way, make sure the memory allocated actually gets allocated, for example, DON'T do this:

Code:
int *ptr = malloc(huge_amt);
/* Bad! */
p[0] = 1;
This is what you should do.

Code:
int *ptr = malloc(huge_amt);
assert(ptr == NULL); //check for error
p[0] = 1;
I would like to add as a final note, that if you allocate outside your RAM, you will start using swap space, which is very slow. If you allocate outside of both, all bets are off (though malloc should fail).
 
Old 11-19-2008, 12:16 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by paulsm4 View Post
I believe you're probably encountering stack overflow (in other words, I believe the "segmentation violation" is actually "stack too small", and I believe that "ulimit -s unlimited" is in fact *very* limited...)
Yes, I was about to say that.

This looks like a major abuse of the stack. You should either make all of the arrays static or malloc them. How long is the program? Have you tried gdbing it?
ta0kira
 
Old 11-19-2008, 12:32 AM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by The_Nerd View Post
I believe paulsm4 is right. When you statically allocate memory for an array (as you are doing), it gets "allocated" (per se) onto the stack. What you are doing isn't really a very good thing todo.
Careful with how you use the word static! You're going to confuse people. Though semantically accurate, "statically allocate" sounds suspiciously like "static data". I would have said "the allocation is static" because "statically allocated" implies allocation in the static data area.
ta0kira
 
Old 11-19-2008, 08:53 PM   #9
felipebil
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Well, at least i'm understanding what is the problem.
But I'm a little bit confuse with the many kinds of memory.
ram, stack, heap, swap, buffer...

in my case, i think that occurred a stack overflow, despite a have no idea of why it happened. I make a simple calculation, and estimate the footprint memory of the program in no more than 200MB.
And as you can see below, the result of the free -lm (my job wasnt running at this time):

Code:
   
               total       used       free     shared    buffers     cached
Mem:          2028MB      600MB     1427MB        0MB        0MB       75MB
Low:           876MB        7MB      869MB
High:         1151MB      592MB      558MB
-/+ buffers/cache:        525MB     1503MB
Swap:            0MB        0MB        0MB
so how can i know the limit of the stack memory when i define "ulimit -s unlimited"? and what is the relation of this with the ram? if i use malloc to allocate memory for my matrices anything in the arrange of the memory changes?

thanks a lot

Last edited by felipebil; 11-19-2008 at 08:54 PM.
 
Old 11-19-2008, 09:05 PM   #10
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Quote:
Originally Posted by felipebil View Post
Well, at least i'm understanding what is the problem.
But I'm a little bit confuse with the many kinds of memory.
ram, stack, heap, swap, buffer...

in my case, i think that occurred a stack overflow, despite a have no idea of why it happened. I make a simple calculation, and estimate the footprint memory of the program in no more than 200MB.
And as you can see below, the result of the free -lm (my job wasnt running at this time):

Code:
   
               total       used       free     shared    buffers     cached
Mem:          2028MB      600MB     1427MB        0MB        0MB       75MB
Low:           876MB        7MB      869MB
High:         1151MB      592MB      558MB
-/+ buffers/cache:        525MB     1503MB
Swap:            0MB        0MB        0MB
so how can i know the limit of the stack memory when i define "ulimit -s unlimited"? and what is the relation of this with the ram? if i use malloc to allocate memory for my matrices anything in the arrange of the memory changes?

thanks a lot
It is simple, when you write a program and compile it, the compiler allocates a heap of memory, a memory block if you will, this is where all the program variables, statically allocated arrays, etc... are put. This is also used as a memory space for variable creation, function calling, etc...

When you dynamically allocate memory via a function called at runtime, the memory is "given to you" from where-ever the OS can find it. Usually it will look fist for space in the RAM, if ALL of RAM is full, the OS will start using a space on the hard-drive called "swap space". This slows down memory operations considerably as the hard-drive is WAY slower than RAM.

So to sum it up:
Code:
/* Statically allocated (basically, this means the compiler allocates space for it on the stack) */
int MyStaticArray[100];
/* Dynamic memory allocation */
int *p = malloc(100 * sizeof(int));
/* Now RAM is full (hypothetically), so now the OS will give you space on the hard-drive. This is low level, so to you it works the same as memory in RAM... it is just way slower. */
int *hard_drive_alloc = malloc(100 * sizeof(int));
/*... error testing...*/
free(p);
free(hard_drive_alloc);
Hope that clears things up for you.
 
Old 11-19-2008, 10:01 PM   #11
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by felipebil View Post
in my case, i think that occurred a stack overflow, despite a have no idea of why it happened. I make a simple calculation, and estimate the footprint memory of the program in no more than 200MB.
There was another thread about same problem. The data allocated on stack will cause stack overflow, if data size is above 8megabytes. Not sure how ulimit affects that.
Solutions:
1) Make large variables (defined within functions) static.
or
2) Make large variables global. I.e. don't put declaration of anything huge within function call, unless it is static.
or
3) Allocate arrays using malloc.

Last edited by ErV; 11-19-2008 at 10:03 PM.
 
  


Reply

Tags
memory, segmentation fault, ulimit


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault -- permission problem? dx0r515t Slackware 13 08-24-2006 02:55 PM
Problem with Segmentation Fault in Linux akzare Programming 2 08-16-2006 11:42 PM
Segmentation Fault, Should I try new memory M$ISBS Linux - Hardware 10 07-04-2006 04:49 PM
problem with segmentation fault lucs Slackware 2 04-28-2005 09:14 AM
Segmentation Fault Problem luvonmik Linux - Newbie 2 02-14-2004 07:44 PM


All times are GMT -5. The time now is 07:17 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration