Reputation: 123
I have a very large C program for Molecular dynamics. It works correctly with N=10000 particles. When I increase the particles to N=100000 by increasing the variable size, it generated a segmentation fault.
I executed ulimit -s unlimited
and problem was resolved.
Q1. Is any way to check how much stack memory my code is going to use, performance check of the code(optimization), memory leakage.
Q2. If the structure of my code is like, nested functions,
int main() {
function1();
return 0;
}
void function1() {
for(int i=0;i<1000;i++) {
function2();
}
}
void function2() {
double Var[100000];
}
Will the use of function2()
inside a for loop use more stack memory than executing it only one time?
Upvotes: 1
Views: 2587
Reputation: 5635
It is best practice not to create large objects on the stack. They should instead be allocated dynamically with malloc
in C or new
in C++.
Your program can then recover if the allocation request is refused by the program.
By using the new smart pointers in C++ you can help ensure that all allocations get freed and that no leaks occur, otherwise you just have to "write your code carefully", which is the downside of C programming.
Upvotes: 0
Reputation: 31449
Q1. I want to know if there is any way to check how much stack memory my code is going to use, performance check of the code(optimization), memory leakage.
Not really. The C standard does not even mention the stack, and any C compiler can create binaries that does not use the stack and still conform to the C standard.
However, in reality a stack is almost always used and the overhead is very small. Just sum up all the local variables and you will get a good estimate.
Performance should not be predicted. It should be measured, and then you optimize if necessary.
It's very hard, if not impossible, for a compiler to detect memory leaks in a reliable way. You can use programs like Valgrind for that.
Q2. if the structure of my code is like,nested functions, because of the function2 call inside a for loop is it going to use more stack memory than using once only?
No. Each time function2()
is called, a new stackframe is created with enough space for 100000 doubles. But it will be released immediately when the function returns. The problem here is not calling a function in a loop. The problem is that you're allocating HUGE arrays on the stack, which might become a problem. You should consider allocating them dynamically instead. Basically, it will look like this:
void function2
{
double *var = malloc(100000*sizeof(*var));
/* Code */
free(var);
}
If you are using recursive functions, the stack might become a problem. Let's consider this sum function that sums all natural numbers up to num:
unsigned long long sum(unsigned long long num)
{
if(num == 0) return num;
return num + sum(n-1);
}
A long long
is typically 8 bytes, so if you use this function for very large num (maybe 100000 or 1000000) you might encounter stack problems.
Upvotes: 4
Reputation: 181824
Q1. I want to know if there is any way to check how much stack memory my code is going to use, performance check of the code(optimization), memory leakage.
You may be able to estimate how much stack space your program will require by analyzing the call graph and the variables declared in each function, but this is not easy for a complex program. It is much more difficult if any recursion is involved, but one should at least be able to place a rough upper bound. But the easy way is to measure the usage when running on characteristic inputs.
Absolute performance is extremely hard for humans to predict. The best we can usually do reliably is to characterize how performance will scale with problem size. Comparative performance should always be measured.
As for memory leakage, careful code analysis by humans can usually detect memory leaks, but it is wise to complement that with usage of a runtime memory-usage analysis tool such as Valgrind. Such tools turn up leaks even in well-analyzed code with distressing frequency.
Q2. if the structure of my code is like,nested functions, [...] because of the function2 call inside a for loop is it going to use more stack memory than using once only?
No. A function's automatically-allocated resources are released when control leaves that function. Calling the same function multiple times, whether in a loop or otherwise, does not use more automatic (stack) resources than the most demanding individual call does.
However, when a function recurses by directly or indirectly calling itself, it probably does use additional resources proportional to the recursion depth. I say "probably" because in some cases the compiler may be able to convert the recursion into an iteration within a single function call.
Upvotes: 2