-
Define the struct:
typedef struct _legaladdress_{
char szip[16];
char szreserve[32];
legaladdress, *plegaladdress;
Define pointer variables:
plegaladdress plegaladdress = null;
Definitely allocate space when using it directly:
plegaladdress = (plegaladdress)malloc(sizeof(legaladdress));
You don't need to allocate space in the following situations:
void func1(char* pbuffer){
plegaladdress plegaladdress = null;
plegaladdress = (plegaladdress)pbuffer;Since pbuffer already has space, just point a pointer of type plegaladdress to it. **。
-
A struct is just a data type that you define, and it's a little more complicated. The time of memory allocation is the same as that of normal variables.
-
In C++, both structs and classes are data structures used to define custom types. They have some similarities, but there are also some differences.
Memory allocation: In a struct, all members are public, so they are allocated contiguous in memory.
In a class, members can be declared private or protected, which means they may not be distributed contiguous in memory. In addition, a class can also contain virtual functions, which means that it will have a virtual function table pointer that stores the virtual function address of the class.
Member's call:
In structs, you can use the point operator (.) or the arrow operator (-> to access the members of the struct. For example:
struct point
static int getvalue()
int myclass::s_value = 0;
int main()
myclass::setvalue(5);
int x = myclass::getvalue();
std::cout In this example, the MyClass class has a static variable S value and three static methods: setvalue, getvalue, and constructor. You can use ::
The operator calls these static methods outside of the class, as shown in the main function.
Note that static variables must be defined and initialized outside of the class. In the example above, the static variable s value is defined as an int type and initialized to 0.
Also note that static methods can't access non-static members of the class. For example, if the myclass class has a non-static variable m value, it cannot be accessed in the static method.
-
In Linux, there are three ways to allocate C++ memory
1) Allocation from a static storage area. Memory is allocated when the program is compiled, and this memory exists for the entire duration of the program. For example, global variables, static variables.
2) Create on the stack. When a function is executed, the storage units of local variables in the function can be created on the stack, and these storage units are automatically released at the end of the function execution. Stack memory allocation operations are built into the processor's instruction set and are highly efficient, but the amount of memory allocated is limited.
3) Allocation from the heap, also known as dynamic memory allocation. The program uses malloc or new to request as much memory as it needs when it runs, and the programmer is responsible for when to free up the memory with free or delete. The lifetime of dynamic memory is up to us and is very flexible to use, but it also has the most problems.
Common Memory Errors:
The memory allocation was unsuccessful, but it was used.
The memory allocation succeeded, but it was referenced before it was initialized.
The memory allocation was successful and initialized, but the operation crossed the boundaries of the memory.
For example, when using arrays, the subscript "1 more" or "1 less" often occurs. Especially in the for loop statement, the number of loops can easily be mistaken, causing the array operation to go out of bounds. )
Forgot to free up memory, causing memory leakage.
Released memory but continued to use it.
-
Two arguments are required <> the realloc call.
In addition, the result returned by the function needs to be detected before it can be used. If realloc fails to apply for memory, null is returned
-
Why do you have to use realloc, isn't it fragrant to use malloc or calloc, which you are very familiar with?
This problem requires understanding that the system allocates memory to static variables when compiling, and the memory units occupied by them are not released after the function call ends, that is, the value of the variable is the value of the previous function call at the next function call. >>>More