|
28 Jan 2003, 08:01
|
#1
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
MS Visual C++: stack overflow when calling function
What is the problem here? It's a simple function of a simple object. If I call another function of the same object it works fine, but that function messes up:
Code:
terrain.loadTerrain("terrain.raw");
bool CTerrain::loadTerrain(char* filename)
{ //it doesn't even come here
unsigned char terra[TERRAINSIZE*TERRAINSIZE];
FILE *pFile = NULL;
etc
I've done a rebuild, and I've tried it on two different computers (one with win98, one with win2k), but the problem remains.
|
|
|
28 Jan 2003, 08:37
|
#2
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
oh..... great....
I have managed to narrow the problem down to one line:
unsigned char terra[TERRAINSIZE*TERRAINSIZE];
It appears that I cannot declare a char aray of one MB (1024 * 1024)........ heh......
1024 * 1012 doesn't give a problem but anything higher gives an error.
Is there something I'm missing? Is there a limit of how much memory you can declare?!
|
|
|
28 Jan 2003, 09:21
|
#3
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
Try using 'new' to allocate the memory rather than the way you're doing it. That way you won't be using the stack (which in VC++ is 1MB).
Guess how I know this? lol
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
28 Jan 2003, 09:49
|
#4
|
Tourist
Join Date: Jun 2001
Location: moon
Posts: 90
|
Re: MS Visual C++: stack overflow when calling function
Quote:
Originally posted by Structural Integrity
What is the problem here? It's a simple function of a simple object. If I call another function of the same object it works fine, but that function messes up:
Code:
terrain.loadTerrain("terrain.raw");
bool CTerrain::loadTerrain(char* filename)
{ //it doesn't even come here
unsigned char terra[TERRAINSIZE*TERRAINSIZE];
FILE *pFile = NULL;
etc
I've done a rebuild, and I've tried it on two different computers (one with win98, one with win2k), but the problem remains.
|
Code:
terrain.loadTerrain("terrain.raw");
bool CTerrain::loadTerrain(char* filename)
{ //it doesn't even come here
unsigned char *terra[TERRAINSIZE*TERRAINSIZE] = new char[TERRAINSIZE*TERRAINSIZE];
FILE *pFile = NULL;
etc
delete[] terra;
That is what Gayle mentioned, in fact you can assign more stack for your application (look at the compiler or editbin options in the manual, its /F for cl.exe and /STACK for editbin, I think), but I'd prefer the pointer way.
__________________
Quote:
Originally posted by Bloomers III
sex is dirty and for losers who can't masturbate properly
|
|
|
|
28 Jan 2003, 09:50
|
#5
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
That array goes on the stack?!
WTF?
I always thought the array memory was allocated, not put on a stack.
I got around it by making the array part of my class, thus allocating the memory compile-time.
This still strikes me as "odd" though.
|
|
|
28 Jan 2003, 10:41
|
#6
|
Tourist
Join Date: Jun 2001
Location: moon
Posts: 90
|
No, it is a local variable and that's why in the stack frame. If you want to have it on heap you have to allocate it with new or malloc.
__________________
Quote:
Originally posted by Bloomers III
sex is dirty and for losers who can't masturbate properly
|
|
|
|
28 Jan 2003, 18:08
|
#7
|
Friendly geek of GD :-/
Join Date: Nov 2000
Location: On my metal roid
Posts: 923
|
lol that's why i prefer VBasic ^^
*ducksandrunsaway*
__________________
[ »] Entropy increases! :-/
|
|
|
28 Jan 2003, 19:45
|
#8
|
∞+♪˛
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
|
Could also declare it as
static unsigned char terra[TERRAINSIZE*TERRAINSIZE];
Should save (don't know how much) time allocating memory... Might also save time when accessing the array, since the address doesn't move, but might not either. Probably depends on the sanity of the writers of the compiler's optimizer. Although it's probably not an important amount of time to a function called loadTerrain.
(Same as declaring it outside a function, except it's supposed to get reset to zero before the program starts. It shouldn't make the program a megabyte bigger, but it's written by Micro$oft, so can't be sure.)
But new and delete are probably fine. (Haven't actually used new and delete, I just use malloc and free, though. Don't know if there is a difference.)
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics... 猫
<!--Last incarnation: Nov 2000-->
|
|
|
28 Jan 2003, 19:48
|
#9
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Quote:
Originally posted by Cyp
But new and delete are probably fine. (Haven't actually used new and delete, I just use malloc and free, though. Don't know if there is a difference.)
|
As far as I know, the only difference is that new calls the constructor when allocating memory for classes. I cant imagine thered be any speed difference, but I could be wrong.
|
|
|
28 Jan 2003, 23:18
|
#10
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
Quote:
Originally posted by Nodrog
As far as I know, the only difference is that new calls the constructor when allocating memory for classes. I cant imagine thered be any speed difference, but I could be wrong.
|
We had a thread about this a few months ago. I think we came to the assumption that new/delete carried a slight overhead but I cba to check again.
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
29 Jan 2003, 07:00
|
#11
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
I don't remember that
|
|
|
29 Jan 2003, 08:14
|
#12
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Was that that "dynamic 2D array" thread? I think it was.
Anyway, I WOULD use new in this case. It isn't a performance critical part. For performance critical parts I'd use a global/static array anyway. I now used an array declared in the class itself. Perhaps I find another use for it in a later stage, if not, I'll change it to a dynamically allocated array.
|
|
|
31 Jan 2003, 14:26
|
#13
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
Quote:
Originally posted by Structural Integrity
Was that that "dynamic 2D array" thread? I think it was.
|
That's the one
On an only vaguely related note, not using the stack (as a habit) also makes you much less likely to introduce a script kiddie exploitable bug
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
|
All times are GMT +1. The time now is 23:28.
| |