Dynamic Memory Allocation

Source: Internet
Author: User
8) "I want to use malloc" and "I cannot use malloc"
Let's look at a metamorphosis program:

/* XX. C: XX module implementation file */
Int * pint;
/* Initialization function of XX module */
Xx_intial ()
{
Pint = (int *) malloc (sizeof (INT ));
...
}
/* Other functions of the XX module (for example only )*/
Xx_otherfunction ()
{
* Int = 10;
...
}

This program defines a global integer variable pointer. In the initialization function of the XX module, this pointer dynamically applies for memory and points pint to the first address of the memory, in other functions of the XX module, the pint pointer is used to read and assign values to the integers it points.

This program makes me sad for many days and throws! This is the work of a master's degree in computer science at my alma mater! In order to use malloc, the author desperately made a program that should have been flat with a global integer variable into a global integer pointer and applied for memory dynamically in the initialization function, be smart and expose your ignorance! I will never see such a program again.

So how should malloc be used? The author provides the following rules:
  Rule 1Do not use malloc to use malloc. malloc is not an aim, but a means;

  Rule 2The true meaning of malloc is reflected in "dynamic" application. If the features of the program do not need dynamic application, do not use malloc;

The abnormal programs listed above do not have the characteristics that require dynamic application. They should be changed:

/* XX. C: XX module implementation file */
Int example;
/* Initialization function of XX module */
Xx_intial ()
{
...
}
/* Other functions of the XX module (for example only )*/
Xx_otherfunction ()
{
Example = 10;
...
}

Rule 3What kind of program has the characteristics of dynamically applying for memory? There are two scenarios:
(1) I don't know how many of them are coming.
Do not understand? For example, if you are processing a message queue, you will store all the received messages to this queue. After processing the packets in the queue header, You need to retrieve the element of the queue header.

You don't know how many packets (so you don't know how many packets should be used). After these packets are processed, they must be released ), this situation is suitable for malloc and free.

(2) Growing Up slowly
For example, how do you write a text editor program in a resource-constrained system? Do you need to define arrays like this?

Char STR [10000];

No, you should not. Even if you define a 10000-byte string, your program will be finished if you enter 10001 characters.

At this time, it is suitable to use malloc, because you do not know how many characters the user will enter, and the text is growing slowly. Therefore, you should also apply for memory slowly and store the strings in a queue.

So should we define the data structure in this way and malloc a charqueue space every time the user inputs a character?

Typedef struct tagcharqueue
{
Char ch;
Struct tagcharqueue * next;
} Charqueue;

No, this is not the case! This causes each character to overhead "1 + pointer length.

The correct method is:

Typedef struct tagcharqueue
{
Char STR [100];
Struct tagcharqueue * next;
} Charqueue;

Let the characters walk slowly in units of 100. When the number of characters entered reaches an integer multiple of 100, apply for a charqueue space.

Rule 4Malloc and free must appear in pairs.
They are a loving couple, and malloc will inevitably die slowly if it is less free. Paired appearance not only shows how many malloc resources should be free, but also shows that they should appear in the same function as much as possible. "Whoever applies, who releases it ", see the following program:

Char * func (void)
{
Char * P;
P = (char *) malloc (...);
If (P! = NULL)
...; /* A series of operations on p */
Return P;
}
/* Call func () somewhere and free the dynamically applied memory in func */
Char * q = func ();
...
Free (Q );

The above Code violates the malloc and free principle of "who applies, who releases it". The code is highly coupled. You need to know the internal details when calling the func function! The correct method is:

/* Apply for memory at the call and input the func function */
Char * P = malloc (...);
If (P! = NULL)
{
Func (P );
...
Free (P );
P = NULL;
}
/* Function func receives PARAMETER p */
Void func (char * P)
{
... /* A series of operations on p */
}

  Rule 5After free, you must set the pointer to null to prevent it from becoming a "wild" pointer.


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.