Difference Between Stack and stack in C Development

Source: Internet
Author: User

(1) stack zone (stack): automatically assigned and released by the compiler, storing function parameter values and local variable values. The operation method is similar to the stack in the data structure. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack. The system willProgramProvides memory. Otherwise, an exception is reported, indicating stack overflow. For example:
Char * allocstrfromstack ()
{
Char pstr [100];
Return pstr; // simulate the stack with Arrays
}
For this function, the function in the pstr is released by the system when it is returned. It is only available when the function is called internally. In fact, there is no returned char.

To this end, I have done an experiment:

 
# Include "stdafx. H "# include" string. H "char * addreturn () {char s [] =" adcdef "; unsigned char n = sizeof (s)/sizeof (char); printf (" % d \ n ", n); printf ("% d \ n", strlen (s); For (unsigned char I = 0; I <n; I ++) printf ("% C", s [I]); printf ("\ n"); Return s;} int main (INT argc, char * argv []) {char * U; u = addreturn (); For (unsigned char I = 0; I <7; I ++) {printf ("% C \ n ", U [I]);} printf ("Hello world! \ N "); Return 0 ;}

The execution result is:

It can be seen that the returned content of addreturn is not valid, so the output in main is garbled.

In Windows, the stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the top stack address and the maximum stack capacity are pre-defined by the system. If the requested space exceeds the remaining stack space, overflow is prompted.
(2) Heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list. The keywords that may be used are new, malloc, delete, and free. In C, malloc functions such as P1 = (char *) malloc (10); but note that P1 is in the stack, because they can still be considered as local variables. For example:
Char * allocstrfromheap (INT Len)
{
Char * pstr;
If (LEN <= 0) return NULL;
Return (char *) malloc (LEN );
}
The memory returned by this function is normal and can be used. But you must call free release, or the memory leak memory will leak!
The operating system has a linked list that records idle memory addresses. When the system receives a program application, it traverses the linked list to find the first heap node with a space greater than the requested space, delete the node from the idle node linked list and allocate the space of the node to the program. In addition, for most systems, the size of the allocation will be recorded at the first address in the memory space.CodeThe delete statement in can be correctly released. The heap is a data structure extended to the high address and is a non-sequential memory area (this is because the system uses a linked list to store idle memory addresses ).
(3) instance proof

# Include "stdafx. H "# include <string. h> # include <stdlib. h> int main (INT argc, char * argv []) {char * STR = NULL; char * string = "Hello World"; STR = getmemory (); memcpy (STR, string, strlen (string); STR [strlen (string)] = '\ 0'; printf ("% s \ n", STR); Return 0 ;}

Heap practices:

 
Char * getmemory () {char * para = (char *) malloc (20); Return para ;}

Stack practices:

 
Char * getmemory () {char para [20]; return para ;}

The heap can run the result, but the stack does not. The warning c00002: returning address of local variable or temporary is displayed. Therefore, the declared address space is returned within the function. Once this function is generated, it is invalid, so it cannot be used like this.

(4) experiment 3 uses the memory address returned by the function to perform operations. If the address returned by the form parameter is used, the following two function segments are compared. The main function is:

# Include "stdafx. H "# include <string. h> # include <stdlib. h> void func (char * P) {P = (char *) malloc (10);} int main (INT argc, char * argv []) {char STR [] = "ABCDE"; char * str1 = NULL; func (str1); memcpy (str1, STR, strlen (STR); str1 [strlen (STR)] = '\ 0'; printf ("% s \ n", str1); Return 0 ;}

At the beginning, I always thought this was correct, and I found it failed during compilation and execution. The key reason is that str1 is a null pointer and cannot be used as an input or output parameter. The following is correct:

 
# Include "stdafx. H "# include <string. h> # include <stdlib. h> void func (char ** p) {* P = (char *) malloc (10);} int main (INT argc, char * argv []) {char STR [] = "ABCDE"; char * str1 = NULL; func (& str1); memcpy (str1, STR, strlen (STR )); str1 [strlen (STR)] = '\ 0'; printf ("% s \ n", str1); Return 0 ;}

Although str1 is still null, the input func is the memory address of this NULL pointer, which can be operated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.