Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stack overflow problem 1

Status
Not open for further replies.

dmaranan

Programmer
Aug 14, 2001
46
US
I'm having a problem understanding structs. I've created a struct (see code below) and then when I compile using MS Visual Studio I recieve a stack overflow problem. When I lower the number of items of the array to around 500, it compiles fine. What am I missing here? Thanks in advance.

#include <stdio.h>
#include <stdlib.h>

typedef struct {
char item[5];
float price[125];
} inventory;

void main()
{
inventory allitems[2000]; /* array of inventory structures */
printf("It Works!\n");
}
 
You either need to increase your stack size or allocate the array on the heap
Code:
int main ()
{
   inventory* allitems = (inventory*) malloc (sizeof(inventory) * 2000);
   printf ("It works\n");
   free (allitems);
   return 0;
}
Another alternative is to declare the array globally outside main
Code:
inventory allitems[2000];
int main ()
{
   printf ("it works\n");
   return 0;
}
 
xwb said:
You either need to increase your stack size or allocate the array on the heap
Is it actually possible to increase your stack size? How would you do that in Visual Studio?
 
> void main()
main returns an int

> inventory
sizeof(inventory) is ~500 bytes
2000 of them would be ~1MB
The default stack size is also 1MB

Making the array static would also work in addition to xwb's suggestions.

@cpjust
It's a linker option
It should be in the GUI interface somewhere, under the linker options.

--
 
Try /Fstack_size compiler option (in cmd line or in IDE Project option text box.
 
Thanks. I don't know if I'll ever need to increase my stack size, but it's good to know anyways.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top