No, you're not allocating memory for y->x twice.

Instead, you're allocating memory for the structure (which includes a pointer) plus something for that pointer to point to.

Think of it this way:

         1          2
        +-----+    +------+
y------>|  x------>|  *x  |
        |  n  |    +------+
        +-----+

You actually need the two allocations (1 and 2) to store everything you need.

Additionally, your type should be struct Vector *y since it's a pointer, and you should never cast the return value from malloc in C.

It can hide certain problems you don't want hidden, and C is perfectly capable of implicitly converting the void* return value to any other pointer.

And, of course, you probably want to encapsulate the creation of these vectors to make management of them easier, such as with having the following in a header file vector.h:

#include <stddef.h>

struct Vector {
    double *data;    // Use readable names rather than x/n.
    size_t size;
};

struct Vector *newVector(size_t sz);
void delVector(struct Vector *vector);
//void setVectorItem(struct Vector *vector, size_t idx, double val);
//double getVectorItem(struct Vector *vector, size_t idx);

Then, in vector.c, you have the actual functions for managing the vectors:

#include <stdlib.h>
#include "vector.h"

// Atomically allocate a two-layer object. Either both layers
// are allocated or neither is, simplifying memory checking.

struct Vector *newVector(size_t sz) {
    // First, the vector layer.

    struct Vector *vector = malloc(sizeof (struct Vector));
    if (vector == NULL)
        return NULL;

    // Then data layer, freeing vector layer if fail.

    vector->data = malloc(sz * sizeof (double));
    if (vector->data == NULL) {
        free(vector);
        return NULL;
    }

    // Here, both layers worked. Set size and return.

    vector->size = sz;
    return vector;
}

void delVector(struct Vector *vector) {
    // Can safely assume vector is NULL or fully built.

    if (vector != NULL) {
        free(vector->data);
        free(vector);
    }
}

By encapsulating the vector management like that, you ensure that vectors are either fully built or not built at all - there's no chance of them being half-built.

It also allows you to totally change the underlying data structures in future without affecting clients. For example:

  • if you wanted to make them sparse arrays to trade off space for speed.
  • if you wanted the data saved to persistent storage whenever changed.
  • if you wished to ensure all vector elements were initialised to zero.
  • if you wanted to separate the vector size from the vector capacity for efficiency(1).

You could also add more functionality such as safely setting or getting vector values (see commented code in the header), as the need arises.

For example, you could (as one option) silently ignore setting values outside the valid range and return zero if getting those values. Or you could raise an error of some description, or attempt to automatically expand the vector under the covers(1).


In terms of using the vectors, a simple example is something like the following (very basic) main.c

#include "vector.h"

int main(void) {
    struct Vector *myvec = newVector(42);
    myvec->data[0] = 2.718281828459;
    delVector(myvec);
}

(1) That potential for an expandable vector bears further explanation.

Many vector implementations separate capacity from size. The former is how many elements you can use before a re-allocation is needed, the latter is the actual vector size (always <= the capacity).

When expanding, you want to generally expand in such a way that you're not doing it a lot, since it can be an expensive operation. For example, you could add 5% more than was strictly necessary so that, in a loop continuously adding one element, it doesn't have to re-allocate for every single item.

Answer from paxdiablo on Stack Overflow
Top answer
1 of 8
198

No, you're not allocating memory for y->x twice.

Instead, you're allocating memory for the structure (which includes a pointer) plus something for that pointer to point to.

Think of it this way:

         1          2
        +-----+    +------+
y------>|  x------>|  *x  |
        |  n  |    +------+
        +-----+

You actually need the two allocations (1 and 2) to store everything you need.

Additionally, your type should be struct Vector *y since it's a pointer, and you should never cast the return value from malloc in C.

It can hide certain problems you don't want hidden, and C is perfectly capable of implicitly converting the void* return value to any other pointer.

And, of course, you probably want to encapsulate the creation of these vectors to make management of them easier, such as with having the following in a header file vector.h:

#include <stddef.h>

struct Vector {
    double *data;    // Use readable names rather than x/n.
    size_t size;
};

struct Vector *newVector(size_t sz);
void delVector(struct Vector *vector);
//void setVectorItem(struct Vector *vector, size_t idx, double val);
//double getVectorItem(struct Vector *vector, size_t idx);

Then, in vector.c, you have the actual functions for managing the vectors:

#include <stdlib.h>
#include "vector.h"

// Atomically allocate a two-layer object. Either both layers
// are allocated or neither is, simplifying memory checking.

struct Vector *newVector(size_t sz) {
    // First, the vector layer.

    struct Vector *vector = malloc(sizeof (struct Vector));
    if (vector == NULL)
        return NULL;

    // Then data layer, freeing vector layer if fail.

    vector->data = malloc(sz * sizeof (double));
    if (vector->data == NULL) {
        free(vector);
        return NULL;
    }

    // Here, both layers worked. Set size and return.

    vector->size = sz;
    return vector;
}

void delVector(struct Vector *vector) {
    // Can safely assume vector is NULL or fully built.

    if (vector != NULL) {
        free(vector->data);
        free(vector);
    }
}

By encapsulating the vector management like that, you ensure that vectors are either fully built or not built at all - there's no chance of them being half-built.

It also allows you to totally change the underlying data structures in future without affecting clients. For example:

  • if you wanted to make them sparse arrays to trade off space for speed.
  • if you wanted the data saved to persistent storage whenever changed.
  • if you wished to ensure all vector elements were initialised to zero.
  • if you wanted to separate the vector size from the vector capacity for efficiency(1).

You could also add more functionality such as safely setting or getting vector values (see commented code in the header), as the need arises.

For example, you could (as one option) silently ignore setting values outside the valid range and return zero if getting those values. Or you could raise an error of some description, or attempt to automatically expand the vector under the covers(1).


In terms of using the vectors, a simple example is something like the following (very basic) main.c

#include "vector.h"

int main(void) {
    struct Vector *myvec = newVector(42);
    myvec->data[0] = 2.718281828459;
    delVector(myvec);
}

(1) That potential for an expandable vector bears further explanation.

Many vector implementations separate capacity from size. The former is how many elements you can use before a re-allocation is needed, the latter is the actual vector size (always <= the capacity).

When expanding, you want to generally expand in such a way that you're not doing it a lot, since it can be an expensive operation. For example, you could add 5% more than was strictly necessary so that, in a loop continuously adding one element, it doesn't have to re-allocate for every single item.

2 of 8
5

The first time around, you allocate memory for Vector, which means the variables x,n.

However x doesn't yet point to anything useful.

So that is why second allocation is needed as well.

🌐
W3Schools
w3schools.com › c › c_memory_struct.php
C Structures and Dynamic Memory
This is useful when you don't know ... program where the number of cars is not fixed). You can use the malloc() function to allocate memory for a struct pointer:...
Discussions

How can malloc allocate structs?
There isn't any further formatting or anything else needed in C to create a variable, you just need a space in memory big enough to hold it and a name that can reference it. The call to malloc reserves a space in memory that will hold your structure and returns a pointer to it, so once that call completes n is a valid pointer to a node structure. The node structure doesn't have valid data in it yet, so we probably want to initialize it to something reasonable with statements like: n->Number = 0; n-Next = NULL; This is exactly what the initializer function in a language like Java or C++ does when you call new Node. In C you need to do those steps yourself. You're also free to skip those steps if they aren't necessary in your context. You're also free to skip those steps when they are necessary and introduce a bug into your program. More on reddit.com
🌐 r/cs50
2
3
January 2, 2022
c - Do we have to malloc a struct? - Stack Overflow
Consider the following code: struct Node { void* data; int ref; struct Node* next; }; typedef struct Node* NodePtr; I have found that I am getting segfaults whenever I try to do anything More on stackoverflow.com
🌐 stackoverflow.com
Question regarding struct, String and Malloc
I've been teaching myself about pointers, struct, and Malloc. I have an example sketch that highlights a mystery. In my example, I have a simple struct with a String, bool, and int. I set values in a struct that is simply created as a global. Using the 'name.variable' syntax all works as expected, ... More on forum.arduino.cc
🌐 forum.arduino.cc
19
0
March 12, 2022
Structs???? Malloc??? Pointers to Malloc and Structs??
I'm learning C++ right now and I am having a very hard time understandind structures, dynamic memory, and pointers to the latters. I understand this is how you write a pointer: ... though I'm not sure how you are supposed to use syntax for malloc, it's confusingint array[10] = (10 * sizeof(int); ... More on cplusplus.com
🌐 cplusplus.com
June 6, 2014
🌐
University of Toronto
cs.toronto.edu › ~heap › 270F02 › node31.html
But first, structs and malloc
n1 = (struct node *) ... n3->data = 3; n3->next = NULL; /* <-- indicates end of list */ malloc allocates sizeof(struct node) bytes, and returns a void pointer to it, which we cast to struct node *. Under some conditions malloc could fail to allocate the required ...
🌐
Nyu-cso
nyu-cso.github.io › notes › 06-struct-malloc.pdf pdf
Struct, malloc Jinyang Li
• Struct · – Group variables together into a primitive “object” · • Malloc · – Allocate data on the heap ·
🌐
Reddit
reddit.com › r/cs50 › how can malloc allocate structs?
r/cs50 on Reddit: How can malloc allocate structs?
January 2, 2022 -

I am currently on lecture 5 of cs50 course. So in it while implementing linked lists, after declaring a struct called "node", we malloc a node..like this:

node *n = malloc(sizeof(node));

And then after that, we treat it as if n is actually pointing to a node and we use operations like

n->Number and n->Next

But malloc only really allocates the amount of memory needed for a node right? We have not yet properly declared a node like we declare other structures like int v = 10 or char l = 'j';

Im sorry but I got slightly confused on this part..

Top answer
1 of 2
3
There isn't any further formatting or anything else needed in C to create a variable, you just need a space in memory big enough to hold it and a name that can reference it. The call to malloc reserves a space in memory that will hold your structure and returns a pointer to it, so once that call completes n is a valid pointer to a node structure. The node structure doesn't have valid data in it yet, so we probably want to initialize it to something reasonable with statements like: n->Number = 0; n-Next = NULL; This is exactly what the initializer function in a language like Java or C++ does when you call new Node. In C you need to do those steps yourself. You're also free to skip those steps if they aren't necessary in your context. You're also free to skip those steps when they are necessary and introduce a bug into your program.
2 of 2
2
node *n = malloc(sizeof(node)); |------| |------------------| A B Part A: Here you declare a pointer to a node "We have not yet properly declared a node like we ....." Part B: Here you allocate the memory sufficient to hold the data of a node. After this you have some memory that can hold a node and a pointer that directs you to that memory location. When you then want to see individual elements of that node struct you ask C to go to that location and look at that memory, C knows you are talking about a "node" since you already declared n as a pointer to a node so C knows what to expect to see at that location. This might not be 100% technically correct but I think it is pretty close and that is what makes me grasp pointers and malloc :)
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-dynamically-create-array-of-structs-in-c
How to Dynamically Create Array of Structs in C? - GeeksforGeeks
July 23, 2025 - To dynamically create an array of structs, we can use the malloc() function to dynamically allocate structs into the array of the given size.
🌐
Delft Stack
delftstack.com › home › howto › c malloc struct
How to Allocate Struct Memory With malloc in C | Delft Stack
February 12, 2024 - Additionally, it explores scenarios ... to zero in C. malloc is the core function for dynamic memory allocation in C that takes a single integer argument representing the number of bytes to be allocated....
Find elsewhere
🌐
Linux Hint
linuxhint.com › array-of-structs-malloc
How to Use Malloc Function to Create Array of Structs – Linux Hint
The struct is a data type similar ... on the memory of the system which can be either dynamic or static. The malloc() function is used for the declaration of the dynamic memory....
Top answer
1 of 3
40

A struct can be allocated automatically, but you are using a pointer to struct which will not allocate space for the destination struct. So that's the reason you get segfault.

The reason your initialization is incorrect is that you are initializing struct members, not the struct itself. Also you are doing this in a wrong way.

There are 2 ways for initializing a struct :

  1. Using stack allocated struct:

    struct example {
      int foo;
    };
    int main() {
      struct example e;
      e.foo=1;
    }
    
  2. Using heap allocated struct with help of malloc():

    struct example {
      int foo;
    };
    int main() {
      struct example *e=malloc(sizeof(struct example));
      e->foo=1;
    }
    

Please note that when you are assigning value to a member of a struct from its pointer (heap allocated struct) you have to use '->' but for the normal struct (the stack allocated one) you have to use '.' .

2 of 3
2

Your assumption is correct: a pointer hasn't memory for the object it is supposed to point on its own, you need to allocate it yourself.

Anyway, as juanchopanza noted: you don't need a pointer and a memory allocation if you're dealing with a local object.

Both techniques follow:

typedef struct Node {
    void* data;
    int ref;
    struct Node* next;
} Node;
typedef struct Node* NodePtr;

int main() {

        NodePtr node = (NodePtr)malloc(sizeof(Node));
        node->data = 0;
        node->next = 0;
        node->ref = 42;
        printf("%d", node->ref);

        Node obj = {0,42,0}; // this is not on the heap
        printf("%d", obj.ref);

The other syntaxes you tried are not correct. Not even part of the language.

🌐
Danluu
danluu.com › malloc-tutorial
Malloc tutorial
Instead, we can mark that the block ... calls to malloc can use re-use the block. But to do that we'll need be able to access the meta information for each block. There are a lot of possible solutions to that. We'll arbitrarily choose to use a single linked list for simplicity. So, for each block, we'll want to have something like · struct block_meta ...
🌐
Quora
quora.com › How-do-I-malloc-an-array-of-structs-in-C
How to malloc() an array of structs in C - Quora
Answer (1 of 3): Let’s say you have defined a struct X, and you want to allocate an array of N of these structs. Then, [code]struct X *pArray = malloc(sizeof(struct X) * N); if (pArray) { // Access elements using pArray } [/code]It’s really that simple. You can use pArray with square bracket ...
🌐
Programiz
programiz.com › c-programming › examples › structure-dynamic-memory-allocation
C Program to Store Data in Structures Dynamically
#include <stdio.h> #include <stdlib.h> struct course { int marks; char subject[30]; }; int main() { struct course *ptr; int noOfRecords; printf("Enter the number of records: "); scanf("%d", &noOfRecords); // Memory allocation for noOfRecords structures ptr = (struct course *)malloc(noOfRecords * sizeof(struct course)); for (int i = 0; i < noOfRecords; ++i) { printf("Enter subject and marks:\n"); scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks); } printf("Displaying Information:\n"); for (int i = 0; i < noOfRecords; ++i) { printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks); } free(ptr); return 0; }
🌐
Quora
quora.com › When-I-declare-a-struct-in-C-does-it-automatically-allocate-memory-for-me-using-malloc
When I declare a struct in C, does it automatically allocate memory for me using malloc()? - Quora
Answer (1 of 13): First, a general principle: no matter what you do, [code ]malloc()[/code] is never invoked automagically. When you declare a [code ]struct[/code], you declare a type. A type, by itself, consumes no memory at runtime. When you declare a variable of the struct type, memory is al...
🌐
Learn C
learn-c.org › en › Dynamic_allocation
Dynamic allocation - Learn C - Free Interactive C Tutorial
This tells the compiler that we ... a pointer of type person to the newly allocated data. The memory allocation function malloc() reserves the specified memory space....
🌐
Scaler
scaler.com › home › topics › pointer to structure in c
Pointer to Structure in C - Scaler Topics
June 20, 2022 - However, the declaration for a pointer to structure only allocates memory for pointer NOT for structure. So to access the member of structure, we must allocate memory for structure using malloc() function and we could also give the address of the already declared structure variable to the structure pointer.
🌐
GitHub
github.com › orgs › community › discussions › 59889
Guys I need help. Do I need to use malloc??? · community · Discussion #59889
July 3, 2023 - However, in your case, since you have declared matrix invers; as a local variable inside the function, it is allocated on the stack automatically. Therefore, there's no need to use malloc in this scenario. One thing to note is that when you return a struct from a function, you need to ensure that the struct's memory is still valid when accessed outside the function.
🌐
Cplusplus
cplusplus.com › forum › beginner › 134883
Structs???? Malloc??? Pointers to Malloc and Structs??
June 6, 2014 - And pointers to structs are like · and pointers to malloc are like ]int *array[10] = (10 * sizeof(int) Like what difference does it make if you write a pointer to a struct or not? What about malloc? What is the synatx structure of it? I really hope you guys can help me.