C Language realization of the list of one-way linked list (ii) node memory application and data initialization __c language

Source: Internet
Author: User
Tags function definition
C language Realization of the list of one-way linked list (b) node memory application and data initialization
In the previous article has given the overall design framework, now tells each one to give each function realization, in order to one by one explain, clear point, will introduce separately.
This article presents the first function of the mylist_single.c---implementation file, which is to request memory for a node in the linked list and initialize the data inside it. The program follows, because it is the first function, so it gives the previous section of the entire file.
/*
*****************************************************************************************
* UART Block * * (c) Copyright S Reserved * * filename:mylist_single.c * * Function: Operation function definition of one-way non-cyclic list * * history:1. Wangyi 2015-4-15 23:0 0 Version 1.0 creat * ***************************************************************************************** * * #i Nclude <stdlib.h> #include <stdio.h> #include <string.h> #include "mylist_single.h"/*==============   ================================================================ * Operation: Allocates memory for the node of the linked list and initializes the data element before the operation: Plistnode as the node of the list *
After operation: Returns the node after allocating memory and initialization, the operation failed to return null ==============================================================================*/ mylistnode* Mallocmeminitnode (mylistnode* plistnode) {//Allocate storage space for nodes Plistnode = (mylistnode*) malloc (sizeof (MYLISTN

    ODE));
   if (Plistnode = NULL) {//Memory allocation failure fprintf (stderr, "The Allocating Memory defeat.\n");
    return NULL;
        else {//Node data initialization strcpy (Plistnode->snodedata.cname, "");
        plistnode->snodedata.iage = 0;        
    PLISTNODE-&GT;PNEXTNODEADDR = NULL; 
return plistnode; }

This program gives the implementation of all the headers of the file information, you can be clearly visible, here no longer repeat. The function is simply explained below.
The function applies memory for the incoming node, initializes the internal data after the memory request succeeds, and returns the node after the application memory. It is important to note here that the passing of the first-level pointer, though allocated memory, but can not be directly out of the function, because it is the value of delivery, so you must use the return method to apply the memory after the pointer back to the external to use, as for specific reasons, in my blog article "C" "pointer" as "function parameters" When the attention of things in a very detailed explanation, you can look at the article, link is:
http://blog.csdn.net/dan15188387481/article/details/49363605 Click here to open the link directly to the article
This function is not very recommended for some libraries, because the memory requested inside this function is not released internally because of where it is allocated, it is necessary to release the memory at the top level, otherwise it will result in a memory leak, which will be introduced in the test cases that follow. In short, this function you can consider not to use, directly in the top allocation of memory, initialization, free memory can be.

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.