C memory block size | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C memory block size

if one memory block contains 8 bits or 1 byte, then how does each memory block store an 8 bytes (on a 64 bit system) worth of memory address data?

12th Dec 2020, 10:45 PM
Shen Bapiro
Shen Bapiro - avatar
3 Answers
+ 1
what im trying to understand is that in a memory block, you would only be able to store 1 byte worth of data but when you try to create a new memory block, it would take 8 bytes worth of memory to store the address while the value would only contain 1 byte. so the memory address already took up 8 bytes to tell the location of this one memory block. so my question would be, how does one memory block which can only store 1 byte be able to store the memory address that needs more space than itself. for example: char a; printf("char size : %lu\n", sizeof(a)); printf("address size : %lu\n", sizeof(&a)); >>> char size : 1 address size : 8
13th Dec 2020, 12:10 AM
Shen Bapiro
Shen Bapiro - avatar
+ 1
okay, the memory address that you said does not contain the actual value, it only contains the address that has the value. #include <stdio.h> #include <stdlib.h> int main() { char* i = malloc(sizeof(char)); *i = 'c'; printf("address of pointer i %p\n", &i); printf("sizeof of address of pointer i %lu\n", sizeof(&i)); printf("address of pointer i that points to value('c') %p\n", i); printf("sizeof of pointer i that points to value('c') %lu\n", sizeof(i)); printf("sizeof value pointed by i %lu\n", sizeof(*i)); return 0; } // on my machine address of pointer i 0x7ffcd333e7b8 sizeof of address of pointer i 8 address of pointer i that points to value('c') 0x236f260 sizeof of pointer i that points to value('c') 8 sizeof value pointed by i 1 // so you are mixing with the address of the pointer, the actual pointer that holds the value, and the sizeof the value itself.
13th Dec 2020, 12:42 AM
Flash
0
not sure if I understood your question correctly. #include <stdio.h> typedef struct { char a; char b; char c; char d; char e; char f; char g; char h; } c; int main() { #if defined(__x86_64__) printf("64 bit-> %lu bytes\n", sizeof(c)); #else printf("32 bit-> %lu bytes\n", sizeof(c)); #endif return 0; } // each block should still contain 1 byte, or a total of 8 bytes for 64-bit machines as well if I'm not mistaken.
12th Dec 2020, 11:40 PM
Flash