# [SOLVED] C code that crashes with one line uncommented



## Special2God (Oct 5, 2009)

I have been programing for some time, not that I am any good, but I try to keep security, form, modularity, portability, etc... in mind. 
I have a problem which is probably best illustrated if you read the below instructions. 
Attached is a test.txt, just paste it into your favorite compiler (after opening empty C win32 console project, etc...) 
Compile and run with the one line un-commented (line 17 I think), then re-comment the line, and un-comment the other line (line 20), compile and run, tell me if the program crashes either time. 
I have tried this on:
Win 7 w/ 
Visual Studio 2008
and
Dev C++ (yes, I realize it is simple and out dated)
Crashes, crashes, crashes, what's wrong, a program should be able to create these arrays without any problems..?

TY


----------



## Ninjaboi (Dec 27, 2010)

*Re: C code that crashes with one line uncommented*

It compiled with both lines un-commented just fine. I ran it, and the test was easy enough. From your system info, you have plenty of RAM for running this program.

I ran it on a system with:

Windows XP Home Edition
4GB RAM

Compiled using Visual Studio 2008, no compile errors, no run-time errors, no crashes.

Have you checked that your compiler and programming environment is setup correctly?


----------



## Special2God (Oct 5, 2009)

*Re: C code that crashes with one line uncommented*

Ran on school computer with 2GBs of ram, crashed. 
Ran on home computer with 8GBs of ram, crashed. 
Just an anomaly that follows me around, lol, idk, just thought I would throw it out there. 
Thank You for the response


----------



## Ninjaboi (Dec 27, 2010)

*Re: C code that crashes with one line uncommented*

I used a .cpp extension on my file type instead of .c . Perhaps trying that might correct the issue. It's hard to detect the errors at run-time ( crashes ), especially if they have no error message.


----------



## niemiro (Feb 20, 2010)

*Re: C code that crashes with one line uncommented*

Hello!

This is really, really strange. I have just tried it. Any and all of the following lines crash the program for me:

*int intHalf [512] [1024/sizeof(int)] = { {0} }; //half megabyte of ints
char charHalf [512] [1024/sizeof(char)] = { {0} }; //half megabyte of chars
char charA [120] [1024/sizeof(char)] = { {0} }; //120 Kilobytes of chars
char charB [110] [1024/sizeof(char)] = { {0} }; //110 Kilobytes of chars*

And yes, even your good line crashes it for me. The rest of the lines do not crash the program! 

I am using Visual Studio 2010 Professional Edition, on Vista SP2 x86 with 4GB of RAM.

As one last test, I am uploading a copy of my compiled .exe with ALL lines uncommented. It crashes for me. Does it crash for you, Ninjaboi and Special2God?

Richard


----------



## reventon (Oct 16, 2009)

*Re: C code that crashes with one line uncommented*

Richard - your compiled exe crashes for me (Win7, 4GB RAM).

I compiled the original version myself with Visual Studio 2010 Professional - it crashes even when exactly as you sent it (with the two lines commented out).

I commented out the lines as Richard did to get it to work and it works for me too (which makes sense as we are using the same compiler).


----------



## Ninjaboi (Dec 27, 2010)

*Re: C code that crashes with one line uncommented*

We'll the .exe file your provided niemiro crashed for me as well, but I think the problem could be due to a missing dependency file. Try static linking in Visual Studio by switching to /MT, then compile. See if that works for you.


----------



## Special2God (Oct 5, 2009)

*Re: C code that crashes with one line uncommented*

Well, niemiro, you could remove #include <stdio.h> and the printf and getc at the end of the file and try compiling that way (example attached).


----------



## niemiro (Feb 20, 2010)

*Re: C code that crashes with one line uncommented*



Ninjaboi said:


> We'll the .exe file your provided niemiro crashed for me as well, but I think the problem could be due to a missing dependency file. Try static linking in Visual Studio by switching to /MT, then compile. See if that works for you.


Just compiled it with /MT. Exactly the same problem. I still get a Stack Overflow. I shall look into it in about half an hours time or so.



Special2God said:


> Well, niemiro, you could remove #include <stdio.h> and the printf and getc at the end of the file and try compiling that way (example attached).


Still the same problem  Still those four lines.

Richard


----------



## Special2God (Oct 5, 2009)

*Re: C code that crashes with one line uncommented*

Ok, I was wondering if the crash was compiler or environment dependent, and it appears to be environment dependent, since niemiro used VS 2010, and I tried Dev C++ and VS 2008 and we got crashes. 
Since Ninjaboi ran with XP and no crash. 
niemiro used Vista and crash. 
I used Win 7 and crash. 
Appears the OS could be the problem/key. 

Any other ideas? 
Thanks for all the responses


----------



## Ninjaboi (Dec 27, 2010)

*Re: C code that crashes with one line uncommented*

The program itself is not platform dependent to that extent, it's simply a problem either with the programming environment your currently using or the memory stack size. You should try to set the stack size to a higher amount. Try looking here.

As far as I can tell, that's all I can get out of the situation. I've never had to use a stack beyond 2MB, but that's just me. The default in Visual Studio is 1MB ( last time I checked ). If your using more than that, you'll have to increase it. Try increasing it by .5MB, compiling, and running the program. Continue this process until it works ( or your reach an outrageous stack size, then we can safely assume that isn't the problem ).


----------



## Special2God (Oct 5, 2009)

*Re: C code that crashes with one line uncommented*

Stack size idea did not change anything, thanks for the idea though. 
The whole program really is not that important, just some random problem I ran into. Nothing to worry about when actually coding, since malloc and functions don't have any problems like this.


----------



## AlbertMC2 (Jul 15, 2010)

*Re: C code that crashes with one line uncommented*

Hi

Ninjaboi is correct. It must be the stack size.
I ran this through Dev-c++ and it also crashed.
Commenting out any of the Arrays seems to allow it to run.
Also making one of the arrays substantially smaller allows it to run.
This all indicates that it is a memory problem.
Since all programs (DevC++, VS etc) only allocate a small amount of stack space (the default being 2MB) - which isn't enough in this case there are 2 options:
1. Either increase the allocated stack size or
2. Use pointers (which would allocate memory dynamically as needed).

In DevC++ you can increase the allocated stack size by using the linker option
*-Wl,--stack,0x10000000 *

Note that your arrays are requesting more than 2MB Stack memory:

```
double doubleHalf [512] [128]   = 512*128*8bytes  = 524288bytes
int intHalf [512] [256]         = 512*256*4bytes  = 524288bytes
char charHalf [512] [1024]      = 512*1024*1bytes = 524288bytes
double doubleEighth [128] [128] = 128*128*8bytes  = 131072bytes
int intEighth [128] [256]       = 128*256*4bytes  = 131072bytes
char charEighth [128] [1024]    = 128*1024*1bytes = 131072bytes
char charA [120] [1024]         = 120*1024*1bytes = 122880bytes
char charB [110] [1024]         = 110*1024*1bytes = 112640bytes
                                                  = 2201600bytes
                                                  = 2.2MB

[B]Where:[/B]
sizeof(char) = 1byte
sizeof(int) = 4bytes
sizeof(double) = 8bytes
```
(Hopefully my maths is correct)
And this is over and above all the extra overhead the program uses.


----------



## Special2God (Oct 5, 2009)

*Re: C code that crashes with one line uncommented*

Ok, thank you for the info and solution, I really appreciate it, will hold on to it for later. 
[solved]


----------

