LonelyCpp
LonelyCpp

Reputation: 2673

Memory allocation to a Node with Character array in a Linked list

OK, this is a simple single linked list program in c

struct node
{
    int id;
    char name[20];
    int sem;

    struct node *link;
};

typedef struct node* Node;

Node getnode()
{
    Node temp=(Node)(malloc(sizeof(Node)));
    if(temp==NULL)
        printf("\n Out of memory");

    return temp;
}

Node ins_pos(Node first)
{
    Node temp=getnode();
    printf("\n Enter id ");
    scanf("%d",&temp->id);
    printf("\n Enter name ");
    scanf("%s",temp->name);
    printf("\n Enter semester ");
    scanf("%d",&temp->sem);

    if(first == NULL)
    {
        temp->link=NULL;
        return temp;
    }

    else
    {
        int pos,i;
        printf("\n Enter position: ");
        scanf("%d",&pos);

        if(pos == 1)
        {
            temp->link=first;
            return temp;
        }

        else
        {
            Node prev=NULL,cur=first;
            for(i=1; i<pos; i++)
            {
                 if(cur==NULL)
                    break;

                prev=cur;
                cur=cur->link;


            }

            if(cur==NULL && i < pos)
                printf("\n Position invalid");
            else
            {
                prev->link=temp;
                temp->link=cur;
            }

            return first;
        }
    }
}

Node del(Node first)
{
    if(first==NULL)
        printf("\n List is Empty ");
    else
    {
        Node temp=first;
        printf("\n ID: %d was deleted",temp->id);
        first=first->link;
        free(temp);
    }
    return first;
}

void disply(Node first)
{
    if(first==NULL)
        printf("\n List is empty");
    else
    {
        Node cur=first;
        while(cur!=NULL)
        {
            printf("\n ID : ");
            printf("%d",cur->id);
            printf("\n Name : ");
            printf("%s",cur->name);
            printf("\n Semester : ");
            printf("%d",cur->sem);
            printf("\n\n\n");

            cur=cur->link;
        }
    }
}
int main()
{
    Node first=NULL;
    int opt;

    do
    {
            printf("\n QUEUE MENU\n 1.Insert at position  \n 2.delete front\n 3.display\n 4.Exit \n\n Enter your choice : ");
            scanf("%d",&opt);

            switch(opt)
            {
                case 1 :first = ins_pos(first);
                        break;

                case 2 :first = del(first);
                        break;

                case 3 :disply(first);
                        break;

            }


    }while(opt!=4);


    return 0;
}  

When inserting a new node, Code Blocks Crashes at the malloc statement. How do I know? well, it crashes before asking "Enter ID". So, am I doing something wrong?

Another point here is, it works fine with only an integer field in node, the problem here maybe the character array.

Upvotes: 0

Views: 262

Answers (3)

ameyCU
ameyCU

Reputation: 16607

In this function Node getnode() -

Node temp=(Node)(malloc(sizeof(Node)));

With the above malloc you allocate memory equal to size of Node which is of type struct pointer , and is not enough .Therefore ,you get a segmentation fault.

instead of this ,write like this -

Node temp=malloc(sizeof(*temp));        //also there is no need of cast

This will allocate memory equal to size of type to which temp points to i.e size equal to that of structure. Which is appropriate .

Upvotes: 1

user1969104
user1969104

Reputation: 2420

You need to use Node temp=(Node)(malloc(sizeof(*temp)));

Upvotes: 0

mksteve
mksteve

Reputation: 13073

Some errors here.

 malloc( sizeof( struct node ) );

Otherwise too little memory is allocated.

Do you include stdlib.h for malloc definition - that would cause this issue (no definition, defaults to int).

Upvotes: 1

Related Questions