Co-language

xiaoxiao2021-03-06  47

Question: Memory uses some functions that convert integers to strings:

Char * itoa (int N) {char Retbuf [20]; sprintf (Retbuf, "% D", N); return Retbuf;}

If I call this function: char * STR5 = ITOA (5), what will STR5?

Answer analysis:

The answer is uncertain, it can be determined is definitely not "5" we want.

RetBuf is defined in the function body, is a local variable, and its memory space is located in a position in the stack (STACK), and its scope is also limited to this function in the ITOA (). When the ITOA () function exits, RetBuf will be retracted in the contents of the call stack. At this time, this memory address may store the content. Therefore, returning the partial variable of RetBuf to the caller is not the expected purpose.

So how to solve this problem, don't worry, the method is not only, but more than one, the following will explain three ways to solve this problem:

1) In the ITOA () function, define a Static Char RetBUF [20], according to the characteristics of the static variable, we know that this can guarantee that the function returns to RetBuf will not be recovered because the static variable in the function is not In the stack, it is placed in a place called ".bss" segment in the program, the content of this place is not recovered because the function exits.

This approach can solve the problem, but this approach also leads to the ITOA () function to become an unremissible function (ie, the same input must have the same output), in addition, RetBUF [] The content will be replaced by the next call result, which is not worth recommending.

2) In the ITOA () function, use malloc () to request a memory, and store the result, and then return RetBUF to the caller. Since RetBUF is located in the heap (HEAP), it will not be released as the function is returned, so we can achieve our goal.

However, there is such a situation to note: the caller of the ITOA () function must release it when it does not need RetBuf, otherwise the memory is leaking, if this function and the call function are written, the problem is not But if not, it is easier to hurt the operation of this release memory.

3) Define the function as Char * ITOA (INT N, CHAR * RETBUF), and RetBUF's space is applied and released by the caller, and it is only stored to RetBUF.

This approach is obvious than the first, two methods, avoiding the impact of the method 1 on the function, and avoiding the impact of Method 2 on memory allocation, is a current approach. Practice.

Extended analysis:

In fact, on this problem itself, I think everyone can immediately think of the answer, the key is to use the correct and reasonable use of memory such sensitive resources, and the following is a simple analysis:

1) There are different memory segments in the program, including:

.DATA - has initialized the overall / static variable, which is valid throughout the software implementation;

.BSS - Not initialized the overall / static variable, effective throughout the software execution;

.stack - Function call stack, the content is valid during the execution of the function and is responsible for assignment and reclaim by the compiler;

.heap - Heap, explicitly assigned and reclaimed by the program, if not recovered is a memory leak.

2) The memory you use is best to apply and release yourself. This can be said to be a principle of memory allocation and release, such as the second type above solution, the memory allocated by ITOA (), and finally released by the caller, it is not a good way, it is better to use the third kind Apply and release by the caller yourself. In addition, this principle also means: if you want to use a pointer, it is best to confident that it has pointed to the legitimate memory area. If you don't have your own assignment, or you have illegal pointer access. Many procedural fatal errors are accessing a pointer that does not point to the legitimate memory area, which also includes empty pointers.

Problem: Memory Allocation & SizeOf I use SizeOf to calculate a pointer variable, I want to get the size of the memory block allocated by this pointer variable, can I?

Char * p = null; int NMemsize = 0; ... p = malloc (1024); NMEMSIZE = SizeOf (P);

Answer and analysis:

The answer is not to meet your requirements, and sizeof can only tell you the memory size of the pointer itself. The memory pointed to by the pointer, if it is malloc assignment, SizeOf is no way to know. In other words, Malloc allocated memory is no way to query the memory management module, of course, you can write code to maintain it.

Question: Stack memory use

What is the problem with the following program run?

Char * getString (void) {char p [] = "Hello World"; return p; // The compiler will make a warning} void test4 (void) {char * str = null; str = getString (); // STR Content is garbage cout << str << endl;}

Answer and analysis:

Return to the stack memory, the memory may be destroyed, or it may not be destroyed, but after the scope is marked, it can be used by the system, so the chaos can not know the content, of course, the content returned by the pointer should not change. It is useful, for example, can be used to detect system memory allocation laws, etc.

Question: Memory use related programming specifications

I want to avoid the problem of memory usage as much as possible, is there any shortcut?

Answer and analysis:

Unless you have done something you have done, otherwise, there is a shortcut, that is, standing on the shoulders of the former people, now all big companies have their own coding norms, these norms condense a lot of experience And lessons, there is a high value of use, in view of these regulations, there are many online circulation, here I will no longer list, interested, recommend reference Lin Rui's "High Quality C / C Programming Guide".

Author Blog:

http://blog.9cbs.net/canvashat/

转载请注明原文地址:https://www.9cbs.com/read-82575.html

New Post(0)