ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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)
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...)
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).
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
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
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):
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?
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):
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);
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.