Allocation of memory for char array

前端 未结 10 848
孤独总比滥情好
孤独总比滥情好 2021-01-02 12:45

Let\'s say you have-

struct Person {
    char *name;
    int age;
    int height;
    int weight; 
 };

If you do-

struct Pe         


        
10条回答
  •  太阳男子
    2021-01-02 13:42

    The name member is just a pointer. The size of a pointer varies with the underlying architecture, but is usually 4 or 8 bytes nowadays.

    The data that name can point to (if assigned later) should be laid out in an area that does not coincide with the struct at all.

    At the language level, the struct doesn't know anything about the memory that the name member is pointing to; you have to manage that manually.

提交回复
热议问题