Reputation: 173
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;
Upvotes: 0
Views: 355
Reputation: 44240
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.
Upvotes: 0
Reputation: 53310
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.
Upvotes: 1