I've got a struct, which I want to sort by the ID and afterwards remove the entries with a NULL
ID. But I get an Adress Boundary Error. Not really sure what the problem is.
typedef struct data_id
{
char* id;
int marker;
int key;
} data_id;
My compare function:
int cmp(const void *p1, const void *p2)
{
const data_id *v1 = (data_id *)p1;
const data_id *v2 = (data_id *)p2;
if (v1->id < v2->id)
return -1;
else if (v1->id > v2->id)
return +1;
else
return 0;
}
and I'm calling qsort in the main function with:
qsort(nodeList, num_id, sizeof(nodeList),cmp);
num_id
is the number
of entries.
But anyway you need to show a minimal reproducible example
I'm reading data from stdin and create the list.
void create_list(input){
nodeList = realloc(nodeList, sizeof *nodeList * 10000);
if (input == 65) // A
{
stop_flag = true;
s_flag = true;
}
else if (input == 73) // I
{
moves_flag = true;
}
else if (s_flag == true && (islower(input) || isdigit(input)))
{
start_node = input;
s_flag = false;
}
else if (moves_flag == true && isdigit(input))
{
num_moves = (char)input - '0';
moves_flag = false;
}
else if ((islower(input) || isdigit(input)) && !stop_flag )
{
if (m_flag == true && isdigit(input))
{
nodeList[tmp_id].marker = digitconcat(nodeList[tmp_id].marker,input);
}
else if (m_flag == false && (isdigit(input)||islower(input)))
{
buffer[0] = (char)input;
if (buffer_size == 0)
{
nodeList[num_id].id = strconcat(1, buffer);
buffer_size++;
}
else
nodeList[num_id].id = strconcat(2, nodeList[num_id].id, buffer);
m_flag = false;
}
}
else if (input == '-' && !stop_flag)
{
m_flag = true;
}
else if (input == '\n' && !stop_flag)
{
tmp_id++;
num_id++;
buffer_size = 0;
nodeList[tmp_id].marker = 0;
nodeList[num_id].key = num_id;
m_flag = false;
}
else if ((input == ',' || input == ':') && !stop_flag)
{
buffer_size = 0;
num_id++;
nodeList[tmp_id].marker = 0;
nodeList[num_id].key = num_id;
m_flag = false;
}
}
The concatenation function for strings and digits are:
unsigned digitconcat(unsigned x, unsigned y)
{
y = (char)y - '0';
unsigned pow = 10;
while (y >= pow)
pow *= 10;
return x * pow + y;
}
char *strconcat(int count, ...)
{
va_list ap;
int i;
// Find required length to store merged string
int len = 1; // room for NULL
va_start(ap, count);
for (i = 0; i < count; i++)
len += strlen(va_arg(ap, char *));
va_end(ap);
// Allocate memory to concat strings
char *merged = calloc(sizeof(char), len);
int null_pos = 0;
// Actually concatenate strings
va_start(ap, count);
for (i = 0; i < count; i++)
{
char *s = va_arg(ap, char*);
strcpy(merged + null_pos, s);
null_pos += strlen(s);
}
va_end(ap);
return merged;
}
An example Input could be:
aqwe:b66,g
b66:g,l-23452
asfag:l
l:-2424
A:g
I:5
The strings seperated by a :
, ,
or LF
are the IDs
. After the -
follows the marker. The last two rows don't need to be saved in the list.
I also didn't figure out yet why the last entry of my list gets added to it, because I specifically set stop_flag
, to stop adding ID
, Key
or Marker
after it reaches the A
in the second to last line.
What is nodeList?
The data is supposed to represent a graph. Tho nodes
are seperated by a :
, ,
or LF
. The weight(marker)
is the number after the dash, which belongs to the first node of the line it is in. Furthermore I give every node a specific 'key'. Ohh and every node that is on the same line is connected. So nodeList
is the array that stores information about every node in the graph.
But I just thought that maybe an array isn't the best data structure to store the data in, because it's harder to get rid of duplicates.