Reputation: 1837
I am a little confused as to what it means to cast a type struct to some memory location in a fixed size array and then wanting to access a specific memory location within the range used for the struct. For example:
static char arraymemory[100];
struct header{
sruct header *previous, *successor;
int isfree;
int size;
};
struct header *headerptr;
headerptr= (struct header*)((char*)p + sizeof(struct header));
where p is a pointer of type struct header that points to the beginning of the array and sizeof(struct header) is the byte count of the struct itself plus the members.
So i checked and sizeof(struct header) is 24 bytes. Now i have an array of char with 1 byte each, so 1 * 100=size of array. I take the pointer p that is located at the beginning of the array and move it 24 bytes(whatever that memory location is), that is what headerptr will point to. If i declare headerptr->isfree and headerptr->size equal to something along with having the pointers previous and successor hold a memory location, does that mean that from where headerptr points to up to at most 24 bytes after will be used for the struct and its members?
Second question, assuming the answer to the above is yes, what happens if i end up accessing the memory location of headerptr + 3? It would be easy to see the output if a plain char data type value such as 'a' was at that specific memory location because headerptr +2 or headerptr +3 would give you a 1 byte char to read. But how does it work if you used that entire range for a struct and you access a specific memory location of that set. Would you get access to one of the members of the struct? Some types take more than 1 byte(char), like int which takes 4. Assuming member isfree is the first thing declared in the struct and it is the first thing that headerptr points to, what does reading the data inside the memory of headerptr + 3 give? The value of isfree regardless of where in the 4 bytes we point to? It is hard for me to visualize this because when i picture the char array i just think of 1 cell for each characters. If we have a struct with multiple different data type that take up multiple cells for each members of the struct and we point to a specific cell, what do we get?
Upvotes: 0
Views: 1182
Reputation: 19395
Your questions are only meaningful provided that p
is sufficiently aligned, which is not guaranteed if p
points to the beginning of a char
array, so let's assume struct header *p = malloc(100);
(the pointer returned if the allocation succeeds is suitably aligned for any type).
But first, there's this strange wording in your post:
sizeof(struct header) is the byte count of the struct itself plus the members.
The struct itself already contains its members, so the plus the members doesn't make sense.
If i declare headerptr->isfree and headerptr->size equal to something along with having the pointers previous and successor hold a memory location, does that mean that from where headerptr points to up to at most 24 bytes after will be used for the struct and its members?
To be exact, it will be the byte headerptr points to through 23 bytes after, totalling 24 bytes.
what happens if i end up accessing the memory location of headerptr + 3?
Since headerptr
is of type struct header *
and 24 bytes from the start of the allocated memory, headerptr + 3
is 72 bytes after, i. e. 96 bytes from the start. Now what happens depends on how you access that location.
It would be easy to see the output if a plain char data type value such as 'a' was at that specific memory location because headerptr +2 or headerptr +3 would give you a 1 byte char to read. But how does it work if you used that entire range for a struct and you access a specific memory location of that set. Would you get access to one of the members of the struct?
When you write headerptr +3
, you seem to mean (char *)headerptr + 3
; I'll assume that from here on. Also, access of individual bytes of an object of a size greater than 1 is only safe with the type unsigned char
. That way we would get access to one byte of one of the struct members.
Assuming member isfree is the first thing declared in the struct and it is the first thing that headerptr points to, what does reading the data inside the memory of headerptr + 3 give?
((unsigned char *)headerptr)[3]
then yields the value of the fourth byte of isfree
, provided that sizeof isfree
is at least 4.
If we have a struct with multiple different data type that take up multiple cells for each members of the struct and we point to a specific cell, what do we get?
We get what we ask for - one byte or cell or (single-byte) character, as in the above example.
Upvotes: 0