c++;内存管理伙伴系统内存地址问题 我试图用C++实现一个好友存储系统,但我遇到了一些问题。我给数组中的元素分配了一个内存地址,main中存储的内存地址与函数返回的内存地址不同。我相信存储地址时出错,将其重置为“有效”位置,但我不确定哪一个是“正确”地址。代码和输入张贴在下面 void buddy_manager(memoryNode* root, process* processes, int num){ int i = 0, count = 0, removed = 0, j = 0; process running[num]; while(removed < 5){ if(i % 50 == 0 && count<5){ cout << "Adding process: " << count << endl; running[count] = processes[count]; running[count].space = my_malloc(root, running[count].memory * 1000); printf("Made new node @ %p \n", running[count].space); count++; } //Check to remove the process from the list omitted } char* my_malloc (memoryNode* root, int size) { //If there are child nodes and the current node is not occupied, visit them! if(root->left != NULL && root->right != NULL && root->occupied == 0){ if(my_malloc(root->left, size) == NULL){ my_malloc(root->right, size); } } else{ //we are at a leaf node if(root->max_size >= size && root->max_size/2 < size && root->left == NULL && root->right == NULL && root->occupied == 0){ //if the node is good for insertion{ printf("address of new node: %p with slize: %d and element size: %d \n", root->start, root->max_size, size); root->occupied = 1; return root->start; } else if(!(root->max_size > size && root->max_size/2 < size) && root->max_size > 0 && root->occupied == 0 && root->left == NULL && root->right == NULL) { //Grow the tree root->left = new memoryNode(root->start, root->max_size/2, NULL, NULL, 0); root->right = new memoryNode((char*)(root->start + (root->max_size)/2), root->max_size/2, NULL, NULL, 0); //printf("Made new nodes at %p and %p \n", root->left->start, root->right->start); my_malloc(root->left, size); } else{ return NULL; } } }

c++;内存管理伙伴系统内存地址问题 我试图用C++实现一个好友存储系统,但我遇到了一些问题。我给数组中的元素分配了一个内存地址,main中存储的内存地址与函数返回的内存地址不同。我相信存储地址时出错,将其重置为“有效”位置,但我不确定哪一个是“正确”地址。代码和输入张贴在下面 void buddy_manager(memoryNode* root, process* processes, int num){ int i = 0, count = 0, removed = 0, j = 0; process running[num]; while(removed < 5){ if(i % 50 == 0 && count<5){ cout << "Adding process: " << count << endl; running[count] = processes[count]; running[count].space = my_malloc(root, running[count].memory * 1000); printf("Made new node @ %p \n", running[count].space); count++; } //Check to remove the process from the list omitted } char* my_malloc (memoryNode* root, int size) { //If there are child nodes and the current node is not occupied, visit them! if(root->left != NULL && root->right != NULL && root->occupied == 0){ if(my_malloc(root->left, size) == NULL){ my_malloc(root->right, size); } } else{ //we are at a leaf node if(root->max_size >= size && root->max_size/2 < size && root->left == NULL && root->right == NULL && root->occupied == 0){ //if the node is good for insertion{ printf("address of new node: %p with slize: %d and element size: %d \n", root->start, root->max_size, size); root->occupied = 1; return root->start; } else if(!(root->max_size > size && root->max_size/2 < size) && root->max_size > 0 && root->occupied == 0 && root->left == NULL && root->right == NULL) { //Grow the tree root->left = new memoryNode(root->start, root->max_size/2, NULL, NULL, 0); root->right = new memoryNode((char*)(root->start + (root->max_size)/2), root->max_size/2, NULL, NULL, 0); //printf("Made new nodes at %p and %p \n", root->left->start, root->right->start); my_malloc(root->left, size); } else{ return NULL; } } },memory,c++11,Memory,C++11,A您可以看到,第一个节点很好,但其余节点的最后两个数字将重置为00 如果您使用的是32位系统,那么您可能会胡乱猜测: 进程结构的大小不是4字节的倍数。因此,运行[num]数组的进程的第二个元素中的空格字段没有正确地与4字节地址对齐 在这个结构的末尾添加4-sizeof(process)%4字节应该可以解决这个问题 如果您使用的是64位系统,这是一种猜测: 进程结构的大小不是8字节的倍数。因此,运行[num]数组的进程的第二个元素中的空格字段没有正确地与8字节地址对齐 在这个结构的末尾添加8-s

A您可以看到,第一个节点很好,但其余节点的最后两个数字将重置为00

如果您使用的是32位系统,那么您可能会胡乱猜测:

进程
结构的大小不是4字节的倍数。因此,运行[num]数组的
进程的第二个元素中的
空格
字段没有正确地与4字节地址对齐

在这个结构的末尾添加
4-sizeof(process)%4
字节应该可以解决这个问题



如果您使用的是64位系统,这是一种猜测:

进程
结构的大小不是8字节的倍数。因此,运行[num]
数组的
进程的第二个元素中的
空格
字段没有正确地与8字节地址对齐

在这个结构的末尾添加
8-sizeof(process)%8
字节应该可以解决这个问题


更新:

如果您已验证上述问题不是问题的原因,则在从函数
my_malloc
返回之前,请打印
root->start
,并确保它始终与4(或8)字节对齐

我还注意到,函数
my_malloc
并非在所有情况下都返回值,这可能会导致未定义的行为(我不明白您是如何设法编译它而不出现错误)


因此,在这个函数中,在
my_malloc(root->left,size)
之前添加
return
running[count].space和
root->start
的数据类型是什么,结果是16个字节。我如何确保它与4个字节的地址对齐?返回时它确实没有正确对齐。@jtrevag:很难说,因为您没有指定
memoryNode
构造函数,但问题很可能出在
新的memoryNode((char*)(root->start+(root->max_size)/2),…)
。我能够修复我的内存地址。当我为整个节点集分配空间时,我使用的是10000000而不是10240000。因此,当节点被拆分时,它们将使用奇怪的偏移地址。现在一切都按计划进行了。谢谢你的帮助!不幸的是,事情仍然没有按预期进行。你是对的,问题出在哪里,但我仍然不确定如何解决它。
address of new node: 0xb6d86008 with size: 156250 and element size: 94000 
Made new node @ 0xb6d86008 
Adding process: 1
address of new node: 0xb6dd24bc with size: 312500 and element size: 193000 
Made new node @ 0xb6dd2400 
Adding process: 2
address of new node: 0xb6e1e970 with size: 156250 and element size: 106000 
Made new node @ 0xb6e1e900 
Adding process: 3
address of new node: 0xb6dc8c25 with size: 39062 and element size: 26000 
Made new node @ 0xb6dc8c00 
Adding process: 4
address of new node: 0xb6e44bca with size: 19531 and element size: 18000 
Made new node @ 0xb6e44b00