Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C what are the four areas of memory in the program?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what are the four areas of memory in the c program, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

In C program, the operating system often divides the code into four areas: heap, stack, global area and code area.

Heap (heap): generally, programmers request to allocate and free space.

Stack: the operating system automatically allocates and releases space to store function parameter values, local variables, and so on.

Global area (global): the global area is subdivided into uninitialized area, initialized area and character constant area. The uninitialized area stores global variables and static variables in one area, and the initialized global variables and static variables are in the same adjacent area, which is released by the operating system after the end of the program.

Code area (code): the binary code that holds the body of a function.

The main function of the four memory areas of C program is to limit the scope and life cycle of variables and to facilitate function calls.

For example:

Char get_str ()

{

Char str [] = "asdaf"

Printf ("str =% d str), sizeof (& str))

Return str

}

Int main (int argc, char argv [])

{

QCoreApplication a (argc, argv)

Char * b = NULL;b = get_str (); printf ("b =% s", b); return a.exec ()

}

The printed b should be uncertain because the memory pointed to by b has been freed. First of all, the string "asdaf" is stored in the global area, and memory is freed after the program ends running, the array str [] is in the stack area, and the memory is freed after the get_str () function finishes running. So the memory of the str pointed to by b has been freed, so it is uncertain.

Thank you for reading this article carefully. I hope the article "what are the four memory areas in the c program" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report