why memory allocation is necessary?

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?
If you are declaring a pointer it will allocate the memory for the pointer itself. If you want to have dynamic memory you need to allocate it explicitly
a little explanation through some code will be more helpful

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

anyway here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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));
waiting for someone to come up with an explanation
The program just happens to work in the first case. In reality, you are stomping memory
somewhere.

1
2
3
int main() {
   int x;
}


declares a variable named x of type int. The variable is allocated on the stack.

1
2
3
int main() {
   int* px;
}


declares a variable named px which is of type pointer-to-int. px is allocated on the stack.
The integer it points to has to exist somewhere in memory, right? But where is the int?
You have to allocate that memory yourself and point px to the memory.

 
px = new int;


"new int" allocates memory to store an int and returns a pointer to it. We then assign the pointer
to px.
thanks for your reply jsmith :)

but i never needed to allocate memory in such simple cases(the example you've mentioned) but reading your reply i think it is a good practice to allocate memory.

thanks once again for helping me out

You should consider playing the lottery then. I think you'll be a winner.
Topic archived. No new replies allowed.