Вопрос:

Why is the process being terminated after my code runs? Process returns 255

c pointers struct linked-list nodes

66 просмотра

3 ответа

10 Репутация автора

I am new to pointers and creating linked lists. The code is only a part of the entire thing. I tried to make this part simpler for testing and debugging but I still can figure it out. Can someone help me figure out with the process is being terminated after it executes the given code?

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

#define SPADE "SPADE"
#define CLUB "CLUB"
#define DIAMOND "DIAMOND"
#define HEART "HEART"

typedef struct Card{
    int value;
    char suit[10];
}Card;

typedef struct BagNode {
    Card* card;
    struct BagNode* next;
}BagNode;

int main() {

    BagNode* head = NULL;
    BagNode* tail = NULL;

    const char* suit = SPADE;
    int value = 6;


    if(tail == NULL)
    {
        printf("add funct works\n");
        BagNode* newNode = (BagNode*)malloc(sizeof(BagNode));   // allocate space
        newNode->card->value = value;                                  // assign value
        strcpy(newNode->card->suit, suit);                      // assign value
        head = newNode;
        tail = newNode;
        printf("added cards\n");
    }
    else{
        BagNode* newNode = (BagNode*)malloc(sizeof(BagNode));   // allocate space
        newNode->card->value = value;                                  // assign value
        strcpy(newNode->card->suit, suit);                      // assign suit
        tail->next = newNode;
        tail = newNode;
    }
}

This is what the output looks like:

add funct works
added cards

Process returned 255 (0xFF)   execution time : 6.898 s

Why is it returning 255?

Any direction would be greatly appreciated!

**note: I'm writing in C

Автор: blazek Источник Размещён: 08.11.2017 11:03

Ответы (3)


1 плюс

71 Репутация автора

My guess is that when you're doing

newNode->card->value

card is a null pointer, so assigning a null pointer's member a value is what is causing your program to crash

Автор: adrotter Размещён: 08.11.2017 11:11

1 плюс

3252 Репутация автора

Решение

C pointers are trouncing you my friend :) Your program is crashing because you are accessing a pointer that you have not initialized, so the object it is supposed to point at does not exist.

Remember a pointer is just a reference to an object. In your case, you first tell us that card is a pointer to a Card object, here.

typedef struct BagNode {
    Card* card;
    struct BagNode* next;
}BagNode;

But remember that you have not yet told us the location of the actual Card object Something like

Card mycard;
newNode->card=&mycard;//pointer equals to address of mycard

or

newNode->card=(Card*)malloc(sizeof(Card));// works because now there is an actual memory location pointed at.

In short, the pointer is pointing at a non existent Card!!!

The moment you try to access this non existent Card, the program crashes

newNode->card->value

The easiest solution is to change the definition of your struct such that card is the actual object not a pointer

typedef struct BagNode {
    Card card;//actual object not the pointer to an object
    struct BagNode* next;
}BagNode;

Then use it like this

newNode->card.value

Or else you can initialise your card pointer with the address of a card object before trying to use it.

Автор: Dr Deo Размещён: 09.11.2017 12:15

0 плюса

11297 Репутация автора

OS is throwing Segmentation fault because you should allocate memory "card" because its a uninitialized pointer pointing to some invalid address.

  so for this add this line :
              newNode->card=malloc(sizeof(Card));
    before this line 
              newNode->card->value = value; 

and after main() function completed successfully ,it return 0 or 1. As of my knowledge in your case its failing so its returning 1 i.e its returning 1 in 2nd byte which is nothing but 255.

Автор: Achal Размещён: 09.11.2017 05:20
Вопросы из категории :
32x32