简体   繁体   中英

C++ Member variables being overwritten by non-member functions

I am working on a project that uses a linkedList class that I have written. Each node in the list contains a string which seems to be overwritten when there is any function call outside of the object. The variable is private and is only ever set within the constructor. Here is a stripped down version of the code which reproduces the error:

linkedList.h:

#include <cstring>
#include <iostream>

class Node{
private:
  std::string value;
  Node* prev_node;
public:
  Node* next_node = nullptr;
  Node(std::string value_in, Node* prev){
    this->value = value_in;
    this->prev_node = prev;
    this->next_node = nullptr;
  }

  void print_value(){
    std::cout << "value = " << this->value << '\n';
  }
  void print_prev(){
    std::cout << "prev = " << this->prev_node << '\n';
  }
  Node* get_prev(){
    return this->prev_node;
  }
};

class linkedList{
public:
  Node start_node = Node((std::string) "", (Node*) nullptr);
  Node* current_node = &start_node;

  void prepend_node(std::string value){
    Node new_node = Node(value, &start_node);
    new_node.print_value();
    new_node.next_node = start_node.next_node;
    start_node.next_node = &new_node;
  }

  void return_to_start(){
    current_node = &start_node;
  }

  void scroll_up(){
    if(current_node->next_node){
      current_node = current_node->next_node;
    }
  }

  void scroll_down(){
    if(current_node->get_prev()){
      current_node = current_node->get_prev();
    }
  }
};

(One thing of note here is that the prepend_node function will insert a node at position 2 of the list. This is intentional)

main.cpp:

#include <iostream>
#include "linkedList.h"

int main(){
  linkedList foo;

  foo.prepend_node("value1");
  foo.prepend_node("value2");

  foo.current_node->next_node->print_value();

  foo.scroll_up();
  foo.current_node->print_value();

  foo.scroll_up();
  foo.current_node->print_value();
  return 0;
}

output:

value = value1
value = value2
value = 
value = 
Segmentation fault

Some gdb output:

(gdb) b linkedList.h:20
Breakpoint 1 at 0x101e: file linkedList.h, line 20.
(gdb) run
Starting program: /path/to/main

Breakpoint 1, Node::print_value (this=0x7fffffffdd50) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) p this->value
$1 = "value1"
(gdb) c
Continuing.
value = value1

Breakpoint 1, Node::print_value (this=0x7fffffffdd50) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) c
Continuing.
value = value2

Breakpoint 1, Node::print_value (this=0x7fffffffdd50) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) c
Continuing.
value = 

Breakpoint 1, Node::print_value (this=0x7fffffffdd50) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) this->value
Undefined command: "this->value".  Try "help".
(gdb) c
Continuing.
value = 

Breakpoint 1, Node::print_value (this=0xa00000000) at linkedList.h:20
20      std::cout << "value = " << this->value << '\n';
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b735b0 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6

The issue is pretty stupid, I forgot to use new . Below is a fixed version of prepend_node that works:

void prepend_node(std::string value){
    Node* new_node = new Node(value, &start_node);
    new_node->print_value();
    new_node->next_node = start_node.next_node;
    start_node.next_node = new_node;
  }

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