Allocating memory for a Structure in C

103,888

Solution 1

My favorite:

#include <stdlib.h>

struct st *x = malloc(sizeof *x); 

Note that:

  • x must be a pointer
  • no cast is required
  • include appropriate header

Solution 2

You're not quite doing that right. struct st x is a structure, not a pointer. It's fine if you want to allocate one on the stack. For allocating on the heap, struct st * x = malloc(sizeof(struct st));.

Solution 3

struct st* x = malloc( sizeof( struct st ));

Solution 4

It's exactly possible to do that - and is the correct way

Assuming you meant to type

struct st *x = malloc(sizeof(struct st)); 

ps. You have to do sizeof(struct) even when you know the size of all the contents because the compiler may pad out the struct so that memebers are aligned.

struct tm {
  int x;
  char y;
}

might have a different size to

struct tm {
  char y;
  int x;
}

Solution 5

This should do:

struct st *x = malloc(sizeof *x); 
Share:
103,888
Blackbinary
Author by

Blackbinary

CIS Major 1st Year

Updated on March 16, 2020

Comments

  • Blackbinary
    Blackbinary over 4 years

    I'm tasked to create a program which dynamically allocates memory for a structure. normally we would use

    x=malloc(sizeof(int)*y);
    

    However, what do I use for a structure variable? I don't think its possible to do

    struct st x = malloc(sizeof(struct)); 
    

    Could someone help me out? Thanks!

  • David Thornley
    David Thornley over 14 years
    Nope: sizeof(struct) is not legal. sizeof(struct st) is.
  • Nikolai Fetissov
    Nikolai Fetissov over 14 years
    Don't let kids play with sharp objects :)
  • Ritwik Bose
    Ritwik Bose over 14 years
    you mean struct st *x = malloc(sizeof(struct st)); struct itself doesn't actually have a size.
  • dirkgently
    dirkgently over 14 years
    The thing I like the most is the fact that even if you rename the structure later, the RHS still works. Arguably, with modern editors this is not much of a problem.
  • Martin Beckett
    Martin Beckett over 14 years
    Sorry - skimmed reading the op. I was concentrating on them asking if sizeof(any struct) was legal
  • Alok Singhal
    Alok Singhal over 14 years
    Is is struct st or struct tm? :-)
  • geocar
    geocar over 14 years
    Do not cast malloc(): 1. It's not needed (C void* is different than C++ void*), 2. Lint-like analysis tools could detect giving malloc() the wrong size, and 3. including the proper header will give the compiler the right hints that you mean the system malloc, which may produce better code.
  • dicroce
    dicroce over 14 years
    Awesome. No more malloc( sizeof( struct foo ) ) for me... :) (great user name also)
  • Jay
    Jay over 14 years
    Won't RHS lead you to trouble if you do a typo error and miss *? I always thought instead of doing *x mentioning the struct name is a better way as it will protect against the typo error. Am I missing something here?
  • dirkgently
    dirkgently over 14 years
    @Jay: Certainly, but that is just one of the many subtle ways in which you can inject bugs. I have read (and suffered) from forgetting to change the struct name on the RHS much more. Another counter-point: read my first bulleted item or David Thornley's comment.
  • Matteo Italia
    Matteo Italia over 14 years
    AFAIK in C there's no such a thing as variable sized structs or abstract structs (you can fake them, but they are not supported at language level); the only case in which the compiler will tell you that it doesn't know the size of a struct is when the struct is only declared but not defined (useful if in the current compilation unit you just handle pointers to the struct).
  • emeraldhieu
    emeraldhieu almost 13 years
    @dirkgently #include <stdlib.h> struct A { int x; }; void main() { struct A st = malloc(sizeof *st); } I got this error when running on Visual C++ 6.0: "error C2440: 'initializing' : cannot convert from 'void *' to 'struct A *' Conversion from 'void' to pointer to non-'void' requires an explicit cast"