Struct pointer memory -- pointer array -- string pointer memory Application

Source: Internet
Author: User

The struct used a few days ago contains struct pointers, arrays, and pointers to strings. It is easy to make mistakes in this aspect, so now I will write out the error-prone content and share it with you for your convenience.


typedef struct {char name[50];char job[50];int age;int people_id;} peopleInfo;typedef struct {bool typeAdd;bool typeDel;int length;peopleInfo *info;char buildDate[64];char lastDate[64];char valueStr[256];} peopleObj;

The above are two structs. The peopleobj struct contains pointers and arrays of the previous struct. (at first, I used a pointer instead of an array, such as char * lastdate, char * valuestr, why should it be changed to an array at last)


Leleobj * meglobal;


If the above pointer definition is available, you must apply for memory for the pointer. Here I use

Meglobal = (peopleobj *) malloc (sizeof (peopleobj); here the memory application uses the malloc application of the c standard library. Char is 1 byte, Int Is 4 bytes, and pointer info is 4 bytes, so it is not the two bool types, A total of 256 + 64 + 64 + 4 + 4 = 392 bytes. C ++ of the bool type has no specification. You can check the information yourself. There is no specification for C ++ primer of Version 4. It is estimated that it is related to the compiler.

Note that only peopleinfo * info is used when applying for memory. The Info pointer in applies for 4 bytes of memory, and the variables in peopleinfo structure do not have memory, if you need to use it, you must apply for memory for them, such:

Meglobal-> info = (peopleinfo *) malloc (sizeof (peopleinfo) * 10); in this way, you can use the variables in info, and no error is reported.


Now I want to explain why I used arrays later:

At first, I defined the struct as follows:


typedef struct {char *name;char *job;int age;int people_id;} peopleInfo;typedef struct {bool typeAdd;bool typeDel;int length;peopleInfo *info;char *buildDate;char *lastDate;char *valueStr;} peopleObj;


In the structure, I am not sure about a lot of data, so I started to use pointers to strings.

The same as above, when you initialize meglobal = (peopleobj *) malloc (sizeof (peopleobj);, only memory is applied for those pointers, and the Pointer Points to no memory, so I use:


Meglobal-> Info-> job = (char *) malloc (sizeof (char) * 50 );

Meglobal-> Info-> name = (char *) malloc (sizeof (char) * 50 );


Meglobal-> builddate = (char *) malloc (sizeof (char) * 64 );
Meglobal-> lastdate = (char *) malloc (sizeof (char) * 64 );
Meglobal-> valuestr = (char *) malloc (sizeof (char) * 256 );


The above memory application is estimated to be the largest, so there is a lot of memory waste. We will not discuss it here. However, if you want to use the memory, you must make a similar request. Otherwise, the content pointed to by the pointer has no memory space. In subsequent program processing, if the above pointer is used, if it points to other memory, be sure to check whether a program exists in other programs is released. If it is released, the pointer used at the beginning is that the wild pointer does not point to this point. If you do not notice this, when you take the pointer directly, it will crash, therefore, you need to apply for memory or point it to a memory before using it, so that no error will occur.


Exercise caution when using pointers. I am too troublesome and start to make mistakes, so use arrays. When the array is defined, a piece of memory is allocated to it.


C ++ primer is not recommended for pointers and arrays. We recommend that you use vector containers and iterators instead. There are still many complicated usage of pointers. Learn more.

If there are any mistakes, please point out that they are learning from each other.



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.