A joke to understand the character array and dynamic memory allocation of C language

[Scenario 1] char funds[10000];

One day, I applied to my boss for a project payment, and the estimated cost was no more than 10,000 yuan. I took a piece of paper to my boss, and it said:

char funds[10000];

The boss looked at the application on the paper, couldn't help smiling, and said to me: This is as if you are applying for a project fund of 10,000 yuan from me! agree.

Me: Thank you boss for approval! Based on my long experience in the field, this project will not cost a lot of money, and I promise to return the balance to you after the project is completed!

Boss: But, let me be clear: If your project exceeds this budget, I will not give you any more funds!


(Unfortunately, man's calculation is not as good as heaven's calculation, the string is too long, exceeds the limit declared by the funds array, memory overflows, and the program terminates abnormally)


Me: Boss, I'm sorry, the actual project cost is 15,000 yuan, which is beyond the budget. What should I do?

Boss: You can figure it out.

I:……

(The project has failed)

[Scenario 2] char* funds = malloc(sizeof(char) * 10000);

After a while, I took on another project, which was conservatively estimated to cost at least 10,000 yuan.
With the unforgettable experience of the last time, we must be extra careful this time, so as not to make mistakes again.
So I decided to use dynamic allocation of memory to solve this problem.
I approached the finance department and submitted an application form that said

char* funds = malloc(sizeof(char) * 10000);

The person in the financial department looked at the application form and asked me with some doubts: "It's as if you want to apply for a project fund of 10,000 yuan from us! But if you need more funds, you need to apply again. !"

I thought about it and felt that this is still not flexible enough. It was decided to submit a supplemental application to the finance department for additional funding. So write the following on the application form:

funds = realloc(funds, 5000);

The person in the financial department received this application, looked at the amount, and asked a little confused: "This seems to be another application for 5,000 yuan from us. You won't spend all the project funds, right?"

I quickly explained: "No, no, it's just to make sure the project goes ahead! If we need more money, we just apply again and report the actual cost to the finance department in a timely manner."

The people in the financial department suddenly realized, and said with a smile: "So, you programmers really know how to play tricks!"

So I started working on the project and kept reporting the actual cost to the finance department.

With sufficient funds, the project can proceed smoothly. After the project is over, I will return the remaining project payment to the finance department and write clearly on the settlement sheet:

free(funds);

[Scenario 3] char* funds = calloc(10000, sizeof(char));

After a few days, I took another project and applied for funding from the Finance Department again using the method of dynamically allocating memory.
I went to the finance department and submitted an application form that said:

char* funds = calloc(10000, sizeof(char));

Finance Department: Why is it different this time from last time?

Me: This project is quite special and requires the use of brand new hundred-yuan bills. Do you have them here?

Finance Department: No. If you don't mind the old paper money, I can transfer the money to you right away. Are you in a hurry?

Me: The sooner the better.

Financial Department: Then wait for me, I will go to the bank counter to withdraw brand new banknotes now.

Me: OK, sorry for the trouble!

(The difference between malloc and calloc is:
malloc allocates memory immediately, but the content is random, not necessarily 0 bytes.
calloc first allocates memory, and then does one more step: clearing the memory space of the specified size.)


I hope this joke can give you a more vivid understanding of character arrays and dynamic memory allocation in C language.
 

Guess you like

Origin blog.csdn.net/Scott0902/article/details/131491540