malloc for struct and pointer in C

前端 未结 8 1999
忘了有多久
忘了有多久 2020-12-04 04:57

Suppose I want to define a structure representing length of the vector and its values as:

struct Vector{
    double* x;
    int n;
};

Now,

8条回答
  •  生来不讨喜
    2020-12-04 05:38

    When you malloc(sizeof(struct_name)) it automatically allocates memory for the full size of the struct, you don't need to malloc each element inside.

    Use -fsanitize=address flag to check how you used your program memory.

提交回复
热议问题