0

I am trying to solve a problem of a cat escaping a flooding room and saving the moves like U for Up , D for down , R for right and L for Left. The problem that I am having is with the following command with no compiler errors when I built my program.

new_node->kin = k; 

The program just freezes there. Both kin and k are of type string. Another thing is that at the start when I am creating the list and call the same void but with " " and not a variable for string k it runs okay. When I use the same thing after to insert lets say new movement for my cat it doesn't work.

More explanation: I use a function that gets the head of my list (of possible moves of the cat) the coordinates of the position I am at the "map" and a string k that is just a char U/D/R/L that I want to add to the nodes variable kin that is of type string and the so far movement of my cat.

I used cout command and found out that it is just that command that has the problem. So far at least. I tried to call the void with " " instead of a variable and does the same thing except when I create the list where it runs okay.

struct cates {
    int x;
    int y;
    string kin;
    struct cates* next;
    struct cates* prev;
};

void instertc(cates** head, int z, int s, string k)
{
    if ((*head) == NULL) {
        cates* new_node = (cates*)malloc(sizeof(cates));
        new_node->x = z;
        new_node->y = s;
        new_node->kin = k; //!!! p.s. i tried "" instead of k
        new_node->next = NULL;
        new_node->prev = NULL;
        (*head) = new_node;
    }
}

I call the void like this: instertc(&c , k , i, kinisi);, where:

cates *c;
int k,i;
string kinisi;

P.S. calling it at first with kinisi= " ", it creates the list ok. I just want it to add the string k (voids input) to the nodes->kin.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 2
    `cates *new_node =(cates *)malloc(sizeof(cates));` you can't use malloc on objects. It won't call the constructors. So `kin` is never constructed (if you use it in any way you will have Undefined Behavior). https://stackoverflow.com/questions/2995099/malloc-and-constructors – drescherjm Mar 30 '19 at 12:57
  • 2
    Uninitialized local (non-static) variables are really uninitialized. They will have an *indeterminate* value, that could be seen as random or garbage. Now think about that and what the value of e.g. `c` might be without initialization. – Some programmer dude Mar 30 '19 at 12:58

1 Answers1

0

Unfortunately you do not give us a self-contained piece of source code, so it is difficult to reproduce the exact problem you're having. I have tried to include below such a self-contained piece of source code to mimic your problem; but it runs fine with GCC/G++ v8.3, and I am at a loss to tell what is different in your environment. You might start by trying the below code in your environment. If it works, then find what's different.

Philosophy: it seems to me that you are writing C code in C++. IMHO it is not a comfortable position long term; you might have to make a choice. If you go for C, you will need to check whether or not malloc() returns a valid pointer, as some other parts of your code might have exhausted memory. As is, you run the risk of corrupting the heap. If you go for C++, operator new() will take care of generating a runtime exception for you, should memory become exhausted. By the way, it is far less risky to use the well debugged STL packages for doubly-linked lists, rather than "re-inventing the wheel" and managing lists manually.

#include  <cstdlib>
#include  <string>
#include  <iostream>

using std::string;

struct cates {
    int x;
    int y;
    string kin;
    struct cates* next;
    struct cates* prev;
};

void insertc(cates** head, int z, int s, string k)
{
    if ((*head) == NULL) {
        std::cout << "Allocating ..."  << std::endl;
        cates* new_node = new cates;
        std::cout << "Allocated.\n" << std::endl;
        new_node->x = z;
        new_node->y = s;
        new_node->kin = k;  //!!! p.s. i tried "" instead of k
        new_node->next = NULL;
        new_node->prev = NULL;
        (*head) = new_node;
    }
}


int main(int argc, char* argv[])
{
    cates *c = nullptr;
    int    k = 0;
    int    i = 0;
    string kinisi{"D"};

    insertc(&c , k , i, kinisi);

    // loop thru list:
    cates*  ptn = c;
    while (ptn != nullptr) {
        std::cout << "Scanning node ..." << std::endl;
        std::cout << "node.kin: " << ptn->kin << std::endl;
        ptn = ptn-> next;
    }
    return 0;
}


jpmarinier
  • 4,427
  • 1
  • 10
  • 23