Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a Structure in C with Malloc

I asked a question earlier on defining a structure using malloc. This was the answer I was given by the majority:

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

I was showing a friend my code, and we came to a stumbling block. Could someone please explain why this code works? From my viewpoint, *st hasn't been defined when you malloc it, so there could be any kind of garbage in there. It should be malloc(sizeof(struct retValue))

Thanks for any help

like image 676
Blackbinary Avatar asked Feb 01 '10 16:02

Blackbinary


People also ask

How do you use malloc in structures?

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 space, in which case it returns the special address NULL.

Can we use malloc in structure?

The malloc() function is used for the declaration of the dynamic memory. An array of a struct can be declared either using the static memory or dynamic memory, in this write-up, we will discuss the array of structs using the malloc() function.

How do you allocate memory to struct?

To allocate a new person in the myperson argument, we use the following syntax: person * myperson = (person *) malloc(sizeof(person)); This tells the compiler that we want to dynamically allocate just enough to hold a person struct in memory and then return a pointer of type person to the newly allocated data.

Can you dynamically allocate memory to structures in C?

The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.


2 Answers

Sizeof looks at the type of the expression given to it, it does not evaluate the expression. Thus, you only need to make sure that the variables used in the expression are declared so that the compiler can deduce their type.

In your example, st is already declared as pointer-to-struct-retValue. Consequently the compiler is able to deduce the type of the expression "*st".

Although it doesn't look like it is already declared in your code, the compiler has already taken care of it for you. All declarations in your code are moved to the beginning of the block in which they occur by the compiler. Suppose you write

One way to illustrate the knowledge that is available to the compiler is to look at the intermediate output it generates. Consider this example code...

struct retValue {long int a, long int b};
...
printf("Hello World!\n");
struct retValue* st = malloc(sizeof(*st));

Using gcc as an example and teh above code in the main() function of test.c, let's look at the intermediate output by running...

gcc -fdump-tree-cfg test.c

The compiler will generate the file test.c.022t.cfg - Look at it and you'll see

[ ... removed internal stuff ...]
;; Function main (main)

Merging blocks 2 and 3
main (argc, argv)
{
  struct retValue * st;
  int D.3097;
  void * D.3096;

  # BLOCK 2
  # PRED: ENTRY (fallthru)
  __builtin_puts (&"Hello World!"[0]);
  D.3096 = malloc (16);
  st = (struct retValue *) D.3096;
  D.3097 = 0;
  return D.3097;
  # SUCC: EXIT

}

Note how the declaration was moved to the beginning of the block and the argument to malloc is already replaced with the actual value denoting the size of the type the expression evaluated to. As pointed out in the comments, the fact that the declaration was moved to the top of the block is an implementation detail of the compiler. However, the fact that the compiler is able to do this and also to insert the correct size into the malloc all shows that the compiler was able to deduce the necessary information from the input.

I personally prefer to give the actual type name as a parameter to sizeof, but that is probably a question of coding-style where I'd say that consistency trumps personal-preference.

like image 124
VoidPointer Avatar answered Sep 23 '22 09:09

VoidPointer


The sizeof operator doesn't actually evaluate its operand - it just looks at its type. This is done at compile time rather than runtime. So it can safely be performed before the variable has been assigned.

like image 20
interjay Avatar answered Sep 20 '22 09:09

interjay