Question about malloc a struct
-
The code below uses a variable's type as size in malloc(). And I printf the size of //line 1, b and c to find out their sizes malloc will allocation the space for them in heap. Question part 1: One person told me I should use // line a's size to malloc in // #####, while the other person said I should use //line c instead. They do have the same size in printf(). Which one is correct? Why? Question part 2: // line b and // line c both have the same type of variable pointers, but why do they have different sizes? struct Vector { double *data; size_t size; }; int main() { int a, b, d; int sz = 12; a = sizeof(struct Vector); // line a printf("%d\n", a); //a = 16 b = sizeof(struct Vector*); // line b printf("%d\n", b); //b = 8 c = sizeof(*retVal); // line c printf("%d\n\n", c); //c = 16 struct Vector *retVal = malloc (sizeof (struct Vector)); //##### retVal->data = malloc (sz * sizeof (double)); // Set size and return. retVal->size = sz; printf("retVal->size: %d\n", sz); return 0; } Thank you for your help. :)
-
The code below uses a variable's type as size in malloc(). And I printf the size of //line 1, b and c to find out their sizes malloc will allocation the space for them in heap. Question part 1: One person told me I should use // line a's size to malloc in // #####, while the other person said I should use //line c instead. They do have the same size in printf(). Which one is correct? Why? Question part 2: // line b and // line c both have the same type of variable pointers, but why do they have different sizes? struct Vector { double *data; size_t size; }; int main() { int a, b, d; int sz = 12; a = sizeof(struct Vector); // line a printf("%d\n", a); //a = 16 b = sizeof(struct Vector*); // line b printf("%d\n", b); //b = 8 c = sizeof(*retVal); // line c printf("%d\n\n", c); //c = 16 struct Vector *retVal = malloc (sizeof (struct Vector)); //##### retVal->data = malloc (sz * sizeof (double)); // Set size and return. retVal->size = sz; printf("retVal->size: %d\n", sz); return 0; } Thank you for your help. :)
-
The code below uses a variable's type as size in malloc(). And I printf the size of //line 1, b and c to find out their sizes malloc will allocation the space for them in heap. Question part 1: One person told me I should use // line a's size to malloc in // #####, while the other person said I should use //line c instead. They do have the same size in printf(). Which one is correct? Why? Question part 2: // line b and // line c both have the same type of variable pointers, but why do they have different sizes? struct Vector { double *data; size_t size; }; int main() { int a, b, d; int sz = 12; a = sizeof(struct Vector); // line a printf("%d\n", a); //a = 16 b = sizeof(struct Vector*); // line b printf("%d\n", b); //b = 8 c = sizeof(*retVal); // line c printf("%d\n\n", c); //c = 16 struct Vector *retVal = malloc (sizeof (struct Vector)); //##### retVal->data = malloc (sz * sizeof (double)); // Set size and return. retVal->size = sz; printf("retVal->size: %d\n", sz); return 0; } Thank you for your help. :)
Both are correct.
sizeof(struct Vector)
expands to the size of the type whilesizeof(*retVal)
expands to the size of the type pointed to by the variable. The second method should be preferred because it will be always the correct size even when changing the variable type. Imagine that you have another structure with different size. When you now change the type ofretVal
to that structure, you must also change the type for the firstsizeof()
but not for the second. -
Both are correct.
sizeof(struct Vector)
expands to the size of the type whilesizeof(*retVal)
expands to the size of the type pointed to by the variable. The second method should be preferred because it will be always the correct size even when changing the variable type. Imagine that you have another structure with different size. When you now change the type ofretVal
to that structure, you must also change the type for the firstsizeof()
but not for the second.Thanks for your response. Just to make sure I understand your answer, I listed .....I'm thinking the second malloc() used in my previous example can be replaced by the line below?
sizeof(12 * sizeof(retVal->data));
Thank you!
-
Thanks for your response. Just to make sure I understand your answer, I listed .....I'm thinking the second malloc() used in my previous example can be replaced by the line below?
sizeof(12 * sizeof(retVal->data));
Thank you!
No, that is wrong. The second example from your initial post (
sizeof(struct Vector*)
) returns the size of a pointer which is 4 or 8 with 32-bit or 64-bit builds. The sizeof[^] operator expands to the size of the argument during compilation. If the argument is not a type but a variable, the type of the variable is returned. If the variable is a pointer, the pointer must be dereferenced using the*
operator to get the size of the type instead the size of the pointer. Similar applies when the type is a pointer itself. But when passing a type followed by*
like in your second example, it is a pointer to the type. In that case the type does not care because all pointers have the same size. Maybe you are irritated how the*
operator is treated. With declarations it indicates a pointer. When accessing variables, it accesses the content of pointer variables. -
No, that is wrong. The second example from your initial post (
sizeof(struct Vector*)
) returns the size of a pointer which is 4 or 8 with 32-bit or 64-bit builds. The sizeof[^] operator expands to the size of the argument during compilation. If the argument is not a type but a variable, the type of the variable is returned. If the variable is a pointer, the pointer must be dereferenced using the*
operator to get the size of the type instead the size of the pointer. Similar applies when the type is a pointer itself. But when passing a type followed by*
like in your second example, it is a pointer to the type. In that case the type does not care because all pointers have the same size. Maybe you are irritated how the*
operator is treated. With declarations it indicates a pointer. When accessing variables, it accesses the content of pointer variables.Thank you for clarifying my confusion, Jochen!! :D I appreciate it.