简体   繁体   English

int指针进入void指针然后进入双指针

[英]int pointer into void pointer and then into double pointer

#include <stdio.h>
#include <string.h>

main()
{
    int an_int;
void *void_pointer = &an_int;
double *double_ptr = void_pointer;
*double_ptr = 10;

printf("%d", sizeof(*double_ptr));

}

Output of this program is 8. 该计划的输出为8。
How does this happens?? 这是怎么回事?
I mean double_ptr was pointing to 4 bytes memory but still output is 8 . 我的意思是double_ptr指向4字节内存,但仍然输出为8。
* Where does other 4 bytes comes from ??? * 其他4个字节来自哪里? This program should crash as other 4 bytes are not allocated to it. 该程序应该崩溃,因为没有分配其他4个字节。 * *
Please explain?? 请解释??

C is statically typed, the expression sizeof *double_ptr is solely computed according to these static type rules, namely double_ptr points to a double which has size 8 . C是静态类型的,表达式sizeof *double_ptr仅根据这些静态类型规则计算,即double_ptr指向大小为8double

This program has undefined behavior, because you are writing to a memory range that you don't "own". 此程序具有未定义的行为,因为您正在写入您不“拥有”的内存范围。 All bets are off, it might crash or not. 所有赌注都已关闭,可能会崩溃或不崩溃。 When doing such nasty conversions you are the responsible for that, not the compiler. 在进行这种讨厌的转换时,你应该对此负责,而不是编译器。

Edit, for nitpicks: 编辑,为挑剔:

  • functions without return type should not be tolerated by a conforming compiler. 一致的编译器不应容忍没有返回类型的函数。 increase the warning level. 提高警告水平。
  • when posting code here, please indent it properly 在此处发布代码时,请正确缩进
  • the result of the sizeof operator is not int but size_t an unsigned type. sizeof运算符的结果不是intsize_t是无符号类型。 Printing that with "%d" also has undefined behavior. 使用"%d"打印也具有未定义的行为。 Use "%zu" . 使用"%zu"

I mean double_ptr was pointing to 4 bytes memory but still output is 8. 我的意思是double_ptr指向4字节内存,但仍然输出为8。

This is because sizeof is evaluated based on the type or on the type of the expression. 这是因为sizeof是根据表达式的类型或类型进行评估的。 Moreover, in this case it can be fully evaluated at compile time, regardless of the pointer's value. 而且,在这种情况下,无论指针的值如何,都可以在编译时对其进行全面评估。

*Where does other 4 bytes comes from ??? *其他4个字节来自哪里?

They belong to a region of memory adjacent to the location of int . 它们属于与int位置相邻的内存区域。 This region is not allocated to the object that you were intended to write, so it is undefined behavior. 此区域未分配给您要写入的对象,因此它是未定义的行为。 Good chances are that you are writing into the pointer itself, which could happen if the compiler places void_pointer in memory immediately after an_int . void_pointer可能你正在写入指针本身,如果编译器在an_int之后立即将void_pointer放在内存中,则可能会发生这种情况。

This program should crash as other 4 bytes are not allocated to it. 该程序应该崩溃,因为没有分配其他4个字节。

Unfortunately, not all undefined behaviors lead to crashes. 不幸的是,并非所有未定义的行为都会导致崩溃。 This is a common case in C when an invalid program does not crash. 当无效程序没有崩溃时,这是C中的常见情况。 For example, all buffer overrun exploits rely on the ability of a program not to crash after exhibiting undefined behavior. 例如,所有缓冲区溢出漏洞都依赖于程序在显示未定义的行为后不会崩溃的能力。

sizeof is compile time operator. sizeof是编译时运算符。 so its checking the type of data that pointer may point (which is double 8 bytes) so the output is 8. 所以它检查指针可能指向的数据类型(这是双8字节),因此输出为8。

int main()
{
    int an_int;
void *void_pointer = &an_int;
double *double_ptr = void_pointer;
*double_ptr = 10;

printf("%d %d",*double_ptr,sizeof(*double_ptr));

} 

i have edited a bit. 我编辑了一下。 It crashes when it tries to access the memory using pointer. 它在尝试使用指针访问内存时崩溃。

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

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