简体   繁体   中英

C - Accessing struct attribute after assignment causes Segmentation Fault

I have 2 structs: wires and wireLLN (a linked list node for wires). The idea is that the findWire() function looks through the linked list for nodes which have a wire with the same name as the given wire (wireName). When adding the first wire, this runs no problem as the head node is null and so a new node is created with the wire as the node's 'wire' attribute. The printf call in addNode shows this. However, on the second run of findWire(), trying to access the 'wire' attribute of the head node causes a segmentation fault. (I have commented where the segfault appears in the code)

typedef struct wires {
  bool value;
  char* name;
} Wire;

typedef struct wireLLN {
  Wire wire;
  struct wireLLN * nextNode;
} WireLLN;

//head of the linked list
WireLLN * headWire = NULL;

// adds a node to the linked list
void addNode(WireLLN* head, WireLLN* node){
  if (headWire == NULL){
    headWire = node;
        printf("Head node was null. Adding node with wire name: %s\n", headWire->wire.name); //this prints no problem
    }
  else if (head->nextNode == NULL)
    head->nextNode = node;
  else
    addNode(head->nextNode, node);
}

//finds if a wire with a given name already exists, if not returns null
Wire * findWire(char wireName[], WireLLN * head){
  if (headWire != NULL){
        puts("head wasnt null");
        printf("HEAD NODE ADDRESS: %s\n", head);
        printf("HEAD WIRE: %s\n", head->wire); //SEG FAULT HERE
    if (strcmp(head->wire.name, wireName) == 0){
            puts("1");
            return &head->wire;
        } else if (head->nextNode == NULL){
            puts("2");
            return NULL;
        } else {
            puts("3");
            return findWire(wireName, head->nextNode);
        }
  } else return NULL;
}


// assigns a wire to a gate if it exists, otherwise creates a new one then assigns it
Wire assignWire(char wireName[]){
  Wire * result = findWire(wireName, headWire);
  if (result == NULL){
    Wire wire = makeWire(wireName);
    WireLLN node;
    node.wire = wire;
    addNode(headWire, &node);
    return wire;
  } else {
    return *result;
  }
}

Thanks for your time.

You have memory deallocation problems. Yoiu forgot that once the function stops running your memory will be deleted. This happens in the assignWireFunction.

you might want to change it to:

Wire assignWire(char wireName[]){
  Wire * result = findWire(wireName, headWire);
  if (result == NULL){
    //to prevent the data being lost after the function returns
    WireLLN* node = (WireLLN*)malloc(sizeof(WireLLN));
    node->wire = makeWire(wireName);
    addNode(headWire, node);
    return node->wire;
  } else {
    return *result;
  }
}

The reason why your NULL checks didn't warn you were, because the pointer you gave the addNode stopped being allocated memory once the function returned. Then you access that memory (the adress is the same) but its not memory that you were allowed to write anything to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM