Allocating memory for a Structure in C
Asked Answered
I

7

38

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!

Indiscipline answered 1/2, 2010 at 14:38 Comment(1)
@Blackbinary: Almost there. The correct syntax is malloc(sizeof(struct st)). Of course sizeof *x stated below is preferred.Fleenor
G
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
Grotius answered 1/2, 2010 at 14:40 Comment(6)
Don't let kids play with sharp objects :)Parted
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.Grotius
Awesome. No more malloc( sizeof( struct foo ) ) for me... :) (great user name also)Jehius
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?Rounce
@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.Grotius
@Grotius #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"Buddle
C
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));.

Closestool answered 1/2, 2010 at 14:40 Comment(0)
C
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;
}
Chamberlin answered 1/2, 2010 at 14:39 Comment(4)
Nope: sizeof(struct) is not legal. sizeof(struct st) is.Closestool
you mean struct st *x = malloc(sizeof(struct st)); struct itself doesn't actually have a size.Candice
Sorry - skimmed reading the op. I was concentrating on them asking if sizeof(any struct) was legalChamberlin
Is is struct st or struct tm? :-)Turtleback
P
5

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

Parted answered 1/2, 2010 at 14:40 Comment(0)
H
2

This should do:

struct st *x = malloc(sizeof *x); 
Hydrated answered 2/2, 2010 at 12:47 Comment(0)
H
1

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

Heptangular answered 1/2, 2010 at 14:41 Comment(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.Bellebelleek
The cast, (struct st *), may need in C++.Pterous
C
-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.

Candice answered 1/2, 2010 at 14:51 Comment(1)
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).Amando

© 2022 - 2024 — McMap. All rights reserved.