How do we use malloc

cht9931 注册会员
2023-02-28 19:06
  • helps you find a similar problem, you can look at: https://ask.csdn.net/questions/7732764 < / font > < / a > < / li > < li > this post is good also, You can see the < a href = "https://blog.csdn.net/weixin_44291161/article/details/106036679?utm_source=csdn_ai_ada_ask_robot" target="_blank"> Common errors when using malloc
  • You can also look at allocates memory - malloc < / font > < / a > < / li >
B8399083 注册会员
2023-02-28 19:06

malloc allocates a bit of memory.
A,b, and c all have the same amount of memory, c this refers to the memory space, it's assigned at the end, All a, b,c values are values that point to the same memory space, and all are equal

doudou0523 注册会员
2023-02-28 19:06


< !- - - - - >
cxfchw 注册会员
2023-02-28 19:06

is used to partition memory, nothing fancy
It's not assigned at first, it's going to be a random number
but all three abc Pointers point to the same memory, So when you change the value of a you change it
when you change the value of b you change the value of c you change it
and when you change the value of c you change it
and a=b you assign b's address to a, it's the same address, it's no assignment, it doesn't work
so the value inside is really the last time you assigned C, Is 3,
then the abc value is actually 3
-=-=-=-== =
here dynamically apply for memory, except when not free will cause memory leakage, there is no difference in the use of
you can directly understand as
int d;
a=b=c=& d;

cqwrmjld 注册会员
2023-02-28 19:06

Write based on Monster group and GPT:
malloc functions can be used to dynamically allocate memory in order to allocate the required size of memory when the program is running. The function prototype is as follows:

void* malloc(size_t size);

Where, size indicates the number of bytes to be allocated, and the return value is the first address of the allocated memory. If allocation fails, NULL is returned.

For example, to allocate an array of type int, you would use the following code:

int* arr;
int n = 10;
arr = (int*) malloc(n * sizeof(int));

Here we first define a pointer to int arr, and then use malloc to allocate n * sizeof(int) bytes of memory, that is, the amount of memory that can hold n int variables. Since malloc returns a pointer to type void*, it needs to be cast to a pointer to type int.

When the allocated memory is used up, it should be freed using the free function to avoid memory leaks. For example, to free the memory allocated in the above code, you can use the following code:


Here frees up the memory space to which the pointer arr points.

About the Author

Question Info

Publish Time
2023-02-28 19:06
Update Time
2023-02-28 19:06

Related Question


RTKLIB Use Problem