简体   繁体   English

malloc为C中的无符号int分配零字节

[英]malloc zero bytes for unsigned int in C

#include<stdio.h>
#include<stdlib.h>

void main(){

unsigned int *p, a[25];
unsigned char *s = "goodcoffee";
unsigned int size, size1, size2, test_variable;

size1 = sizeof(unsigned int);
size2 = sizeof(unsigned int *);
size = sizeof(unsigned int)/sizeof(unsigned int *);

p = malloc(sizeof(unsigned int)/sizeof(unsigned int*));

test_variable = 0xFFFFFFFF;
*p = 0xFFFFFFFF;

strcpy(a,s);

printf("Size of int: %d , Size of int *: %d, Size: %d, Test variable:                     %d\n",size1,size2,size,test_variable);

printf("%s %d \n", a, *p);

return;

}

Hi, I am trying to understand why the below mentioned behavior is seen. 嗨,我试图理解为什么看到以下提到的行为。 I need your help to understand what happens with this code. 我需要您的帮助,以了解此代码会发生什么。 Here malloc is allocating zero bytes. 在这里,malloc分配了零字节。 (This was an interview question). (这是一个面试问题)。

output: 输出:

 size of int: 4,  size of int *: 4, size:1 , Test_variable: -1
 goodcoffee -1

I could not understand why Test_variable is showing -1 though it is an unsigned int. 我不明白为什么Test_variable显示-1,尽管它是一个无符号的int。 This happens only with the last nibble. 仅在最后一次轻咬时才会发生这种情况。 I tried giving range of value from 0 to F as last nibble. 我尝试给出从0到F的值范围,作为最后一个半字节。 code was giving different negative values. 代码给出了不同的负值。 But if I get rid of last nibble, ieTest_variable is 0xFFFFFFF, the output was 268435455. 但是,如果我摆脱了最后的蚕食,即Test_variable为0xFFFFFFF,则输出为268435455。

Here malloc is allocating zero bytes. 在这里,malloc分配了零字节。

Malloc is not allocating 0 bytes, its allocating 1 bytes for your machine.. as Malloc没有分配0字节,而是为您的机器分配1字节。

size = sizeof(unsigned int)/sizeof(unsigned int*) //gives 1

so, p is pointing to 1 byte 因此,p指向1个字节

and

*p = 0xFFFFFFFF;

you are trying to store a 4 byte number in p while p is pointing at a 1 byte allocated space. 你想存储4字节数p ,而p是在指向1字节分配的空间。 Its undefined behaviour. 其不确定的行为。

Printf format %d is used to print a signed integer. Printf格式%d用于打印有符号整数。 Use %u to print an unsigned integer. 使用%u打印无符号整数。 The representation of the unsigned integer 0xFFFFFFFF for a signed integer is -1. 有符号整数的无符号整数0xFFFFFFFF的表示形式为-1。

Read more about malloc(3) ; 阅读有关malloc(3)的更多信息; it wants the number of bytes (not of data items), and it could fail. 它需要字节数(不是数据项),并且可能失败。 So code, if p is supposed to contain one unsigned integer: 所以代码,如果p应该包含一个无符号整数:

p = malloc(sizeof(unsigned int));
if (!p) { perror("malloc"); exit(EXIT_FAILURE); };
*p = 0xFFFFFFFF;

Your code incorrectly called malloc with either 0 or 1 byte which is not enough for an unsigned int (on my Debian/x86-64 machine, sizeof(unsigned int) is 4, and pointers want 8 bytes, so sizeof(unsigned int*) is 8, so 4/8 is 0). 您的代码错误地调用了0或1 个字节的 malloc ,这对于一个unsigned int是不够的(在我的 Debian / x86-64机器上, sizeof(unsigned int)是4,指针需要8个字节,所以sizeof(unsigned int*)是8,所以4/8是0)。

BTW, if your system has it, use valgrind . 顺便说一句,如果您的系统有,请使用valgrind Don't forget to compile with all warnings & debug info ( gcc -Wall -Wextra -g ). 不要忘记编译所有警告和调试信息( gcc -Wall -Wextra -g )。 Then use the debugger ( gdb ) 然后使用调试器gdb

The negative value is stored in 2's compliment form in memory. 负值以2的补码形式存储在内存中。

In your case test_variable = 0xFFFFFFFF; 在您的情况下,test_variable = 0xFFFFFFFF; represent -1 in decimal. 代表-1(十进制)。

That is what printed on the console. 这就是控制台上打印的内容。

Here its how it is done 这是怎么做的

1 represent 00000001 in binary
1's complement of 1 is 11111110
adding 1 gives 11111111=> represent ffff.

No of bits depends on the memory layout used by the system. 位数取决于系统使用的内存布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM