0

I'm declaring a global variable in my program:

double mat[3200][3200];

when passing this array to fwrite() by using the variable's name as argument, I get stack overflow thrown at runtime.

fwrite(mat, sizeof(double), 3200*3200, fid);

Why is that? Shouldn't the compiler replace the variable mat with a pointer to its first element?

I've read here and here, and still can't understand.

Community
  • 1
  • 1
liorda
  • 1,552
  • 2
  • 15
  • 38

3 Answers3

1

A stack overflow error could be due to either an infinite recursion (unlikely in your example) or too much data passed on the stack (parameters). It seems your array is passed by copy, not by adress.

You can try to replace the call by

fwrite(&(mat[0][0]), sizeof(double), 3200*3200, fid);

A static array is not a pointer.

jv42
  • 8,521
  • 5
  • 40
  • 64
  • Hi jv42, That's my question exactly: why is my array passed by value? You solution is a nice workaround, tho. – liorda Dec 14 '10 at 14:02
  • Well, that's something to be quoted from the C language specification. A static array (ie an array with fixed size) isn't equivalent to a pointer to its first element, it is handled differently by the C compiler. – jv42 Dec 14 '10 at 16:18
  • Are you sure about this distinction? AFAIK, the name of an array variable is converted to a pointer to the first element whenever it is being referred to as an rvalue. Unfortunately, I don't have a copy of the C standard handy to go digging through it. – Praetorian Dec 14 '10 at 17:08
  • Well I'm quite sure, and it's a common mistake. There are two kinds of array constructs in C. – jv42 Dec 15 '10 at 11:25
1

Is the array being created on the stack? For instance, are you creating the array like this?

void foo()
{ 
  double mat[3200][3200];

  // ... other stuff

  fwrite(mat, sizeof(double), 3200*3200, fid);
}

If so, are you sure that it is fwrite and not the array declaration itself that is causing the stack overflow? The array occupies 3200*3200*8 = 81920000 bytes (about 78 MiB), probably a little too big for a stack allocation. Try malloc'ing the array instead.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • Hi Praetorian, the variable is global, i.e. declared in the outermost scope, and I believe it is allocated on the stack. – liorda Dec 14 '10 at 13:58
  • 1
    Global variables should be in static storage, which is not on the stack. So that isn't the problem. – Karl Knechtel Dec 14 '10 at 14:00
  • @liorda: Global data is not created on the stack, it is created in some implementation defined section such as `.bss` or `.data`. When you say outermost scope, do you mean within `main()` or another function, or outside of any function at the file level? – Praetorian Dec 14 '10 at 14:02
  • @Karl, I'm reading and writing mat before the call to fwrite() (and the result are all good), so the declaration isn't the problem, and the exception I get from windows is "stack overflow", so I don't know that else could it be :/ – liorda Dec 14 '10 at 14:04
  • @Praetorian outside of main() and any other function, namespace, etc. – liorda Dec 14 '10 at 14:06
  • @liorda: C does not have namespaces. – unwind Dec 14 '10 at 14:13
  • 1
    OP specified 'global variable', so it's not defined on the stack. – jv42 Dec 14 '10 at 16:17
0

I guess that the variable was passed (using the stack) by value. Thanks anyone for trying to help me figure this out.

liorda
  • 1,552
  • 2
  • 15
  • 38