0

Please see the below code:

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

typedef int item_t;

typedef struct {item_t *base; item_t *top; int size;} stack_t;

stack_t *create_stack(int size)
{   stack_t *st;
    /* Is this just used to assign some memory to st pointer before    using it? Is this allocation of memory necessary?
    */
    st = (stack_t *) malloc( sizeof(stack_t) );

    /* How is this related to the above allocation of memory to st?
    this code moves the pointer address to base of the stack. How does the
    above syntax related to this? 
    */
    st->base = (item_t *) malloc( size * sizeof(item_t) );
    st->size = size;
    st->top = st->base;
    return( st );
}

int main()
{  stack_t *st;
   char nextop;
   st = create_stack(50);
}

Please see the comment in the code where I have asked the question.

Is this just used to assign some memory to st pointer before using it? Is this allocation of memory necessary?

st = (stack_t *) malloc( sizeof(stack_t) );
Arnold
  • 185
  • 1
  • 2
  • 8
  • 1
    [Don't cast malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Jan 31 '15 at 19:43
  • I agree ^... in some IDEs you get a warning for not casting malloc but ignore them. There is no benefit to type casting a malloc. st = malloc(sizeof(stack_t)). Do the same with st->base. – ShadyBears Jan 31 '15 at 20:02

4 Answers4

0

Yes, the allocation of st is necessary. Before you can use a pointer, it has to point to something. You can either set it to the address of some declared variable, use malloc() to allocate memory, or copy another pointer to it.

If you only need a fixed number of stacks in the program, you could declare them as global variables instead of allocating them dynamically. Outside the functions you can declare:

stack_t the_stack;

You could then use st = &the_stack in create_stack.

Regarding base, you do need to allocate that. Unless you want to hard-code the maximum size of the stack, you need to be able to reallocate this array when it gets full. That can only be done using dynamic allocation.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Does this work this way : let say st = (stack_t *) malloc( sizeof(stack_t) ); allocates memory from 200 to 212 and st->base = (item_t *) malloc( size * sizeof(item_t) ); allocates memory from 300 to 304. Is it right the different memory locations are allocated from st and st->base? or is it like st is allocated 200 to 212 and st->base can be allocated 200 to 204 + size? – Arnold Jan 31 '15 at 19:54
  • There will never be any overlap between allocations that are active at the same time. – Barmar Jan 31 '15 at 19:57
  • How could it possibly work if there were overlap? When you changed one structure it would destroy what you put in a different structure. – Barmar Jan 31 '15 at 19:58
  • This is all basic stuff that should be covered in any C textbook or tutorial. Learning about pointers is one of the most basic things about C programming, it's used all the time. – Barmar Jan 31 '15 at 19:59
0

Is this just used to assign some memory to st pointer before using it? Is this allocation of memory necessary?

Yes, this is used to assign memory to st before using it. Again yes, this is necessary because without this allocation, st is pointing to nowhere.

this code moves the pointer address to base of the stack.

No, it's not that way. Simply it allocates memory to the base member variable in st. Again, without this allocation, st->base points nowhere.

Point to note: Please do not cast the result of malloc() and family.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Your first question (in comments), malloc() allocates memory for a structure that defines a stack, but that structure has not yet been initialised for use.

The second question (in comments), malloc() allocates memory for a stack of size integers (from the typedef), sets the maximum stack size to size, and sets the top-of-stack to the base, that is, the stack is empty.

My opinion on the method, is that you should either store the number of items stacked and the stack size, or a current stack pointer and a max top-of-stack pointer, but this is neither.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

The size of a pointer is that of a size of an int and a pointer just stores a memory address in it. The memory you allocated using the malloc function will allocate the memory equal to the size of the structure and will assign the address of beginning of the memory to the int sized pointer. so when you allocate the memory first time it will allocate 3*(sizeof(int)) this much amount of memory(2 pointers and 1 int variable). now the two pointers do not point anywhere. so in second malloc call you allocate the memory equal to size of int and assign the location of the allocated memory to the pointer.

shiv garg
  • 761
  • 1
  • 8
  • 26