0
Follow
5
View

C variable memory mechanism on Codeblock

jake7866891 注册会员
2023-02-25 23:22

Part of the answer refers to GPT, GPT_Pro better solve the problem
A: This is an interesting problem. The Codeblock debugger actually has a problem with the memory order of variables. It should be storing a and ch in the wrong place. We know that in C language, variables are red in memory from low to high address. That is, the variables defined first should occupy the lower address space in memory, and the variables defined later should occupy the higher address space.

Let's look at the problem with Codeblock17 12debug. The first red box is four bytes of a, the second red box is one byte of ch, and the middle 32 00 00 should be the control byte. As can be seen from here, in fact, the memory storage locations of a and ch are reversed, which should be defined as a at the low address and ch at the high address.

To solve this problem, we can modify the memory order through the code. We can use the C language malloc function to do this. For example:

int *a = (int *)malloc(sizeof(int));  
char *ch = (char *)malloc(sizeof(char));  

Using the malloc function to separate a and ch memory space, can normally define the variable memory order.
If the answer is helpful, please accept it.

About the Author

Question Info

Publish Time
2023-02-25 23:22
Update Time
2023-02-25 23:22

Related Question

python可以在同一台电脑上下32bit和64bit吗 pycharm是不是只能在64bit用

为什么异步方法从API请求解码JSON数据时会抛出错误?斯威夫特

发送许可列表到Frontend(API)

被请求的资源上没有'Access-Control-Allow-Origin'标头.如何修复此错误[重复]

mongodb数据库插入问题

python网页抓取有问题[关闭]

python中xpath爬取问题

小程序通过获取的值查在另一个json中的数据

python print语句内容不显示

PYTHON列表打印语句