37

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!

Blackbinary
  • 3,936
  • 18
  • 49
  • 62
  • 1
    @Blackbinary: Almost there. The correct syntax is `malloc(sizeof(struct st))`. Of course `sizeof *x` stated below is preferred. – kennytm Feb 01 '10 at 14:42

7 Answers7

82

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
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 21
    Don't let kids play with sharp objects :) – Nikolai Fetissov Feb 01 '10 at 14:42
  • 4
    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. – dirkgently Feb 01 '10 at 14:45
  • Awesome. No more malloc( sizeof( struct foo ) ) for me... :) (great user name also) – dicroce Feb 01 '10 at 15:02
  • 1
    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? – Jay Feb 01 '10 at 15:04
  • @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. – dirkgently Feb 01 '10 at 15:52
  • @dirkgently #include 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" – emeraldhieu Jul 16 '11 at 05:35
7

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));.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
5

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;
}
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
5

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

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
2

This should do:

struct st *x = malloc(sizeof *x); 
codaddict
  • 445,704
  • 82
  • 492
  • 529
1

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

alemjerus
  • 8,023
  • 3
  • 32
  • 40
  • 2
    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. – geocar Feb 01 '10 at 15:00
-1

I believe, when you call sizeof on a struct type, C recursively calls sizeof on the fields of the struct. So, struct st *x = malloc(sizeof(struct st)); only really works if struct st has a fixed size. This is only significant if you have something like a variable sized string in your struct and you DON'T want to give it the max length each time.

In general,

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

works. Occasionally, you will run into either variable sized structs or 'abstract' structs (think: abstract class from Java) and the compiler will tell you that it cannot determine the size of struct st. In these cases, Either you will have to calculate the size by hand and call malloc with a number, or you will find a function which returns a fully implemented and malloc'd version of the struct that you want.

Ritwik Bose
  • 5,889
  • 8
  • 32
  • 43
  • 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). – Matteo Italia Feb 02 '10 at 12:52