Malloc usage error example

Source: Internet
Author: User

Error program:

Void getmemory (char * P)
{
P = (char *) malloc (100 );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (STR );
Strcpy (STR, "Hello World ");
Printf ("% s", STR );
}

This is a question that tests pointer comprehension. After the above program is run:

1. After getmemory (STR) is called, STR does not change. It is still null. It only changes the memory of a copy of Str.

2, strcpy (STR, "Hello World"); an error occurs when the program runs.

3. A memory error may occur during the new operation. * P = (char *) malloc (Num) should be used to determine whether the memory application is successful. The following code should be added:
If (* P = NULL)
{
... // Handle the Memory Request failure
}

4. The dynamically created memory is not released.

 

 

Error analysis:

The error message "P" in getmemory (char * P) is "str" in getmemory (STR. But P "not" STR, it is "equal to" str.

Like: int A = 100;
Int B = A; // now B is equal to
B = 500; // can a = 500?
Obviously, a cannot be considered as 500, because B is only equal to a, but not! When B changes, A does not change, and B is not equal to. Therefore, although P already has new memory, STR is still null

Getmemory (STR); // transmits STR into the table. STR is a pointer, and it is actually an int.
Void getmemory (char * P) // P is a copy of STR
{
P = (char *) New char [100]; // The value of P is changed, but the value of STR is not changed.
}
WhileDouble pointerWhy:
Getmemory (& Str); // transmits the STR address
Void getmemory (char**P) // P is a copy of the STR address
{

*P = (char *) New char [100]; // The Value indicated by P changes, that is, the value of Str.

}

Method 1: (this method is recommended)

Void getmemory2 (char ** P) is changed to a second-level pointer.
Void getmemory2 (char**P, int num)
{
*P = (char *) malloc (sizeof (char) * num );
}
Void test (void)
{
Char * STR = NULL;
Getmemory = (&Str );
Strcpy (STR, "Hello World ");
Printf (STR );
}

 

Method 2:

Char * getmemory ()
{
Char * P = (char *) malloc (100 );
Return P;
}
Void test (void ){
Char * STR = NULL;
STR = getmemory ();
Strcpy (STR, "Hello World ");
Printf (STR );
}

 

 

 

Appendix A (related materials)

Question 5:
Char * getmemory (void)
{
Char P [] = "Hello World ";
Return P;
}

Void test (void)
{
Char * STR = NULL;
STR = getmemory ();
Printf (STR );
}
Question 6:
Void getmemory (char ** P, int num)
{
* P = (char *) malloc (Num );
}

Void test (void)
{
Char * STR = NULL;
Getmemory (& STR, 100 );
Strcpy (STR, "hello ");
Printf (STR );
}
Question 7:

Void test (void)
{
Char * STR = (char *) malloc (100 );
Strcpy (STR, "hello ");
Free (STR );
... // Other omitted statements
}

Answer:

Question 5
Char P [] = "Hello World ";
Return P;
The P [] array is the partial automatic variable in the function. After the function returns, the memory has been released. This is a common mistake made by many programmers. Its root cause is that they do not understand the survival of variables.

Question 6
1. getmemory avoids Question 4. The input parameter of getmemory is a pointer to the string pointer, but the request memory and value assignment statement are executed in getmemory.
* P = (char *) malloc (Num );
If the memory application is successful, add:
If (* P = NULL)
{
... // Handle the Memory Request failure
}
2. the malloc memory is not released in the test function of question 6.

Question 7
The problem is the same as that in question 6.
Char * STR = (char *) malloc (100); no memory application is successful. In addition, STR is left blank after free (STR, as a result, it may become a "wild" pointer. The following should be added: Str = NULL;

 

Analysis:

Question 4 ~ 7. Measure the test taker's understanding about memory operations. Generally, 50-50 of the respondents with solid basic skills can answer these questions correctly ~ 60 error. However, it is not easy to answer the correct questions completely.

The memory operations are mainly focused on:

(1) pointer understanding;

(2) The survival time and scope of the variable;

(3) good dynamic memory application and release habits.

Let's take a look at the following program errors:

Swap (int * P1, int * P2)
{
Int * P;
* P = * P1;
* P1 = * P2;
* P2 = * P;
}

In the swap function, P is a "wild" pointer, which may point to the system zone, causing program running to crash. In VC ++, the "access violation" error is prompted during debug ". The program should be changed:

Swap (int * P1, int * P2)
{
Int P;
P = * P1;
* P1 = * P2;
* P2 = P;
}

 

 

Error program:

Void getmemory (char * P)
{
P = (char *) malloc (100 );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (STR );
Strcpy (STR, "Hello World ");
Printf ("% s", STR );
}

This is a question that tests pointer comprehension. After the above program is run:

1. After getmemory (STR) is called, STR does not change. It is still null. It only changes the memory of a copy of Str.

2, strcpy (STR, "Hello World"); an error occurs when the program runs.

3. A memory error may occur during the new operation. * P = (char *) malloc (Num) should be used to determine whether the memory application is successful. The following code should be added:
If (* P = NULL)
{
... // Handle the Memory Request failure
}

4. The dynamically created memory is not released.

 

 

Error analysis:

The error message "P" in getmemory (char * P) is "str" in getmemory (STR. But P "not" STR, it is "equal to" str.

Like: int A = 100;
Int B = A; // now B is equal to
B = 500; // can a = 500?
Obviously, a cannot be considered as 500, because B is only equal to a, but not! When B changes, A does not change, and B is not equal to. Therefore, although P already has new memory, STR is still null

Getmemory (STR); // transmits STR into the table. STR is a pointer, and it is actually an int.
Void getmemory (char * P) // P is a copy of STR
{
P = (char *) New char [100]; // The value of P is changed, but the value of STR is not changed.
}
WhileDouble pointerWhy:
Getmemory (& Str); // transmits the STR address
Void getmemory (char**P) // P is a copy of the STR address
{

*P = (char *) New char [100]; // The Value indicated by P changes, that is, the value of Str.

}

Method 1: (this method is recommended)

Void getmemory2 (char ** P) is changed to a second-level pointer.
Void getmemory2 (char**P, int num)
{
*P = (char *) malloc (sizeof (char) * num );
}
Void test (void)
{
Char * STR = NULL;
Getmemory = (&Str );
Strcpy (STR, "Hello World ");
Printf (STR );
}

 

Method 2:

Char * getmemory ()
{
Char * P = (char *) malloc (100 );
Return P;
}
Void test (void ){
Char * STR = NULL;
STR = getmemory ();
Strcpy (STR, "Hello World ");
Printf (STR );
}

 

 

 

Appendix A (related materials)

Question 5:
Char * getmemory (void)
{
Char P [] = "Hello World ";
Return P;
}

Void test (void)
{
Char * STR = NULL;
STR = getmemory ();
Printf (STR );
}
Question 6:
Void getmemory (char ** P, int num)
{
* P = (char *) malloc (Num );
}

Void test (void)
{
Char * STR = NULL;
Getmemory (& STR, 100 );
Strcpy (STR, "hello ");
Printf (STR );
}
Question 7:

Void test (void)
{
Char * STR = (char *) malloc (100 );
Strcpy (STR, "hello ");
Free (STR );
... // Other omitted statements
}

Answer:

Question 5
Char P [] = "Hello World ";
Return P;
The P [] array is the partial automatic variable in the function. After the function returns, the memory has been released. This is a common mistake made by many programmers. Its root cause is that they do not understand the survival of variables.

Question 6
1. getmemory avoids Question 4. The input parameter of getmemory is a pointer to the string pointer, but the request memory and value assignment statement are executed in getmemory.
* P = (char *) malloc (Num );
If the memory application is successful, add:
If (* P = NULL)
{
... // Handle the Memory Request failure
}
2. the malloc memory is not released in the test function of question 6.

Question 7
The problem is the same as that in question 6.
Char * STR = (char *) malloc (100); no memory application is successful. In addition, STR is left blank after free (STR, as a result, it may become a "wild" pointer. The following should be added: Str = NULL;

 

Analysis:

Question 4 ~ 7. Measure the test taker's understanding about memory operations. Generally, 50-50 of the respondents with solid basic skills can answer these questions correctly ~ 60 error. However, it is not easy to answer the correct questions completely.

The memory operations are mainly focused on:

(1) pointer understanding;

(2) The survival time and scope of the variable;

(3) good dynamic memory application and release habits.

Let's take a look at the following program errors:

Swap (int * P1, int * P2)
{
Int * P;
* P = * P1;
* P1 = * P2;
* P2 = * P;
}

In the swap function, P is a "wild" pointer, which may point to the system zone, causing program running to crash. In VC ++, the "access violation" error is prompted during debug ". The program should be changed:

Swap (int * P1, int * P2)
{
Int P;
P = * P1;
* P1 = * P2;
* P2 = P;
}

 

 

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.