Home > Back-end >  Put the structure in the stack usage is generally under what circumstances?
Put the structure in the stack usage is generally under what circumstances?

Time:10-16

 typedef struct dog {

Int weight;

} dog

Dog dog1;
Dog1. Weight=2;


This dog1 is on the stack, normally we would like the following use structure

Dog * dog2=(dog *) malloc (sizeof (dog));

Dog2 to point to in the heap structure,



Under what circumstances do dog1 that above, usually use?

CodePudding user response:


I can think of,
1. Consider taking up space, if the object of the application memory is too big, so you define such as stack char a, [999999]. This will certainly run collapse,
2. Consider the operation lifetime of objects, if you want to be exceptionally long lifetime, with the heap memory,

CodePudding user response:

1 small memory, after 2 USES automatically released by the stack, this kind of circumstance can use dog1

CodePudding user response:

Local variables and function arguments are stack object
Are dynamically allocated heap object
Also use global variables and static variable area

About the location of the relationship between above

Dynamic memory can be a very big, but nothing is infinite, has a physical limit on any of the existing system
  • Related