Search code examples
clinked-listdynamic-memory-allocationsingly-linked-listfree

Stacks & Dynamic memory in C


Exercise: Read a line from the user and print the words in reverse order. Considering the nature of the problem I implemented a singly linked list, to store the words, then copied from the head of the stack the words of the stack to another singly linked list, so then I would print the second list in reverse order. To allocate the strings, stored in a temporary value (Buffer) I allocated memory for the strings. But, its giving me an error when compiling, I don´t understand the nature of that error or how can I replace it. The error: In function ‘strcpy’,writing one too many bytes into a region of a size that depends on ‘strlen’ and In function ‘push’: destination object of size [0, 9223372036854775805] allocated by ‘malloc’ Any problems understanding the problem comment and I will try to help. I am a beginner so any advice and I am al ears.

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

#define MAX_NOME 256

typedef struct stru_node {
    struct stru_node *next;
    char *v;
} node;

node *pop (node *head) {
    node *new;
    if (head != NULL) {
        new = head->next ;
        free(head);
        return new;
    }

    return NULL;
}

node *push( node *head , const char *buff ) {
    node *link = (node*) malloc( sizeof(node) );
    link->v = (char *) malloc( sizeof(char) * strlen (buff) );
    link->v = strcpy( link->v , buff );
    link->next = head;
    return link;
}

node *destroy( node *head ) {
    while ( head != NULL ) {
        head = pop(head);
    }
    return NULL;
}

void print ( node *head ) {
    node *link = head;
    for ( link = head ; link != NULL ; link = link->next ) {
        printf("%s\n" , link->v );
    }
}

int main () {
    char buffer[MAX_NOME];
    node *head;
    while( scanf("%s", buffer) != EOF && strcmp("x" , buffer)) {
        head = push( head , buffer );
    }
    print(head);
    return 0; 
}    `

Solution

  • The pointer head is uninitialized

    node *head;
    

    You need to write

    node *head = NULL;
    

    Within the function push you need to allocate memory for a string including its terminating zero character '\0'. And this assignment

    link->v = strcpy( link->v , buff );
    

    is redundant.

    The function can look like

    node *push( node *head , const char *buff ) {
        node *link = malloc( sizeof(node) );
        link->v = malloc( strlen (buff) + 1 );
        strcpy( link->v , buff );
        link->next = head;
        return link;
    }
    

    The function pop also shall free the allocated memory for the stored string

    node *pop (node *head) {
        node *new = NULL;
    
        if (head != NULL) {
            new = head->next ;
            free( head->v );
            free(head);
        }
    
        return new;
    }
    

    the function print should be declared like

    void print ( const node *head ) {
        for ( const node *link = head ; link != NULL ; link = link->next ) 
        {
            printf("%s\n" , link->v );
        }
    }
    

    And in main you should add a call to the function destroy

    head = destroy( head );