开发者

pointer to a structure

开发者 https://www.devze.com 2023-02-07 00:29 出处:网络
this will give a proper output even though i have not allocated memory and have declared a pointer to structure two inside main

this will give a proper output even though i have not allocated memory and have declared a pointer to structure two inside main

    struct one
    {
    char x;
    int y;
    };

    struct two
    {
    char a;
    struct one * ONE;
    };

    main()
    {
    struct two *TWO;
    scanf("%d",&TWO->ONE->y);
    printf("%d\n",TWO->ONE->y);
    }

but when i declare a pointer to two after the structure outside main i w开发者_JAVA技巧ill get segmentation fault but why is it i don't get segmentation fault in previous case

    struct one
    {
    char x;
    int y;
    };

    struct two
    {
    char a;
    struct one * ONE;
    }*TWO;


    main()
    {
    scanf("%d",&TWO->ONE->y);
    printf("%d\n",TWO->ONE->y);
    }


In both the cases TWO is a pointer to a object of type struct two.

In case 1 the pointer is wild and can be pointing anywhere.

In case 2 the pointer is NULL as it is global.

But in both the cases it a pointer not pointing to a valid struct two object. Your code in scanf is treating this pointer as though it was referring to a valid object. This leads to undefined behavior.


Because what you are doing is undefined behaviour. Sometimes it seems to work. That doesn't mean you should do it :-)

The most likely explanation is to do with how the variables are initialised. Automatic variables (on the stack) will get whatever garbage happens to be on the stack when the stack pointer was decremented.

Variables outside functions (like in the second case) are always initialised to zero (null pointer for pointer types).

That's the basic difference between your two situations but, as I said, the first one is working purely by accident.


When declaring a global pointer, it will be initialized to zero, and so the generated addresses will be small numbers that may or may not be readable on your system.

When declaring an automatic pointer, its initial value is likely to be much more interesting. It will be, in this case, whatever the run-time library left at that point on the stack prior to calling main(), or perhaps a left-over value from the compiler-generated stack-frame setup code. It is somewhat likely to be a saved stack pointer or frame pointer, which is a valid pointer if used with small offsets.

So anyway, the uninitialized pointer does have something in it, and one value leads to a fault while the other, for now, on your system, does not.

And that's because the segmentation fault is a mechanism of the OS and not the C language.

A fault is a block-based mechanism that allocates to itself and other programs some number of pages -- which are each several K -- and it protects itself and other program's pages while allowing your program free reign. You must stray outside of the block context or try to write a read-only page (even if yours) to generate a fault. Simply breaking a language rule is not necessarily enough. The OS is happy to let your program misbehave and act oddly due to its wild references, just as long as it only reads and writes (or clobbers) itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消