Sunday, July 26, 2009

Regarding memory reference error?

Hi All,





I m creating one exe using C programing and i m facing one issue in this.


I defined an array which size was 1024 (like SortOnPrimarySol[1024] )Now i want to increase this size to more than this 1024.





While increasing the size as more than 1035, the exe is creating but while execiting this exe, its giving me memory reference error.


If i will decrease this size to 1030 and again create this exe and run this exe then it is working fine.





Can any one let me know what is the prob while i m giving array size as more than 1030???? and why?? is thr any limitation ??


How can i overcome from this issue???





Plaese let me know.





Thanks in advance,


Sharique

Regarding memory reference error?
you can't define your array: Array[1024] at the begining of your program, and then later, during execution redefine it as Array[1035]. You are running past the end of your array. You just happen to be getting lucky that you aren't running into trouble at point 1025. If you want to change the size of an array during runtime you must dynamically allocate the memory so that you can change it during run time.





initial definition:


char *EmptyValue = (char *)malloc( 1024 );





then later to change the size of the allocated block of memory:


realloc( EmptyValue, 1035 );





the realloc will not change the contents, it will just increase the size.





when you are done with this variable (probably at exit) you must "free" the allocated memory:





free( EmptyValue);
Reply:as the previous poster said, you have to use malloc or new[] (in c++) to dynamically resize an array. but if you used a static array, but your software crashed, then most likely you have a bug in your code, even if it sounds improbable from your point of view.





either send us more code, or (better) use a debugger. if you use gcc, then just start your program in gdb: "gdb test.exe", then "r" to "run".





if you use microsoft visual studio, just press F5 and the debugger will stop when the error appears.


No comments:

Post a Comment