将 `int` 添加到地址会导致 int 被添加 4 次 [英] Adding `int` to address causes int to be added 4 times

查看:97
本文介绍了将 `int` 添加到地址会导致 int 被添加 4 次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于有关操作系统功能的课程,我们必须为特定大小的结构编写 malloc/free 实现.我们的想法是在该块的前几个地址中存储开销,例如我们的代码必须在其中工作的指定(静态)内存块的开始和结束.

For a course about the functioning of operating systems, we had to write a malloc/free implementation for a specific sized struct. Our idea was to store the overhead, like the start and end of the specified (static) memory block our code has to work in, in the first few addresses of that block.

但是,最后一个内存槽的计算出了问题;我们将所有可用内存插槽的大小与第一个可用内存插槽的地址相加,以确定最后一个插槽是什么.但是,当在currentslot的地址上加上int sizeofslots时,实际上是在这个地址上加上了4次sizeofslots.相关代码如下:

However, something went wrong with the calculation of the last memory slot; We're adding the size of all usable memory slots to the address of the first usable memory slot, to determine what the last slot is. However, when adding the int sizeofslots to the address of currentslot, it actually adds sizeofslots 4 times to this address. Here's the relevant code:

/* Example memory block*/
/* |------------------------------------------------------------------------------|*/
/* | ovr | 1 t | 0   | 1 t | 1 t | 1 t | 0   | 0   | 0   | 0   | 0   | 0   | 0   |*/
/* |------------------------------------------------------------------------------|*/
/* ovr: overhead, the variables `currentslot`, `firstslot` and `lastslot`.        
 * 1/0: Whether or not the slot is taken.      
 * t: the struct 
 */

/* Store the pointer to the last allocated slot at the first address */
currentslot = get_MEM_BLOCK_START();
*currentslot = currentslot + 3*sizeof(void *);

/* The first usable memory slot after the overhead */
firstslot = currentslot + sizeof(void *);
*firstslot = currentslot + 3*sizeof(void *);

/* The total size of all the effective memory slots */
int sizeofslots = SLOT_SIZE * numslots;

/* The last usable slot in our memory block */
lastslot = currentslot + 2*sizeof(void*);
*lastslot = firstslot + sizeofslots;
printf("%p + %i = %p, became %p\n", previous, sizeofslots, previous + (SLOT_SIZE*numslots), *lastslot);

我们认为这与 4 个字节的整数有关,但我们仍然不明白这里发生了什么;谁能解释一下?

We figured it had something to do with integers being 4 bytes, but we still don't get what is happening here; Can anyone explain it?

推荐答案

当你把一个整数加到一个指针上时,它会增加很多步长(即 myPointer + x 会增加 x*sizeof(x).如果没有发生这种情况,可能会有未对齐的整数,这是许多处理器架构的一个错误,至少可以说会导致一些时髦的行为.

When you add an integer to a pointer, it increments by that many strides (i.e. myPointer + x will increment by x*sizeof(x). If this didn't happen, it would be possible to have unaligned integers, which is many processor architectures is a fault and will cause some funky behaviour, to say the least.

以下内容为例

char* foo = (char*)0x0; // Foo = 0
foo += 5;               // foo = 5

short* bar = (short*)0x0; // Bar = 0; (we assume two byte shorts)
bar += 5;                 // Bar = 0xA (10)

int* foobar = (int*)0x0;  // foobar = 0; (we assume four byte ints)
foobar += 2;              // foobar = 8; 

char (*myArr)[8];         // A pointer to an array of chars, 8 size
myArr += 2;               // myArr = 0x10 (16). This is because sizeof(char[8]) = 8;

这篇关于将 `int` 添加到地址会导致 int 被添加 4 次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆