Segmentation faults with huge loops? (C++)

  • C/C++
  • Thread starter Simfish
  • Start date
  • Tags
    Loops
In summary, the conversation is about writing a program for research on spike triggered averages in neurons. The program involves a lot of data analysis and has encountered errors when the size of the data is increased. Possible solutions discussed include using scientific notation for the size, using a debugger to identify the source of the errors, and avoiding declaring large arrays as automatic local variables.
  • #1
Simfish
Gold Member
823
2
So I'm writing a program for a professor (not for class) for research related with spike triggered averages in neurons. Which involves a lot of data analysis.

So basically my code looks like this:
for(int i=0; i<SIZE; i++){
Write a lot of things into arrays in memory (although far fewer items than of size SIZE).
}

So when my SIZE is 1000000/4, the program runs fine, but when the SIZE is 1000000, the program runs into all sorts of weird errors. It crashes on the windows cmd line, while ./a.out on the Linux command line will produce the "segmentation fault" error. What's the best way to get around this?

Thanks!
 
Last edited:
Technology news on Phys.org
  • #2
Hi there,

Don't remember much about c++, but have you tried writing your SIZE in scientific form? Maybe the compiler would prefer that.

Otherwise, it does seem terribly peculiar, specially that integers can be more or less 2 billion.

If you really don't have any other solution, you can nest this for loop inside another, just to start it again and again. I don't believe that it's the best solution, but if it works.

Cheers
 
  • #3
Are you declaring the arrays statically (as variables in functions) or dynamically (by declaring only a pointer and then using new() to allocate the memory)?

Static variables are allocated on the program's "stack" which is often limited in size by the operating system. Dynamic variables are allocated in the "heap" which is usually much larger.
 
  • #4
Segmentation fault usually means a bad pointer:

http://en.wikipedia.org/wiki/Segmentation_fault

Can you use a debugger to see what is going on when it fails? Microsoft Visual Studio Express C++ is a free compiler and debugger you can use to test your program with, if you use Windows.

On Intel environments, static variables are pre-allocated in the initialized portion of the data segment, not on the stack. Non-static global variables are pre-allocated in the uninitialized portion of the data segment. Only local variables and alloca() or _alloca() use the stack.
 
Last edited:
  • #5
OK, "local variables" was what I meant, not "static." I've been away from extensive reading about C++ for long enough that my terminology has gotten rusty. :blushing:
 
  • #6
Jeff Reid said:
Only local variables and alloc() use the stack.
Are you sure are about alloc()? That usually takes data from the heap, and the heap tends to be considerably larger than the stack.

Code:
#define SIZE 1000000
dont_do_this (void) {
   double big_array[SIZE];
   ...
}
That big_array is going to be allocated from the stack on many systems. That big size will cause the stack to overflow. That array is not as big as you ask for, and the failure to size the array properly is often undetected -- until you try to write on protected memory, for example.

If on the other hand you did something like this,
Code:
#define SIZE 1000000
dont_do_this_either (void) {
   double kaboom[250000];
   for (int i=0; i<SIZE; ++i) { kaboom[i] = i; }
   ...
}
The array might well be as big as you asked, but you are running past the end.How to avoid the problem?
1. Do not declare large arrays (more than a 1000 elements or so) as automatic local variables. Alternatives are
a. Allocate them dynamically (e.g., new in C++, malloc() in C)
b. Declare them with some other scope. File-scope or static local, for example.

2. No matter how you construct your arrays, never, ever read or write outside their bounds.
 
  • #7
D H said:
Are you sure are about alloc()?
I was thinking of the function alloca() or _alloca():

http://msdn.microsoft.com/en-us/library/wb1s57t5(VS.71).aspx

I corrected my previous post. I don't know if alloc() ever existed. There some chance it existed in an old Atari ST Alcyon C compiler I used back in 1986, but I never used alloc() or _alloca(), so if I'm remembering it, it's because I saw it in some reference manual or sample code. Some compilers used an alloc.h, so that may be what I'm remembering.

msdn defines an Alloc() function, but it's noted as preview, and I don't know how it's different from malloc:

http://msdn.microsoft.com/en-us/library/dd492420(VS.100).aspx
 
Last edited:

1. What is a segmentation fault in C++?

A segmentation fault is an error that occurs when a program tries to access a memory address that is not allowed or does not exist. This can happen when the program tries to access memory that it does not have permission to use, or when it tries to access memory that has not been allocated.

2. Why do segmentation faults occur in huge loops?

Segmentation faults can occur in huge loops because the program may be trying to access memory that is outside the bounds of the loop. This can happen if the loop runs for too long or if the program is trying to access memory that has already been freed or deleted.

3. How can I debug a segmentation fault in my code?

Debugging a segmentation fault can be a challenging task, but there are a few steps you can take to help identify the issue. First, make sure your code is free of syntax errors and that all variables are properly initialized. You can also use a debugger tool to step through your code and see where the segmentation fault is occurring. Additionally, you can try adding print statements or logging to track the flow of your program and identify any potential issues.

4. Can I prevent segmentation faults in my code?

While it is not always possible to prevent segmentation faults, there are some steps you can take to reduce the likelihood of them occurring. These include properly allocating and freeing memory, avoiding accessing memory outside the bounds of an array or loop, and carefully checking pointer references.

5. Are segmentation faults only specific to C++?

No, segmentation faults can occur in other programming languages as well. They are a result of accessing memory that is not allowed or does not exist, so any language that allows for direct memory manipulation is susceptible to segmentation faults.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
7
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
944
Back
Top