0

To shorten, my issue is simply understanding why would this code:

int main() {
    typedef int* ASElement;
    int zero = 0;
    int one = 1;
    int two = 2;
    int three = 3;
    ASElement *elements = (int**)malloc(4 * sizeof(ASElement));
    *elements = (int*)malloc(4*sizeof(int));
    *(elements) = &zero;
    *(*(elements+1)) = one; //segementation fault here


    printf("%d", *(*(elements+1)));



    return 0;
}

not work?

5
  • 2
    *(*(elements+1)) = one; what does elements+1 point to? Commented Nov 22, 2019 at 12:41
  • 3
    *(elements) = &zero; <- here you overwrite the allocation of the previous line, since *elements and *(elements) are the same. What did you intend to do? perhaps *(*(elements)) = zero? Commented Nov 22, 2019 at 12:41
  • @Blaze - I have allocated the first pointer(*elements+0) at the array of pointers - elements, to point to the start of an array using malloc, so, should'nt *(*(elements+1)) be the value of the first item of that array? Commented Nov 22, 2019 at 12:45
  • @Ctx shouldnt *(elements) = &zero simply make the first item of the *(elements) array the value of zero? Commented Nov 22, 2019 at 12:47
  • 1
    @TomerAttali How could it? The right hand side is an address that is most definitely not zero. Commented Nov 22, 2019 at 12:53

1 Answer 1

2

Your program, as written, uses the contents at address elements+1 uninitialized when it does *(elements+1). You have never written at that address before, and it is inside a block allocated by malloc, so the value it contains is indeterminate and you are not allowed to use this value.

The crash you observed can be explained by the fact that when you executed the program, the contents of that memory location did not form a valid pointer. Perhaps the contents were zeroes, perhaps a number that did not happen to be a valid address for your program. You were lucky: the program might not have crashed.

Maybe you intended to write:

*(elements+1) = &one;

https://taas.trust-in-soft.com/tsnippet/t/cb539105

Sign up to request clarification or add additional context in comments.

5 Comments

Just getting things straight: *elements = (int*)malloc(4*sizeof(int)); - here, I have allocated the first pointer, at the array of int pointers, to point at a block of size 4 allocated by malloc. *(*(elements+1)) = one - here, should'nt it simple assign the value of the first item at the array *(elements+0) to the number one?
elements+1 is an address, the address of the second element of the array allocated by the first call to malloc. You have never written to this address, but you read from it. And then, you dereference what you have read. This cannot go well. I am not sure what you are trying to do but it cannot involve reading uninitialized array elements and accessing to the address they represent.
*(elements + 1) is an uninitialized value. *(*(elements+1))=…; uses an uninitialized value as address to write to.
Got it, thanks alot!. my de-referencing logic was flawed. *((*elements)+1) = one; was what I ment to do.
@TomerAttali I'm glad to hear this. Do not hesitate to validate your programs in the Tsnippet service, it is designed to detect all undefined behaviors every time, which is more helpful than relying on crashes to notice that something is wrong (that was the “You were lucky: the program might not have crashed.” part of my answer). With the change you suggest, for instance, the code increments the address of the variable zero, producing an invalid address that happens not to crash your program when you write to it: taas.trust-in-soft.com/tsnippet/t/c12c3e8a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.