4

For example I have this structure:

typedef struct{
    char *var = (char*)malloc(20*sizeof(char));
} EXAMPLE;

EXAMPLE *point = (EXAMPLE*)malloc(sizeof(EXAMPLE));

My first question is, will the memory allocated inside the structure only be allocated when I allocate memory for the EXAMPLE pointer?

My second question, when I use free(point) will the memory allocated for var also be freed?

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44
Pedro
  • 63
  • 9
  • 1
    This is not valid C code. – AnT stands with Russia Dec 22 '16 at 01:09
  • I dont know why but when i submit the question it removes the asterisks – Pedro Dec 22 '16 at 01:11
  • `free` frees the memory that was allocated as the result of the call that produced the value of its argument. That's it. No more and no less. – Kerrek SB Dec 22 '16 at 01:11
  • 1
    Have you actually checked if this code even compiles? Someone correct me if I'm wrong, but I don't think you can call `malloc()` inside a struct definition, and if you can it's surely bad practice. Instead you should define purely what you want in the struct, then `malloc()` space for it in a function. – Daniel Porteous Dec 22 '16 at 01:12
  • yes the code is working – Pedro Dec 22 '16 at 01:14
  • sorry you are right it compiles but it crashes – Pedro Dec 22 '16 at 01:15
  • The posted code will not compile. Post the code that actually compiles. – jxh Dec 22 '16 at 01:18
  • With what compiler and what command line or settings does this code compile? Are you sure it is C, and if so which standard? – PJTraill Dec 22 '16 at 01:20
  • 1
    As a side note, you don't need to (and probably shouldn't) cast the return value of `malloc`. See here for an explanation: [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Mike Holt Dec 22 '16 at 01:22
  • @Rodrigo Pina: No need to make things up. This code is not compilable. – AnT stands with Russia Dec 22 '16 at 01:23
  • 1
    @DanielPorteous: calling `malloc` as here, if possible, would be similar to what happens in a constructor in an OO language and would actually be reasonable practice — but the whole thing looks like a weird fantasy if it is meant to be C! – PJTraill Dec 22 '16 at 01:27

2 Answers2

4

With this struct:

typedef struct{
    char *var;
} EXAMPLE;

Remember that when you allocate space for the struct, you're allocating space only for the char * pointer. This pointer just points to memory elsewhere. When you free() the struct, you're just freeing the char * pointer, not the actual memory that it points to.

As such, if you make this struct and then malloc() space for the string you want var to point to, you need to free() the string as well as free()ing the struct.


Some code demonstrating how this might work:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct {
    char *var;
} EXAMPLE;

int main(int argc, char *argv[]) {

    // malloc() space for the EXAMPLE struct.
    EXAMPLE *point = malloc(sizeof(EXAMPLE));
    // malloc() space for the string that var points to.
    point->var = malloc(20 * sizeof(char));
    // Copy "Hello!" into this memory (care with strcpy).
    strcpy(point->var, "Hello!");
    // Print it, it works!
    printf("point->var is: %s\n", point->var);

    // Free stuff.
    free(point->var);
    free(point);

    return 0;
}

Also note that we don't cast the result of malloc(), in C you're not meant to. Also note that we free() point->var first before point. This is important because if we free() point first, we lose the pointer to point->var, and we have a memory leak.

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44
3

I'm ignoring the non-compiling code since the question is pretty clear without it ;-)

No. free() does not know the structure of what it is pointing at,so it just frees one "block". If that block has pointers to other malloced memory then they are leaked (unless there is a pointer to those blocks somewhere else).

John3136
  • 28,809
  • 4
  • 51
  • 69
  • 1
    Yes. If you malloc it, you free it (or more broadly: whoever mallocs it should be responsible for freeing it) - if a library allocates memory a call back into that library should free it. – John3136 Dec 22 '16 at 01:15