2015.12.14 macro Definition enumeration dynamic memory allocation

Source: Internet
Author: User

Macro definition (#define)

The meaning and purpose of a macro definition: (traditionally , it starts with "K" )

1. In order to make some data meaningful.

            

2. Similar to inline functions (easy to use).

            

3. Switch the output log.

(Nonzero is true, 0 is "release phase", 1 is "development phase")

Enum (enum)

An enumeration type declaration defines a type name for a set of related symbolic constants. Enumeration is used for "multiple selection" scenarios where the program runs from a fixed number of "choices" that have been set at compile time.

  By default, the underlying type of each element in the enumeration is int. You can use a colon to specify another integer value type. The default is 0, which will be +1 above the former.

Dynamic memory allocation

  Automatically allocate memory: When you define a variable of the basic type, the system will automatically allocate memory for this variable, which exists on the heap. When the scope ends, the system will automatically reclaim this memory.

Dynamically allocating memory: the memory space that the developer requests to the system, the memory that is requested is on the stack, and the system does not automatically recover the memory after it is used as a bundle. This memory must be released by the developer himself . If it is not released, the memory leaks. the "free (void *)" function is used to release, it is worth noting that the parameters of the parameter must be a pointer, and is a dynamic memory allocation .

When do I need to allocate memory dynamically?

In the process of running the program, you need to save/record the corresponding data, but the memory is not prepared in advance, then you need to temporarily allocate memory dynamically.

Using functions:

  void *malloc (size_t);

size_t: The memory space that you want to request (bytes byte)

void *: The system initially does not know what data you need to store, the different data need the memory space is not the same, so the default is to give a void *, refers to all pointer types (char *, int *, struct *), when in use must be converted to the corresponding type of void *. If there is no successful request, the return value is null.

      so every time after the application from the space to check whether the successful opening up memory space .

Calculate Structure body Storage space

Rationale: If there are multiple data types inside a struct, align to the type that occupies the highest number of bytes in memory

typedef struct{

Char *name;

int age;

}person; //16

char * occupies 8 bytes, int occupies 4 bytes

So the age variable is automatically aligned to name. The whole occupies 16 bytes

typedef struct{

char name;

int age;

}person; //8

typedef struct{

char name[2];

int age;

}person; //8

typedef struct{

Char name[6];

int age;

}person; //12

The P.s.① pointer variable is assigned an initial value by default, or null if not, because it prevents the presence of a wild pointer .

try not to return pointers in the ② function .

18:52:16

2015.12.14 macro Definition enumeration dynamic memory allocation

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.