Thread: Seg. Fault trying to print dynamically allocated array if called twice

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    5

    Seg. Fault trying to print dynamically allocated array if called twice

    Hey everyone, I've already posted a couple days ago regarding a problem I was facing and I was very lucky to have found help in fixing it

    That said, I'll ask once again for help. This one returns me a seg. fault error (found out via debugger) whenever i try to call my printQueue function more than once, I'll post a minimal example code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
    #define DIM_CODA 10
    #define DIM 50
    
    typedef struct{
        char articolo[DIM];
        double prezzo;
        char destinatario[DIM];
    }Ordine;
    
    struct node{
        Ordine data;
        struct node* link;
    };
    
    typedef struct node Node;
    
    typedef struct{
        Node* front;
        Node* back;
        int cont;
    }Queue;
    
    typedef struct{
        Node* top;
        int cont;
    }Stack;
    
    _Bool isQueueEmpty(Queue *);
    _Bool isQueueFull(Queue *);
    void pushQueue(Queue *, Ordine *);
    void printQueue(Queue *);
    
    int main(){
        int nValues, i, done, choice = 0;
        Ordine ordine[1];
    
    
        /** START STUFF I CANNOT TOUCH **/
        //declaring queue
        Queue queue;
        queue.front = NULL;
        queue.back = NULL;
        queue.cont = 0;
        /** END STUFF I CANNOT TOUCH **/
    
        while (done != 1){
        printf("How many items?\n");
                            scanf("%d", &nValues);
                            printf("\n");
                            getchar();
                            for (i = 0; i<nValues; i++){
                                printf("\nInserirt item:\n");
                                scanf("%[0-9a-zA-Z ]s", ordine[0].articolo);
                                printf("\nInsert price:\n");
                                scanf("%lf", &ordine[0].prezzo);
                                getchar();
                                printf("\nInsert buyer:\n");
                                scanf("%[0-9a-zA-Z ]s\n", ordine[0].destinatario);
                                getchar();
                                pushQueue(&queue, ordine);
                            }
                            printf("wanna print? 1 for yes\n");
                            scanf("%d", &choice);
                            if (choice == 1){
                            printQueue(&queue);
                            done = 0;
                            }
            }
        return 0;
        }
    
    _Bool isQueueEmpty(Queue * e) {
        if (e == NULL) {
            return false;
        }
        if (e->cont == 0) {
            return true;
        } else {
            return false;
        }
    }
    
    _Bool isQueueFull(Queue * e){
        return e->cont == DIM_CODA;
    }
    
    void pushQueue(Queue * e, Ordine * val){
        Node * newnode;
        if (isQueueFull(e)){
            printf("Overflow error.\n");
        }
        else
            newnode = malloc(sizeof(Node));
            newnode->data.prezzo = val->prezzo;
            strcpy(newnode->data.articolo, val->articolo);
            strcpy(newnode->data.destinatario, val->destinatario);
            newnode->link = NULL;
            if (isQueueEmpty(e)){
                e->front = newnode;
                e->back = newnode;
            }
            else{
                e->back->link = newnode;
                e->back= newnode;
            }
            e->cont++;
    }
    
    void printQueue(Queue * e){
        int i;
        for (i=0; i<e->cont; e->front = e->front->link, i++){
            printf("Articolo: %-24s\tPrezzo: %-24lf\t Destinatario: %-24s\n", e->front->data.articolo, e->front->data.prezzo, e->front->data.destinatario);
        }
    }
    So this code is about the management of a queue in C. It looked like it was going smoothly up until i tried to printQueue my queue more than once. After the first (successful) printf, the second time I call it my program just crashes with a seg. fault

  2. #2
    Registered User
    Join Date
    Jun 2018
    Posts
    5
    Can't edit the topic anymore so I'll just add another post

    Fixed the problem by changing my function to:
    Code:
    void printQueue(Queue * e){    Queue * a = malloc(sizeof(Queue)*DIM_CODA);
            a->front = e->front;
        while(e->front){
            printf("Articolo: %-24s\tPrezzo: %-24lf\t Destinatario: %-24s\n", e->front->data.articolo, e->front->data.prezzo, e->front->data.destinatario);
            e->front = e->front->link;
        }
        e->front = a->front;
    }
    and applying all subsequent changes

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,493
    Quote Originally Posted by equals
    Fixed the problem by changing my function to:
    You now have a memory leak because you have a malloc without a corresponding free. What you should have done:
    Code:
    void printQueue(Queue *queue) {
        Node *current = queue->front;
        while (current){
            printf("Articolo: %-24s\tPrezzo: %-24lf\t Destinatario: %-24s\n",
                current->data.articolo,
                current->data.prezzo,
                current->data.destinatario);
            current = current->link;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2018
    Posts
    5
    Quote Originally Posted by laserlight View Post
    You now have a memory leak because you have a malloc without a corresponding free. What you should have done:
    Code:
    void printQueue(Queue *queue) {
        Node *current = queue->front;
        while (current){
            printf("Articolo: %-24s\tPrezzo: %-24lf\t Destinatario: %-24s\n",
                current->data.articolo,
                current->data.prezzo,
                current->data.destinatario);
            current = current->link;
        }
    }
    Thanks a lot for this, I'll replace the code and analize it since all those structures I'm forced to use are kinda confusing me as a newbie
    I appreciate your input a great deal!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-16-2012, 01:14 PM
  2. Dynamically Allocated Array
    By chris4 in forum C Programming
    Replies: 9
    Last Post: 05-06-2011, 10:01 AM
  3. Initializing a 2D array dynamically allocated
    By newboyc in forum C Programming
    Replies: 5
    Last Post: 02-01-2011, 01:08 PM
  4. Dynamically allocated array
    By dre in forum C Programming
    Replies: 17
    Last Post: 08-13-2009, 06:57 PM
  5. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM

Tags for this Thread