views:

505

answers:

9

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!

+1  A: 

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
Nope: `sizeof(struct)` is not legal. `sizeof(struct st)` is.
David Thornley
you mean `struct st *x = malloc(sizeof(struct st));` struct itself doesn't actually have a size.
piggles
Sorry - skimmed reading the op.I was concentrating on them asking if sizeof(any struct) was legal
Martin Beckett
Is is `struct st` or `struct tm`? :-)
Alok
+20  A: 

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
One nice thing is that it enforces `x` being a pointer.
David Thornley
Don't let kids play with sharp objects :)
Nikolai N Fetissov
Interesting - had forgotten that trick.
Martin Beckett
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
Awesome. No more malloc( sizeof( struct foo ) ) for me... :) (great user name also)
dicroce
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
@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
wow never knew you could do this. awesom!
Claudiu
+3  A: 

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
+3  A: 

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

Nikolai N Fetissov
A: 

In C pointers are explicit:

struct st * x = (struct st *)malloc(sizeof(struct st));
Mike Dunlavey
+1  A: 

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

alemjerus
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
A: 

Basically your way is correct if x is a pointer. Take a look at http://www.captain.at/howto-c-code-malloc-struct.php .

batbrat
A: 

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.

piggles
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
A: 

This should do:

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