Memory knowledge accumulation and memory knowledge accumulation

Source: Internet
Author: User

Memory knowledge accumulation and memory knowledge accumulation
Questions about memory (accumulating)


Void GetMemory (char * p)

{

P = (char *) malloc (100 );

}

Void Test (void)

{

Char * str = NULL;

GetMemory (str );

Strcpy (str, "hello world ");

Printf (str );

}

What are the results of running the Test function?

A: The program crashes.

Because GetMemory cannot transmit dynamic memory,

Str in the Test function is always NULL.

Strcpy (str, "hello world"); program crash

.

Char * GetMemory (void)

{

Char p [] = "hello world ";

Return p;

}

Void Test (void)

{

Char * str = NULL;

Str = GetMemory ();

Printf (str );

}

What are the results of running the Test function?

A: It may be garbled.

Because GetMemory returns "stack memory"

Pointer, the pointer address is not NULL, but its original

The current content has been cleared, and the new content is unknown.

Void GetMemory2 (char ** p, int num)

{

* P = (char *) malloc (num );

}

Void Test (void)

{

Char * str = NULL;

GetMemory2 (& str, 100 );

Strcpy (str, "hello ");

Printf (str );

}

What are the results of running the Test function?

A:

(1) Output hello

(2) Can I change the memory leakage to strcpy_s?

Void Test (void)

{

Char * str = (char *) malloc (100 );

Strcpy (str, "hello ");

Free (str );

If (str! = NULL)

{

Strcpy (str, "world ");

Printf (str );

}

}

What are the results of running the Test function?

A: Tampering with content in the dynamic memory area is difficult to pre-empt.

Material, very dangerous.

Because free (str); after that, str becomes a wild pointer,

If (str! = NULL) the statement does not work.

2. Memory leakage

In computer science, memory leakage refers to the failure to release memory that is no longer used due to negligence or errors. Memory leakage does not mean that the internal memory disappears physically, but that after the application allocates a certain segment of memory, the memory is wasted due to design errors and the loss of control over this segment of memory. Memory leakage is similar to many other problems, and can be analyzed only by programmers who can obtain the program source code. However, many people are accustomed to describing any increase in memory usage as Memory leakage, even if strictly speaking this is not accurate.
Generally, memory leakage refers to heap memory leakage. Heap memory refers to the memory allocated by the program from the heap, which is of any size (the size of the memory block can be determined during the running period). The released memory must be displayed after use. Applications generally use functions such as malloc, realloc, and new to allocate a block of memory from the heap. after use, the program must call free or delete to release the block. Otherwise, this memory cannot be used again, so we can say this memory is leaked.

Related Article

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.