2

I understand the difference between memory allocation using malloc for an array of struct and for an array of pointers to struct. But I want to assign memory using malloc for an array of struct, in the case that the struct itself contains a pointer to another struct. Here is the code for what I am trying to do:

#include<stdio.h>
#include<stdlib.h>

typedef struct linkedlist {
    int pCol;
    struct linkedlist *next;
} plist;

typedef struct particle {
    int color;
    double rad;
    double rx;
    double ry;
    double vx;
    double vy;
    struct linkedlist *event;
} state;

int main()
{
    int N = 5, w = 4;

    plist *ls = (plist*)malloc((N+w)*sizeof(plist));
    printf("N=%d, w=%d, sizeof state=%d, total=%d, sizeof discs=%d\n\n",
    N,w,sizeof(plist), (N+w)*sizeof(plist), sizeof(ls));

    state *discs = (state*)malloc((N+w)*sizeof(state));
    printf("N=%d, w=%d, sizeof state=%d, total=%d, sizeof discs=%d\n",
    N,w,sizeof(state), (N+w)*sizeof(state), sizeof(discs));
    return 0;
}

When I run this, I get the following output:

N=5, w=4, sizeof plist=8, total=72, sizof ls=4

N=5, w=4, sizeof state=56, total=504, sizof discs=4

So, I want to know why does the ls or the discs get assigned only 4 bytes and how can I get total memory required assigned for these arrays of structs?

2
  • The total memory required for a pointer is 4 bytes (or 8, or 2, depending on platform). Think about that for a minute... it's pointing at something, not containing something. ;-) Commented Feb 9, 2015 at 14:24
  • ls and discs are of pointer type, which has size of 4bytes on 32-bit systems. Commented Feb 9, 2015 at 14:25

2 Answers 2

2

You are printing size of pointer(ls) and not size of datatype/structrure pointed by the pointer (*ls).

Do sizeof(*ls) and sizeof(*discs) in your printf statements if you intend to find size of plist and state because that is where ls and discs point to.

If you are thinking of printing the total size allocated to ls or discs by malloc. That is not possible. Its not going to print ((N+w)*sizeof(plist)). So even doing sizeof(*ls) is not going to tell you how many plist structures you allocated

Sign up to request clarification or add additional context in comments.

2 Comments

I did do sizeof(*ls) and sizeof(*discs) in my printf codes but now the output becomes: N=5, w=4, sizeof state=8, total=72, sizeof ls=8 which means still size of *ls is not 72. Why?
You can't find the size of memory allocated to a pointer. You have to keep track of it yourself. If you do sizeof(ptr) it will give you size of ptr on that architecture. if you do sizeof(*ptr) it will give you the size of datatype/structure that ptr points too.
1

in your code, ls is of type plist *,.i.e., a pointer.

sizeof() is an operator which returns the size of the data type, not the amount of memory allocated.

Same goes for discs.

3 Comments

Thanks for the explanation. But then, I have one more question. For sizeof(ls), the ans 4 bytes is right, but then how do I get to know the total memory allocated to the array of struct?
@Neha You cannot, at least not using sizeof().
Well, and the reason behind downvote? If anyone explain, I'll be thankful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.