Jump to content

why memory allocation is necessary?

- - - - -

  • Please log in to reply
8 replies to this topic

#1
swAn

swAn

    Newbie

  • Members
  • Pip
  • 5 posts
i have just started learning data structures and was trying to implement linked list.

i wanted to know why memory allocation is necessary for the pointer variable i am declaring.

my doubt is when i am declaring a new pointer of list type i am creating doesn't it get the required memory allocated automatically?why i have to specifically assign it?

here is the code due to which i came up with such a question(i am not sure if i asked the correct thing)

anyway here it is:

#include<stdio.h>

#include<malloc.h>

typedef struct list

{

    int info;

    struct list *link;

}node;

void insertatbeg(node **start)

{

    node *ptr;

    node *new1;

    int d;

    ptr=(node*)malloc(sizeof(node));

    printf("enter the element you want to insert");

    scanf("%d",&d);

    ptr->info=d;

    ptr->link=*start;

    *start=ptr;

   // printf("value of *start in beg func = %u",*start);

}

void insertatend(node **start)

{

    node *ptr,*gptr;

    int d;

    gptr=(node *)malloc(sizeof(node));

    //printf("\n *start=%d",*start);

    ptr=*start;

    while (ptr->link!=NULL)

    {

        ptr=ptr->link;

    }

    printf("enter the element");

    //scanf("%d",&gptr->info);

    scanf("%d",&d);

    gptr->info=d;

    gptr->link=ptr->link;

    ptr->link=gptr;

}

void createlist(node **start)

{

    int n,j=0;

    printf("enter the number of elements you want to insert");

    scanf("%d",&n);

    while (j<n)

    {

        if (*start==NULL)

        {

            insertatbeg(start);

        }

        else

        {

            insertatend(start);

    }

    j++;

}

}

void display(node **start)

{

    node *ptr=*start;

    while (ptr!=NULL)

    {

        printf("\t%d",ptr->info);

        ptr=ptr->link;

    }

}

main()

{

    node *start1=NULL;

    createlist(&start1);

    display(&start1);

    system("pause");

}

My doubts:

1. In the insertatbeg() if i'll make the following line comment the proogram will still work perfectly:

ptr=(node*)malloc(sizeof(node));

2.But in the insertatend() if i'll make the following line comment the program crashes:

gptr=(node *)malloc(sizeof(node));


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
There are only two possibilities:
1) A node creates a static node variable. This will create a node, which creates a node, which creates a node, which creates a... You run out of memory.
2) A node creates a pointer to a node variable. You only create another node variable, via memory allocation, when you actually need it. You might run out of memory if you try, but it's not automagical :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Pointer is a variable that stores the address of the memory location. When you create a pointer variable, the memory gets automatically allocated for that pointer variable with garbage value. You are allocating memory through malloc() not for that pointer variable, only to make the pointer variable to point some memory.

Try this simple program
#include<stdio.h>
#include<malloc.h>

main()
{
    int *ptr;     // allocates a variable ptr at 0X0492ABC1 memory location

    ptr = (int*)malloc(sizeof(int)); // assigns  0X123BCA24 memory location to ptr

    printf("ptr address: %p\n", ptr);     // this gives memory address of ptr i.e 0X0492ABC1
    printf("ptr address: %p\n", *ptr);  // this gives the memory address stored in ptr i.e 0X123BCA24
    
    system("pause");
}

For both of your doubts, the program will crash.. I don't know why your program didn't crash for the 1st.

#4
swAn

swAn

    Newbie

  • Members
  • Pip
  • 5 posts

WingedPanther said:

There are only two possibilities:
1) A node creates a static node variable. This will create a node, which creates a node, which creates a node, which creates a... You run out of memory.
2) A node creates a pointer to a node variable. You only create another node variable, via memory allocation, when you actually need it. You might run out of memory if you try, but it's not automagical :)


will you please explain a little more....what is the point of static node variable here?

#5
swAn

swAn

    Newbie

  • Members
  • Pip
  • 5 posts

veda87 said:

Pointer is a variable that stores the address of the memory location. When you create a pointer variable, the memory gets automatically allocated for that pointer variable with garbage value. You are allocating memory through malloc() not for that pointer variable, only to make the pointer variable to point some memory.

Try this simple program
#include<stdio.h>
#include<malloc.h>

main()
{
    int *ptr;     // allocates a variable ptr at 0X0492ABC1 memory location

    ptr = (int*)malloc(sizeof(int)); // assigns  0X123BCA24 memory location to ptr

    printf("ptr address: %p\n", ptr);     // this gives memory address of ptr i.e 0X0492ABC1
    printf("ptr address: %p\n", *ptr);  // this gives the memory address stored in ptr i.e 0X123BCA24
    
    system("pause");
}

For both of your doubts, the program will crash.. I don't know why your program didn't crash for the 1st.

whatever you said i clearly understand that but if you'll try to run my you can see what i have pointed out is exactly happening

#6
swAn

swAn

    Newbie

  • Members
  • Pip
  • 5 posts
can anyone come up with a little more explanation to my problem

please

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

swAn said:

will you please explain a little more....what is the point of static node variable here?
What static node variable? You mean the first one?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
swAn

swAn

    Newbie

  • Members
  • Pip
  • 5 posts

WingedPanther said:

What static node variable? You mean the first one?

in your first reply you mentioned static node variable i would like to know what do you mean by static node variable.....read the first point you mentioned in your first reply to my query.

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
By static node variable, I meant a non-pointer node variable.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users