I'm trying to implement 2-3 trees and I get a segmentation fault when searching for the node to be deleted. Here's the code:
p = root;
while (p!=NULL || p->k1!=kkey || p->k2!=kkey)
{
if (kkey < p->k1)
p = p->st;
else if (kkey > p->k1 && kkey < p->k2)
p = p->mid;
else
p = p->dr;
}
So I'm trying to search for the node that contains the kkey. The debugger tells me I have a Segmentation fault in that while where I'm trying to check whether the key was found or we are still in the tree.
Your while condition looks highly suspect.
Let's say p is NULL; the while will still try to dereference p->k1 and will segfault.
Did you mean to say && ("and") instead of || ("or")?
Related
I am not C programmer but have recently taking interest in it. I am trying to modify a node of a YAML file using the C libyaml library. When I try to modify the node from an event scalar data the compiler doesn't complain but I get segmentation fault errors.
while (!done)
{
/* Get the next token. */
if (!yaml_parser_parse(&parser, &event))
goto parser_error;
//yaml_parser_scan(&parser, &token);
/* Check if this is the stream end. */
if(beginServerNodes && event.type == 8) {
beginServerNodes = 0;
}
if (event.type == YAML_SCALAR_EVENT) {
if(beginServerNodes == 1) {
//I WANT TO MODIFY THIS VALUE
printf("%s\n", event.data.scalar.value);
}
if(strcmp("servers",event.data.scalar.value) == 0) {
beginServerNodes = 1;
}
}
if (event.type == YAML_STREAM_END_EVENT) {
done = 1;
}
/* Emit the token. */
if (!yaml_emitter_emit(&emitter, &event))
goto emitter_error;
}
So while in that loop when I attempt to modify the following value
event.data.scalar.value
It must be of type yaml_char_t
yaml_char_t *newHost = "10.132.16.48:6379:1 redis-001";
event.data.scalar.value = newHost;
event.data.scalar.length = sizeof(newHost);
The compiler doesn't complain and the code run by dies with segementation fault. If have seen the examples in the libyaml test directories but nothing is intuitive as far as simply editing a node, at least not to a C newb like myself.
Libyaml expect that the values of each scalar can be removed via free(). So you need to initialize this value with malloc()ed memory:
const char* newHost = "10.132.16.48:6379:1 redis-001";
event.data.scalar.value = (yaml_char_t*)strdup(newHost);
event.data.scalar.length = strlen(newHost);
I get a pretty weird segmentation fault error when I am trying to use the same function in two different places.
printTVNode function work fine on main.
On Main:
printTVNode(headTVNode); /* Works fine here */
TVNodePointer headTopic = NULL;
TopicEmmissions(&headTopic,headTVNode,desiredTopic);
When I am trying to use printTVNode inside TopicEmmissions function a get Seg Fault.
void TopicEmmissions(TVNodePointer * headTopic,TVNodePointer headTVNode,char * desiredTopic){
TVNodePointer currentTVNode = headTVNode;
EmmissionPointer currentEmmission;
EventPointer currentEvent;
EventPointer topicSubjects = NULL;
int flag,countEvent = 1,countEmmission = 1;
printTVNode(headTVNode); /* Get Segmentation Fault here*/
...
printTVNode function:
void printTVNode(TVNodePointer headTVNode){
TVNodePointer currentTVNode = headTVNode;
while ( currentTVNode != NULL ){
printEmmission(*(currentTVNode->anEmmission));
currentTVNode = currentTVNode->next;
}
}
The problem seems to be in the following line :
printEmmission(*(currentTVNode->anEmmission));
In a situation where anEmmission is NULL, when you try to dereference it, I think you will get a segfault.
Make sure to check that anEmmission is not NULL before doing dereferencing.
I have this code, which is part of a library for handing a database (as a linked list):
char *db_getVal(char *key, Node *database) {
while(database != NULL){
if(strcmp(key, database->key) == 0){
return database->value;
}else{
database = database->next;
}
}
return NULL;
}
It works fine when I use a key that exists in database, but when I enter a key that does not, I get a segmentation fault. Why is that?
Make sure the last element's next member is set to NULL. If it's not explicitly set, it might be some junk value that's not NULL but nevertheless will cause your program to receive a segmentation fault if accessed.
When you generate the database link, make sure its last item database->next set to null
I am kinda new to C and I am trying to write a simple snake remake.
You can view the source on github: https://github.com/blackwolf12333/Snake
When building there are no warnings or errors in the output. But when I run the executable and hit enter it exit's with "Segmentation fault(core dumped)".
I am not a pro yet with pointers, I come from java, and when googling I found that it probably is a problem with pointers.
I have no idea of where it is going wrong because for as far as I know I am doing things right. The problem is when I try to loop through my snake's body_part's.
void print_snake() {
int i;
body_part *next = main_snake.head.next;
move(main_snake.head.pos.x, main_snake.head.pos.y);
addch('$');
for(i = 0; i < main_snake.length; i++) { //TODO: segfaults when 'main_snake.length'(should be this) instead of 'main_snake.length - 1'
printf("1 part");
print_body_part(next);
next = next->next;
}
}
That's from the snake.c file in the repository.
I hope you guys can help me,
greetings blackwolf12333
Before going deep through the code it is obvious that when next becomes null and next->next causes segmentation fault.
In the loop you are starting at a node next to head(main_snake.head.next). Therefore in a list of 4 objects you are processing only 3 objects. In that case iterations should be 3 instead of 4 because main_snake.length counts the head also as shown in the function initialize_snake. That is why you are getting segmentation fault.
If you want to iterate over a chained list, don't use a seperate stop condition. Your i variable and main_snake.length are not necessary. You can replace
for(i = 0; i < main_snake.length; i++)
with
body_part *next;
for(next = main_snake.head.next; next->next != NULL; next=next->next){
...
}
I am new to using threads, and I thought a good excercise would be to write a queue that works with threads. However, something is wrong, I suspect that the consumer threads try to access the same data or something. I use mutexes but I probably has misunderstood something...
I get this error (about every third run):
*** glibc detected *** ./t_queue_test: double free or corruption (fasttop): 0x0000000002114610 ***
The code is rather long so I've posted it on pastebin, but if thats incorrect, I can paste it here.
t_queue.h -> http://pastebin.com/2KYmujeE
t_queue.c -> http://pastebin.com/1wZPMwDB
t_queue_test.c -> http://pastebin.com/QKCTQWaf
I thinks the error occures in function 'get_q', and I've marked it in the code.
Thanks for any pointers or suggestions. I've digged around stackoverflow for similar questions, and I will dig some more! Valgrind doesn't show anything yet either.
Just a note for later in case pastebin goes away; the faulty code is this;
if(q->rear != NULL && q->front != NULL)
{
node_n = q->front;
*d = node_n->data;
q->front = node_n->next;
free(node_n);
}
It cleans up the front quite ok, however if the last element is removed, rear also needs to be updated to reflect the queue being empty. For example, this will do it;
if(q->rear != NULL && q->front != NULL)
{
node_n = q->front;
*d = node_n->data;
q->front = node_n->next;
if(q->front == NULL)
q->rear = NULL;
free(node_n);
}