Darth.Vader
Darth.Vader

Reputation: 6271

memory allocation for stack vs primitive datatypes

While declaring a structure in C, say:

typedef struct my_stuct {
 int x;
 float f;
} STRT;

If we want to create an instance of this struct and use it, we explicitly need to call malloc, get a pointer to the memory location for this struct before we can actually initialize/use any of the members of the structure:

STRT * my_struct_instance = (STRT *) (malloc(sizeof(STRT)));

However, if I declare a primitive data type (say "int a;") and then want to initialize it (or do any other operation to it), I do not need to explicitly assign mempory space for it by calling malloc before performing any operation on it:

// we do not need to do a malloc(sizeof(i)) blah blah here. Why?
i = 10;

Can you please explain what is the reason for this inconsistency? Thank you!

Upvotes: 1

Views: 314

Answers (5)

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

You can do it in both ways, there isn't any inconsistency.

Heap

int* a= malloc(sizeof(int));
*a=10;
STRT* b= malloc(sizeof(STRT));
b->x=1; 
b->f=1.0;

Stack

int a=10;
STRT b= {1, 1.0};

Upvotes: 1

Philipp Claßen
Philipp Claßen

Reputation: 43969

In your malloc example, you are using pointers. The inconsistency, as you call it, is because a pointer can be initialized in several ways. It is not always initialized by a new memory allocations, but it can also be initialized to point at an existing memory block. So, it is not possible for the language to assume that the variable should be allocated on the heap:

STRT* my_struct_instance; // here I assume (incorrectly) that it is automatically allocated on the heap
my_struct_instance->x = 0; // ERROR: uninitialized use of that variable

Don't know if that answers your question.

Upvotes: 2

Griwes
Griwes

Reputation: 9031

STRT * my_struct_instance = (STRT *) (malloc(sizeof(STRT)));

uses dynamic storage;

int a;

uses automatic storage (I'm using C++ names right now, but it is probably called similarly in C). So, those are two completely different things. int a; is local, on (in most implementations) stack (although stack is not relevant implementation detail); SRTR * [...] is dynamic, on (in most implementations) heap (although, again, heap is not relevant implementation detail).

So, there is no inconsistency. Saying there is one is like saying that there is inconsistency between apples and oranges - but of course there is, since you are comparing apples and oranges. (The other parts of the question don't make sense, since they are based on assumption that apples and oranges are one and the same thing).

Upvotes: 0

Helio Santos
Helio Santos

Reputation: 6805

you can do:

int i;

or

int *i = (int*) malloc(sizeof(int));

just like you can do

STRT my_struct_instance;

or

STRT * my_struct_instance = (STRT *) (malloc(sizeof(STRT)));

Upvotes: 2

NPE
NPE

Reputation: 500287

There is no inconsistency. Each of the two methods can be used both with primitives and with structs:

  STRT s1 = {1, 2};
  int i1 = 1;

  STRT *s2 = (STRT *)malloc(sizeof(STRT));
  int *i2 = (int *)malloc(sizeof(int));
  ...

Upvotes: 7

Related Questions