Dark Horse programmer--c Language learning experience--function passing two-dimensional array

Source: Internet
Author: User

Dark Horse programmer--c Language learning experience--function passing two-dimensional array

-------Java training , Android training ,iOS training ,. Net Training , look forward to communicating with you! -------

First, you must initialize the pointer when you define it.
Variables are initialized when they are defined, which is a good habit of ensuring that there is no error. Especially in the use of pointers, if we do not give the pointer initialization, there will be a wild pointer, the pointer is not what we want, once the wrong release of the pointer, memory access will occur. So how to initialize pointer variables, there are generally the following methods:
1. Initialize NULL pointer
int* Pinteger=null;
2. Initialize with existing variables
int length=5;
int* pinteger=&length;
3. Allocating space to pointers with memory allocation function
int* pinteger= (int*) malloc (10*sizeof (int));//Allocates a memory space of 10 integers for the pointer.
Second, the correct application and release of memory
After using the pointer, if you do not release the memory used by the pointer, it will cause memory leaks, so there will be a large amount of memory due to failure to release, other programs can not use this part of memory, if a program keeps requesting memory and not to release memory, will soon cause system crashes. So how to apply and release the memory correctly.
1, the pointer initialization, the above has been said
2, the correct application of memory
How to apply for memory is the correct application of memory? The first is to determine whether the pointer is empty, if not empty, then free the pointer to the memory block, if the memory is not freed, and directly to the memory, it will cause memory leaks. After applying for memory, be sure to determine whether the application is successful.
such as: int* pinteger=null;//pointer definition Place
...
if (Pinteger! = NULL)
{
Free (Pinteger);
pinteger=null;//the pointer is not empty after it is released, to set it to be empty
}
Pinteger= (int*) malloc (10*sizeof (int.));
if (Pinteger! = NULL)
{
printf ("The memory request did not succeed \n! ");
Exit (0);
}
...
3. Memory Release
When the program is finished using pointers, be sure to release the memory that the pointer points to. Be sure to remember to set the pointer to a null pointer after release. Because the free function releases the pointer, it simply frees the memory space that the pointer points to, without assigning the pointer to a null value. So be sure to remember to assign the pointer to a null value after releasing the pointer.
Such as:
int* pinteger=null;//pointer definition at
...
Free (pinteger);//Release pointer
Pinteger=null; The pointer is assigned a NULL value
When using pointers, be sure to determine if the pointer is empty
Be sure to determine if the pointer is empty when using the pointer, and if it is empty, do the appropriate action. If you do not make a judgment, you may use a null pointer incorrectly.
such as: char* dest=null;
...
strcpy (dest, "string");//error if dest is empty

The correct way to use this is:
if (dest = = NULL)
{
Dest= (char*) malloc (7*sizeof (char));//Because the string ends with "
So to apply 7-character memory
Determine if the memory request was successful
...
}
strcpy (dest, "string");

-------Java training , Android training ,iOS training ,. Net Training , look forward to communicating with you! -------

Dark Horse programmer--c Language learning experience--function passing two-dimensional array

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.