-
June 28th, 2010, 12:08 PM
#1
seg fault with mere creation of 2 3d arrays in a single object
hello all,
so i need to have a class that has two 3-dimensional arrays, i.e.
class values {
public:
long totals[8][8][8];
int averages[8][8][8];
int data_points;
}
however, when i try to create an instance of this class, i immediately get a seg-fault; if i remove one of the 3-dimensional arrays, it does not seg fault, and after repeated trial and error i have determined that if i were to resize one of the arrays to 3x3x3, it does not segfault. does anyone have an idea why this is happening and how to fix it?? thanks!
-
June 28th, 2010, 12:13 PM
#2
Re: seg fault with mere creation of 2 3d arrays in a single object
Impossible to say without more information. But here are three possibilities based on screw-ups I've seen before with some regularity:
1) You are trying to cast these arrays to long***s somewhere. Despite the fact that a T[8] is convertible to a T*, the same does not hold true for multidimensional arrays.
2) You are trying to create many of these objects and running out of stack space. You only have a couple MB on the stack, usually. (Creating one of these objects should be fine on its own.)
3) You need to clean/rebuild your project, because some translation unit is confused about how large the values class is.
-
June 28th, 2010, 12:15 PM
#3
Re: seg fault with mere creation of 2 3d arrays in a single object
Post an example please. How do you create the class in question? How do you use it? Post an example that we can compile that demonstrates the problem. The arrays didn't seem that big to me but sometimes creating large arrays on the stack causes some run time problems. This might be one of those situations where you have to create the object in heap memory by using operator new.
-
June 28th, 2010, 12:18 PM
#4
Re: seg fault with mere creation of 2 3d arrays in a single object
yes, it looks like i may be running out of stack space, as i am trying to make multiple objects like this, and if i lower the dimensions (i.e. using less) it does begin to work. how can i get more memory on the stack?
thanks!
-
June 28th, 2010, 12:20 PM
#5
Re: seg fault with mere creation of 2 3d arrays in a single object
Trying to get more memory on the stack is usually the wrong approach. Why don't you show us what you're doing so we can make targeted suggestions. It may be as simple as replacing this:
with
Code:
vector<values> v(5000);
-
June 28th, 2010, 12:25 PM
#6
Re: seg fault with mere creation of 2 3d arrays in a single object
or deque<values> d(5000), or list<values> L(5000) depending on what you need to do with the array or whether it will grow at run-time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|