Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Segmentation fault error. ?Maximum memory avalaible for each C program?
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Roberto




PostPosted: Fri Oct 16, 2009 7:59 am   Post subject: Segmentation fault error. ?Maximum memory avalaible for each C program?

Hi everybody, hope you can help me,

I am writing a code that needs to declare a large array of doubles of N x N x 256 x 128, where N can be as big as 65.

The compilation gave no error. The allocation is static with 'double array[N][N][256][128];'
Dynamic allocation will not give me any advantage since I know the amount of memory I will use, I mean, if constant N=9 I will use by sure 9x9x256x128 matrixs.
Variable array will store NxN images of size 256x128 pixels.

Well, if N=3 I can execute the program with no problems but, if N>=4 I get "Segmentation fault". My laptop is a 2GB RAM with a free memory of 0.5GB checked with 'top' Linux command. A matrix of 4x4x256x128 of doubles would have a size of 4.2MB so I do not understand where is the problem. It can not be the RAM memory required.
I commented all the lines in the program that make use of the array variable in order to detect a bad index but the Segmentation Fault error still remain with only the declaration of the array in the code.

I also have executed the program in a machine of 8GB RAM and the program crashes with N>=5. Of course, changing the type of the variable to float, or even to int, increases the value of N with whichl I can declare the array but this is not the idea and, in any case, I still have a limit.

In order to check a possible bug I have written the following:

#define NUMBER xx

main()
{
double array[NUMBER];
printf("\nn?mero = %d",NUMBER);
}

I get a Segmentation fault if NUMBER=2000000 but not in case of 1000000. This tells me there is no problem trying to access to a wrong memory position with a bad index since in this little program I am not using the matrix.

I wish anybody could explain me where is the problem, is there any limitation to the memory allowed for each program? Must I use dynamic allocation even though I will not save space respect to static since I know the memory size I need? How can I solve this bug?

Thanks in advance
R.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Fri Oct 16, 2009 8:56 am   Post subject: RE:Segmentation fault error. ?Maximum memory avalaible for each C program?

You're probably requesting too much memory with a single call, and the value of your array-pointer is probably NULL (even if you didn't explicitly declare it as a pointer, it will be handled as such in the background). Check the values of (size_t)-1 and SIZE_MAX, as suggested by this thread.

If you're allocating more than that, the allocation will fail and you may have to do many separate, smaller allocations. You could, for example, have a 2D array of size NxN of pointers, each of which points to a 2D array of size 256x128 (which you could then allocate separately).
OneOffDriveByPoster




PostPosted: Fri Oct 16, 2009 5:06 pm   Post subject: Re: Segmentation fault error. ?Maximum memory avalaible for each C program?

A cut-down version of your code showing the issue would help.
If you go up to a large number, it is no surprise you will get a failure.
btiffin




PostPosted: Sat Oct 17, 2009 12:12 am   Post subject: RE:Segmentation fault error. ?Maximum memory avalaible for each C program?

Check the tech specs for your compiler to see if you can adjust the stack size. I'd hazard a guess that you get 8Mb by default. As DemonWasp suggested, you may need to allocate off the heap.

Cheers
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: