2014-09-28 52 views
0
void *memory; 
unsigned int b=65535; //1111 1111 1111 1111 in binary 
int i=0; 

memory= &b; 

for(i=0;i<100;i++){ 
    printf("%d, %d, d\n", (char*)memory+i, *((unsigned int *)((char *) memory + i))); 
} 

我想了解一件事。用指针存储的数据

(字符*)存储器+ I - 在范围2686636打印出ADRESS - 2686735.

,当我存储65535与存储= & B本应在ADRESS 2686636和2686637 存储此数因为每ADRESS是只是一个字节,以便8个二进制字符,从而当我打印出来

*((无符号整型*)((字符*)存储器+ i))的本应打印2686636,255和2686637,255

,而不是它,它打印2686636,65535和2686637,

我想实现内存分配的随机数。这是学校项目。这应该代表记忆。一个地址应该是一个字节,因此标题将为2686636-2586639(块的大小为4个字节)和2586640(1字节的char为空闲或使用的存储器标志)。有人能解释给我的感谢。

感谢您的回答。

void *memory; 
void *abc; 

abc=memory; 
for(i=0;i<100;i++){ 
*(int*)abc=0; 
abc++; 
} 

*(int*)memory=16777215; 

for(i=0;i<100;i++){ 
    printf("%p, %c, %d\n", (char*)memory+i, *((char *)memory +i), *((char *)memory +i)); 
} 

输出

0028FF94,  , -1 
0028FF95,  , -1 
0028FF96,  , -1 
0028FF97, , 0 
0028FF98, , 0 
0028FF99, , 0 
0028FF9A, , 0 
0028FF9B, , 0 

,我认为它的工作原理。 255只有一个-1,65355 2次-1和16777215 3次-1。

+0

只删除unsigned int?现在输出是2686636,-1和2686637,-1 – 2014-09-28 19:07:57

+0

我这样做printf(“%p,%c,%d \ n”,(char *)内存+ i,*((char *)内存+ i), *((char *)memory + i));它给出了2686636,-1,但我注意到,当我使b = 65535第2个​​地址是空格,-1但是当我使b = 4294967295第4个地址是空格,-1和4294967295是32位,所以我认为它以某种方式工作。 – 2014-09-28 19:24:00

+0

我改变了代码,我认为它现在正在工作chceck我把它贴在最后的问题 – 2014-09-28 20:03:35

回答

0

我没有在我的评论昨天提到这一点但很显然,从0到100的for循环超出了无符号整数的大小。

我简单地忽略了代码中的一些显而易见的问题,并尝试提出您提出的实际问题的提示(难以做到比方便得多:-))。不幸的是我昨天没有时间完成这个任务。所以,有一天推迟我的提示给你。

尽量避免对特定类型有多大的假设(如2字节或4字节)。即使您的假设现在成立,如果您切换编译器或切换到另一个平台,它可能会改变。因此,在整个代码中使用sizeof(type)。对于这个更长的讨论,你可能想看看:size of int, long a.s.o. on Stack Overflow。该标准仅规定某种类型应该能够容纳的范围(0-65535表示无符号整数),因此仅适用于类型的最小尺寸。这意味着int的大小可能(并且最终)大于2个字节。除了基本类型sizeof还可以帮助您计算由于内存对齐而导致的结构大小& &通过简单查看其属性,打包结构的大小可能与您“期望”的大小不同。所以sizeof运营商是你的朋友。

确保在printf中使用正确的格式。

由于结果取决于指针的类型(显然取决于您添加的整数的值),请小心使用指针算术和投射。

I.e.

(无符号整数*)存储器+ 1!=(无符号字符*)存储器+ 1

(无符号整数*)存储器+ 1 ==(无符号字符*)存储器+ 1个*的sizeof(无符号整数)

下面是我会怎么写代码:

//check how big is int on our platform for illustrative purposes 
printf("Sizeof int: %d bytes\n", sizeof(unsigned int)); 

//we initialize b with maximum representable value for unsigned int 
//include <limits.h> for UINT_MAX 
unsigned int b = UINT_MAX; //0xffffffff (if sizeof(unsigned int) is 4) 

//we print out the value and its hexadecimal representation 
printf("B=%u 0x%X\n", b, b); 

//we take the address of b and store it in a void pointer 
void* memory= &b; 

int i = 0; 

//we loop the unsigned chars starting at the address of b up to the sizeof(b) 
//(in our case b is unsigned int) using sizeof(b) is better since if we change the type of b 
//we do not have to remember to change the sizeof in the for loop. The loop works just the same 
for(i=0; i<sizeof(b); ++i) 
{ 
    //here we kept %d for formating the individual bytes to represent their value as numbers 
    //we cast to unsigned char since char might be signed (so from -128 to 127) on a particular 
    //platform and we want to illustrate that the expected (all bytes 1 -> printed value 255) occurs. 
    printf("%p, %d\n", (unsigned char *)memory + i, *((unsigned char *) memory + i)); 
} 

我希望你会发现这个有帮助。祝你的学校作业顺利,我希望你能学到一些你现在和未来可以使用的东西:-)。

1

在你的程序中,似乎b的地址是2686636,当你将写入(char *)内存+ i或(char *)& b + i时,这意味着这个指针指向char,所以当你添加一个它将跳转到只有一个存储器地址,即2686637等,直到2686735(即(char *)2686636 + 99)。


现在,当你提领即*((unsigned int的*)((字符*)内存+ I))),你会得到的内存地址的值,但你给的值只为b (其地址是2686636)。所有其他内存地址都有你正在打印的垃圾值。


所以首先你要一些数据在地址的其余部分(2686637至2686735) 好运存储.. 我希望这将有助于