What is the memory allocation if I double the size in malloc().
struct node{
int data;
struct node*next;
};
void main(){
//What if I place 2,3 or 4 times the size.What is the memory allocation looks like.What if I do something like 1.5 times or say 1.3 times
struct node* a =(struct node *)malloc(2*sizeof(struct node));
//Moreover if I print this I get 8,if I am saying twice size,shouldn't I get 16 size(two nodes with 8 bytes each(4 byte of data and 4 byte of next))
printf("%d",sizeof(*a));
}
4 个答案:
答案 0 :(得分:3)
Even though your question is unclear, I think you are confused about the sizeof() operator, it does not give you the allocated size of an object, but instead the size of it's type, in the case of arrays since they are of array type their size will be the size of the array in bytes.
This is a quote from the standard draft
6.5.3.4 The sizeof and _Alignof operators
The sizeof operator yields the size (in bytes) of its operand, which may be an
expression or the parenthesized name of a type. The size is determined from the type of
the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
The _Alignof operator yields the alignment requirement of its operand type. The
operand is not evaluated and the result is an integer constant. When applied to an array
type, the result is the alignment requirement of the element type.
When sizeof is applied to an operand that has type char, unsigned char, or
signed char, (or a qualified version thereof) the result is 1. When applied to an
operand that has array type, the result is the total number of bytes in the array. 103) When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.
Also, if you are working with c, don't cast void * to other pointer types, it's not necessary, if it's c++, then use new instead of malloc().
The line
node *a = malloc(2 * sizeof(*a));
allocates enough space to hold two objects of type node, so you can use it whith index notation if you want like
a[0].data = 1;
a[1].data = 2;
but that doesn't mean that a is an array, this syntax is possible because it's equivalent using pointer arithmetic is
(*(a + 0)).data = 1;
(*(a + 1)).data = 1;
/* incrementa a by 1, and then dereference it -> a[1] */
where I suppose it's easier to see that a is not an array.
103)When applied to a parameter declared to have array or function type, the sizeof operator yields the size of the adjusted (pointer) type (see 6.9.1).
答案 1 :(得分:1)
When you say malloc(2*sizeof(struct node)) you are allocating a memory block that can store two struct nodes right next to each other. If you say 3, or 4, etc., that is how many struct nodes you can then put next to each other in this bigger allocation.
If you say something like malloc(1.5*sizeof(struct node)) then you should get a compiler warning or error about passing a double to a function (malloc) that requires an int, because 1.5*sizeof(struct node) is 1.5 multiplied by an integer, which is a double.
sizeof(*a) actually depends not on your malloc call, but on only the size, in memory, of a single instance of a struct node. This size is comprised of the actual fields in your struct, along with any padding or extra bytes your implementation of the C compiler wants.
答案 2 :(得分:0)
sizeof *a won't change - that's the size of the element you get when you dereference a. Since *a is a struct node, then sizeof *a==sizeof (struct node).
BTW, don't cast the result of malloc() - instead, #include <stdlib.h>.
答案 3 :(得分:0)
Before anything :
main() should return int, not void.
Do not cast the result of malloc().
Please indent your code to improve readability.
The format specifier for size_t is %zu, not %d.
Now, let's get to your questions :
What if I place 2,3 or 4 times the size.What is the memory allocation looks like.What if I do something like 1.5 times or say 1.3 times
Well you're allocating enough for 2/3/4 of these structs, packet together in an array. You can access them using array indexing notation :
// Accesses the second struct
a[1].data = 47;
Moreover if I print this I get 8,if I am saying twice size,shouldn't I get 16 size(two nodes with 8 bytes each(4 byte of data and 4 byte of next))
sizeof is a compile-time* operator. It measures the size of the type you pass to it. a is a pointer to a struct node, so *a is a struct node, and that's what you're measuring. If you want to keep track of the length of your dynamically-allocated array, you need to do it by hand.
*Unless its operand is a VLA. But nobody likes VLAs.