ian
ian

Reputation: 33

Program flow stuck in function

I seem to have created a selfish function which refuses to pass control back to my main function. I've inserted into my code several output statements to ease debugging, and to prove that the program is resting at the end of my 'add_node()' function. I have included the entire program because I am genuinely perplexed as to where the problem may lie.

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

/* All-purpose error function */
void kill(char *error)
{
  if(errno != 0) {
    perror("Error: ");
  } else {
    printf("%s", error);
  }
  exit(1);
}

// A link with string data type
typedef struct slink
{
    char value[20];
    struct node* prev;
    struct node* next;
}slink;

// A link with double data type
typedef struct dlink
{
    double value;
    struct node* prev;
    struct node* next;
}dlink;

// Node structure, contains several links
typedef struct node
{
    slink name;
    dlink weight;
    dlink height;
    dlink spins;  //Place forhead on bat, count spins until fall.
    dlink chug;   //Open beer, chug. Record time.
}node;

// Creates a new node and populates it with given data arguments
void add_node(char* n, double w, double h, double s, double c)
{
    node* this = malloc(sizeof(node));
    bzero(this, sizeof(node));
    if(!this) kill("Failed to create node.\n");

    strncpy(this->name.value, n, 20);
    this->weight.value = w;
    this->height.value = h;
    this->spins.value = s;
    this->chug.value = c;

    sort_node(this);
    printf("returned flow to add_node\n");
}

/*Compares values of this node to the values of all other nodes
and assigns appropriate ptr values to the links*/
int sort_node(node* this)
{
    static bool first_sort = true;
    static node* head;

    if(first_sort){
        this->name.prev = NULL;
        this->name.next = NULL;
        this->weight.prev = NULL;
        this->weight.next = NULL;
        this->spins.prev = NULL;
        this->spins.next = NULL;
        this->chug.prev = NULL;
        this->chug.prev = NULL;
        first_sort = false;
        head = this;
        printf("first node sorted successfully\n");
    } else {
        node* current = head;

        //Traverse list searching for logical alphabetical neighbors
        while(current->name.value < this->name.value) {
            if(current->name.next) {
                current = current->name.next;
            }
        }
        this->name.next = current;
        this->name.prev = current->name.prev;
        current->name.prev = this;
    }

    return 0;
}

int main(void)
{
    add_node("Chiis Manly", 195, 66.5, 39, 23);
    printf("chiis added");

    add_node("Harrie Malaria", 253, 48, 210, 4);

    return 0;
}

The output of this program is as follows:

first node sorted successfully
returned flow to add_node

note that 'printf("chiis added");' in my 'main' function is never run. Thank you all greatly for any assistance!

Upvotes: 0

Views: 861

Answers (2)

Alexguitar
Alexguitar

Reputation: 722

If you add a fflush(stdout); after printf("chiis added"); you'll see that printf is indeed executed and your program gets stuck somewhere else (see @i_am_jorf's answer).

Whatever you pass to printf() is buffered by the standard library to improve the performance of writing to the terminal. You can manually flush the buffer with fflush().

Upvotes: 1

i_am_jorf
i_am_jorf

Reputation: 54600

If the following if statement condition is false:

if(current->name.next) {

Then how will your while loop ever terminate? That's your problem.

See this on debugging small programs.

Upvotes: 1

Related Questions