SIGSEGV when allocate a certain size


 
Thread Tools Search this Thread
Top Forums Programming SIGSEGV when allocate a certain size
# 1  
Old 12-05-2011
SIGSEGV when allocate a certain size

The problem: I need to work with large arrays and after one of my structures grew in size my program started getting segmentation fault.

My code where I allocate the arrays:

Code:
static  R1         *tarr;
static  R2         *rarr;
proc_init_mem()
{
  const int     t_sz = sizeof(R1) * MAX_R1;
  const int     r_sz = sizeof(R2) * MAX_R2;
        tarr = malloc(t_sz);
        rarr = malloc(r_sz);
        if(tarr == NULL || rarr == NULL)
                return(-1);
        printf("tarr sz: %i\n", trp_sz);
        printf("rarr sz: %i\n", run_sz);
        return(0);
}

When I run the program, I am getting the printouts:

Code:
tarr sz: 11280000
rarr sz: 20200000

and then program dies according to debugger somewhere in fgetc (libc) as it reads in config params. If I decrease MAX_R1 or MAX_R2 everything is fine.

I am not exactly clear which resource limitations I am breaking.
This is Ubuntu 8 with gcc 4.2.4, the program is mostly in C, with addition of C++ libraries.

The rlimit parameters are as follows:
Code:
 0 -               -1               -1 per proc. CPU limit
 1 -         16777216         16777216 largest file created
 2 -               -1               -1 max sz of data segment
 3 -         33546240         33546240 max size of stack seg
 4 -               -1               -1 largest core sz
 5 -               -1               -1 largest resident set sz (swapping related)
 6 -             8191             8191 number of processes
 7 -             1024             1024 number of open files
 8 -            32768            32768 locked-in mem addr space
 9 -               -1               -1 addr space linit
10 -               -1               -1 max file locks
11 -             8191             8191 max number of pending signals
12 -           819200           819200 max bytes per msg queue
13 -                0                0 nice priority
14 -                0                0 max realtime priority

(I just run getrlimit in loop 0 through 14 to produce this list and manually added annotations)

I tried to run ulimit -s 32760 and then execute my program, but it did not help. BTW, the size 32760 was the bigest I was able to ulimit. Also, I tried to run as root, hoping that root would not have the limitation, but the same SIGSEGV happened.

Does anyone know how to deal with this type of problem? Any insight will be appreciated.
# 2  
Old 12-05-2011
Is your program 32-bit or 64-bit? You are approaching 32-bit address space limitations.
# 3  
Old 12-06-2011
It is 32 bit - I base this on the fact that sizeof(int) is 4.

When you say I approach 32 bit space limit, how is so? the sizes of those arrays printed total in 31,480,000 that would be roughly 32MB. am I wrong?
# 4  
Old 12-06-2011
Quote:
Originally Posted by migurus
It is 32 bit - I base this on the fact that sizeof(int) is 4.
sizeof(int) is often 4 in a 64-bit system, too. sizeof(long) may be a better indicator, but not always.
Quote:
When you say I approach 32 bit space limit, how is so? the sizes of those arrays printed total in 31,480,000 that would be roughly 32MB. am I wrong?
You are correct, I misread.

There doesn't appear to be anything wrong with your memory allocation, then. There must be a bug somewhere else in your code.
# 5  
Old 12-06-2011
For now I declared one of the arrays as statically as.
Code:
static R1 rarr[MAX_R1]

and keep the second array allocated dynamically by malloc, the program works.

Any suggestions on how to find the root cause would be much appreciated.
# 6  
Old 12-06-2011
I can't see your computer from here. Please post your code.

Almost certainly it's overrunning the end of the array or otherwise corrupting memory in ways that don't always crash the program.
# 7  
Old 12-06-2011
malloc implementations often use memory descriptors (the first long word of a malloc-ed space in heap as the length of the space allocated, and the starting address). When you write past the end of the preceding object in memory, you obliterate those memory descriptors, then the result becomes undefined - usually a crash or badly trashed data.
This User Gave Thanks to jim mcnamara For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to create SIGSEGV at particular memory?

Hi I want to create a SIGSEGV when program tries to access one particular area in memory. Is this possible. If so whats the command. (4 Replies)
Discussion started by: jionnet
4 Replies

2. Shell Programming and Scripting

Error PHP Fatal error: Allowed memory size of 67108864 bytes exhausted(tried to allocate 401 bytes)

While running script I am getting an error like Few lines in data are not being processed. After googling it I came to know that adding such line would give some memory to it ini_set("memory_limit","64M"); my input file size is 1 GB. Is that memory limit is based on RAM we have on... (1 Reply)
Discussion started by: elamurugu
1 Replies

3. Solaris

sigsegv Fault

I receive a sigsegv failure. I was under the impression that a core file is created everytime a sigsegv occurrs and the process is terminated. I have had two recent occurrances of a core file not being created. Does anyone know why a core file would not be created. (2 Replies)
Discussion started by: amp4cats
2 Replies

4. AIX

Received signal #11, SIGSEGV [default] on AIX 6.1

Hello, One of our customer is getting segmentation fault when he runs his shell script which invokes our executable on AIX 6.1. On AIX 5.3, there were no issues. Here is the truss output. 811242: __loadx(0x0A040000, 0xF0D3A26C, 0x00000000, 0x00000009, 0x00000000) = 0xF026E884... (0 Replies)
Discussion started by: erra_krishna
0 Replies

5. UNIX and Linux Applications

SIGSEGV Signal handling

Hello, Can anybody tell me how can i handle segmentation fault signal, in C code? (2 Replies)
Discussion started by: mustus
2 Replies

6. Programming

problem in SIGSEGV signal handling

i wrote handler for sigsegv such that i can allocate memory for a variable to which sigsegv generated for illlegal acces of memory. my code is #include <signal.h> #include<stdio.h> #include<stdlib.h> #include<string.h> char *j; void segv_handler(int dummy) { j=(char *)malloc(10); ... (4 Replies)
Discussion started by: pavan6754
4 Replies

7. Programming

SigSegV during stack unwind (AIX)

Hi I am getting a strange segmentation fault during the unwind process. I am trying to throw an object of an Exception class. During the DoThrow (in libC.a) the stack starts growing until a Signal is received. The object thrown is of a class that inherits from a common class, and the signal... (0 Replies)
Discussion started by: barak
0 Replies

8. Solaris

How to correctly allocate size while creating FileSystem

Hello - I am finding difficulty in creating and allocating correct size to File Systems on solarix x86 box. Please see below contents I followed on screen and in the end It shows that /app file system is created of size 135GB , I wanted it to be 30gb as mentioned during 'format' command in 'Enter... (7 Replies)
Discussion started by: panchpan
7 Replies

9. Programming

SIGSEGV, Segmentation fault

Here is my initiating code: #define NUM 20 static struct tab { int count; int use; } tab; int curtab = 0; int tab_create(int tab_count) { curtab++; tab.use = 1; tab.count = tab_count; kprintf("here!"); return curtab; } (2 Replies)
Discussion started by: micmac700
2 Replies

10. Programming

SIGSEGV problem

Hi. Can someone to help me in a segfault problem? I have a big C++ program that crash from time to time because it receive the SIGSEGV signal. So my question is: Can I find, without using gdb or other debugging tools, which line from source code cause that problem? Or if exist some gdb API... (1 Reply)
Discussion started by: vaidac_coder
1 Replies
Login or Register to Ask a Question