C语言 在 malloc 的内存上使用 sizeof()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2259890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using sizeof() on malloc'd memory
提问by Michael Dickens
Possible Duplicate:
newbie questions about malloc and sizeof
I am trying to read strings into a program. When I noticed that the strings were sometimes being corrupted, I tried the following code:
我正在尝试将字符串读入程序。当我注意到字符串有时被损坏时,我尝试了以下代码:
void *mallocated = malloc(100);
printf("sizeof(mallocated) = %d\n", sizeof(mallocated));
According to my program, the size of mallocatedwas 8, even though I allocated 100 bytes for it. Because of this, whenever I try to store a string longer than 8 bytes, everything after the 8th byte will sometimes disappear. Why is this happening, and how can I prevent it?
根据我的计划,规模mallocated是8,即使我被分配100个字节它。因此,每当我尝试存储超过 8 个字节的字符串时,第 8 个字节之后的所有内容有时都会消失。为什么会发生这种情况,我该如何预防?
回答by
Because the size of the "string" pointer is 8 bytes. Here are some examples of using sizeof()with their appropriate "size". The term size_of()is sometimes deceiving for people not used to using it. In your case, the size of the pointer is 8 bytes.. below is a representation on a typical 32-bit system.
因为“字符串”指针的大小是 8 个字节。以下是使用sizeof()适当“大小”的一些示例。size_of()对于不习惯使用它的人来说,该术语有时具有欺骗性。在您的情况下,指针的大小为 8 个字节。以下是典型 32 位系统的表示。
sizeof (char) = 1
sizeof (double) = 8
sizeof (float) = 4
sizeof (int) = 4
sizeof (long) = 4
sizeof (long long) = 8
sizeof (short) = 2
sizeof (void *) = 4
sizeof (clock_t) = 4
sizeof (pid_t) = 4
sizeof (size_t) = 4
sizeof (ssize_t) = 4
sizeof (time_t) = 4
You are leaving out how you are determining your string is disappearing (char array). It is probably being passed to a function, which you need to pass the explicit length as a variable or track it somewhere. Using sizeof()won't tell you this.
您忽略了如何确定字符串正在消失(字符数组)。它可能被传递给一个函数,您需要将显式长度作为变量传递或在某处跟踪它。使用sizeof()不会告诉你这一点。
See my previous questionabout this and you'll see even my lack of initial understanding.
回答by Alex B
In C89, sizeofoperator only finds the size of a variablein bytes at compile time (in this case a voidpointer of 8 bytes). It works the way you'd expect it to work on plain arrays, because their size is known at compile time.
在 C89 中,sizeof运算符仅在编译时以字节为单位查找变量的大小(在本例中void为 8 字节的指针)。它以您期望它在普通数组上工作的方式工作,因为它们的大小在编译时是已知的。
char arr[100]; // sizeof arr == 100
char *p = arr; // sizeof p == 4 (or 8 on 64-bit architectures)
char *p = malloc(100); // sizeof p == 4 (or 8). Still!
To know the size of heap-allocated memory, you need to keep track of it manually, sizeofwon't help you.
要知道堆分配内存的大小,您需要手动跟踪它,sizeof对您没有帮助。
回答by Kyle Lutz
sizeofreturns the size of the pointer (void *) that you gave it, not the size of the memory you allocated. You would have to store the size of the memory in a separate variable if you want to use it later.
sizeof返回void *您给它的指针 ( ) 的大小,而不是您分配的内存大小。如果以后要使用它,则必须将内存的大小存储在单独的变量中。
回答by Mawg says reinstate Monica
You cannot. As pointed out, you can get the size of the void *mallocated, but that does not tell you much.
你不能。正如所指出的,您可以获得void *错误分配的大小,但这并不能告诉您太多。
You cannotget the size of the malloed *mallocated. That is to say, there is no function call to return 100 (in your example) - unless you write your own memory management routines.
您无法获得 malloed 的大小*mallocated。也就是说,没有函数调用返回 100(在您的示例中)-除非您编写自己的内存管理例程。
Simplest is just to remember it somewhere ... maybe ....
最简单的就是在某处记住它......也许......
stuct {
void *data;
unsigned int size
} myStructureWhichRemebersItsSize;
myStructureWhichRemebersItsSize *someData;
someData.data = malloc(100); // and check for NULL
someData.size = 100;
回答by Potatoswatter
void*is the type of a location in memory if you don't know what it contains. It should be avoided.
void*如果您不知道它包含什么,则是内存中位置的类型。应该避免。
char*is the type of a value which points to some location in memory which holds a char. Identifying a location in memory takes eight bytes.
char*是指向内存中某个位置的值的类型,该位置包含char. 识别内存中的位置需要 8 个字节。
sizeoftells you how many bytes a particular type takes. Not how many were allocated with mallocbut just how much memory compiler knows the type shouldtake. Applying sizeofto values is often considered bad style, and as someone else mentioned here, sometimes invokes smart behavior in C99.
sizeof告诉您特定类型需要多少字节。不是分配malloc了多少,而是编译器知道类型应该占用多少内存。应用sizeof到值通常被认为是不好的风格,正如这里其他人提到的那样,有时会在 C99 中调用智能行为。
char[100]the type of a value which holds 100 chars. char[100] a;is a string of 100 chars on the stack.
char[100]包含 100 个字符的值的类型。char[100] a;是char堆栈上的一个 100秒的字符串。
char(*)[100]is the type of a pointer to a value that holds 100 chars. char(*b)[100];makes bpoint to 100 chars, possibly on the heap. What you probably want is
char(*)[100]是指向包含 100 个字符的值的指针的类型。char(*b)[100];品牌b指向100个字符,可能在堆上。你可能想要的是
char (*s)[100] = malloc( sizeof( char[100] ) );
printf( "%u byte pointer to %u bytes of size %u elements\n",
sizeof s, sizeof *s, sizeof **s );

