0

I have function along the lines of:

void insert(btnode **ptr, char *name, unsigned int race, unsigned int class, unsigned int id, char *guild)
{
    if((*ptr) == NULL)
    {
        (*ptr) = (btnode*)malloc(sizeof(btnode));
        (*ptr)->rec = (record*)malloc(sizeof(record));
        (*ptr)->left=NULL;
        (*ptr)->right=NULL;
        strcpy((*ptr)->rec->name,name);
        (*ptr)->rec->race = race;
        (*ptr)->rec->class = class;
        (*ptr)->rec->id = id;
        strcpy((*ptr)->rec->guild, guild);
    }
    else
    {
        if((*ptr)->rec->id > id)
        {
            insert(&((*ptr)->left),name,race,class,id,guild);
        }
        else
        {
            insert(&((*ptr)->right),name,race,class,id,guild);
        }
    }
}

It is use to insert values into a binary tree

The issue I'm having is when the first node is null everything works fine. But when the function has to call its self the char array doesn't print what its meant to.

Any suggestions how to solve this issue?

EDIT: full code added, there is no problems with unsnigned ints only chars.

struct decelerations:

#define TWOBYTEINT 16
#define FOURBYTEINT 32
#define MAXIMUMLINE 70
#define FALSE 0
#define TRUE 1

typedef struct record
{
        char name[13];
        unsigned int race : TWOBYTEINT;
        unsigned int class : TWOBYTEINT;
        unsigned int id : FOURBYTEINT;
        char guild[30];
}__attribute__((packed)) record;

typedef struct node
{
        record *  rec;
        struct node *right, *left;
}btnode;
8
  • 1
    This code makes no sense, if (null) is always false and will never execute the body. I guess you meant something along the lines of if (!node) ... ? Additionally you should show more of your code, what is insert? Commented Mar 28, 2012 at 9:21
  • 1
    This part of the code is fine, you'd have to provide more code to see whether there's some other mistake. Commented Mar 28, 2012 at 9:22
  • Please specify how you create your node and also fix the problem @hochl mentioned Commented Mar 28, 2012 at 9:23
  • @hochl I'm sure this is just shortened and made to be read as pseudo code (i.e. that null would be some check to see whether a matching node exists) Commented Mar 28, 2012 at 9:23
  • 1
    "doesn't print what its meant to." Ok, but we don't know what it's meant to print, so you have to tell us. Commented Mar 28, 2012 at 9:24

2 Answers 2

1

The strcpy look extremely dodgy - they look they are copying to unallocated memory pointed to by the uninitialised memory in the (*ptr)->rec structure.

Surprised your code doesn't crash.

Sign up to request clarification or add additional context in comments.

Comments

0

Nothing wrong with the code, just remove some bad habits. (I kept the packed attribute, unfortunately)

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

typedef struct record {
        char name[13];
        unsigned race : 16;
        unsigned class : 16;
        unsigned id : 32;
        char guild[30];
        }__attribute__((packed)) record;

record *record_new(char *name, unsigned int race, unsigned int class, unsigned int id, char *guild);

typedef struct node {
        record *  rec;
        struct node *right, *left;
        } btnode;

void insert(btnode **ptr, char *name, unsigned int race, unsigned int class, unsigned int id, char *guild);

void insert(btnode **ptr, char *name, unsigned int race, unsigned int class, unsigned int id, char *guild)
{
    while( *ptr ) { /* may need to check for (*ptr)->rec, too .. */
        ptr = ((*ptr)->rec->id > id)
            ? &(*ptr)->left
            : &(*ptr)->right;
        }
    (*ptr) = malloc(sizeof **ptr);
    if (!*ptr) return;
    (*ptr)->left=NULL;
    (*ptr)->right=NULL;
    /* This could cause failures elsewhere ... */
    (*ptr)->rec = record_new (name,race, class, id, guild);
}

record *record_new(char *name, unsigned int race, unsigned int class, unsigned int id, char *guild)
{
    record *rec ;
    rec = malloc(sizeof *rec);
    if (!rec) return NULL;
    strncpy(rec->name,name, sizeof rec->name);
    rec->name[sizeof rec->name-1] = 0;
    rec->race = race;
    rec->class = class;
    rec->id = id;
    strncpy(rec->guild,guild, sizeof rec->guild);
    rec->guild[sizeof rec->guild-1] = 0;
    return rec;
}

BTW: I removed the recursion, since it is not needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.