Click here to Skip to main content
15,909,742 members

Comments by Member 16116753 (Top 50 by date)

Member 16116753 19-Dec-23 12:52pm View    
I don't quite get it why taking a modulo from an address of Str[0]. Isn't array used to be that data is one after another. Shifting 1 byte shifts my array from one place to another. It doesn't matter what value is the memory it is placed on / value of address it rather takes first 4 free bytes and places int value in it. Same goes with char, it sees first free byte and places value.
Member 16116753 19-Dec-23 7:53am View    
I just wanted to emphasize that even if I write int* x = (char*) y; then if I write after that x + 1 it will still shift by 4 bytes and not by 1 byte so this char doesn't matter.
Member 16116753 18-Dec-23 13:16pm View    
okey but in your example str[0] would work ok printing the value of variable "i" but str[1] not even though it has 4 required bytes, str[0] actually also has 4 bytes because I see you've assumed that int is 4 bytes so from str[0] to str[3] it works ok but when you've from str[1] to str[4] in which str[4] is equal 0x0 you've said it would be segfail. Why ?

"Given the constraints above, one of the two printf statements will produce a seg fault"

So str[4] is defined and this is the 4th byte for the int so why seg fault ?
Member 16116753 18-Dec-23 12:17pm View    
Understandable, although I still have question for your code why is str[1] supposed to be segfault ? It is 4 byte for int str[1] ... str[4] in which str[4] is 0x0.
Member 16116753 18-Dec-23 11:26am View    
okey cool so no matter if it's int *x = (int *)y or int *x = (char *)y the arithmetics are still the same, which is for this example 4 bytes/8 bytes shift no matter what "y" will be. This is only for the compiler to stop complaining.