Saturday 15 May 2010

Segmentation Fault (core dumped) C++ error, pointer related -



Segmentation Fault (core dumped) C++ error, pointer related -

from understand far; issue i'm having pointer related. relatively new c++ , while think know problem occurring, having problem understanding why occurring.

below constructor linked list of nodes store string (called "data") each , want have method can phone call on linked list (initialized in main) prints out contents of each node. started attempting print contents of first node (which pointed root) ran segmentation fault error. have been fiddling code , looking online resources hours no avail. input appreciated.

#include <string> #include <iostream> #include "node.h" #include "linked.h" linked::linked(int size){ node *root; //the start of linked list, remains static root = new node; // sets point @ node node *conductor; // point each node traverses list conductor = root; // conductor points first node if ( conductor != 0 ) { while ( conductor->next != 0) conductor = conductor->next; } (int = 0; < size; i++){ //std::cout << conductor->data; conductor->next = new node; // creates node @ end of list conductor = conductor->next; // points node } } void linked::printmemory(){ int memorycheck[32] = { }; // node *conductor; //creates conductor conductor = root; //sets conductor point same node root does, issue may occur here. why? (int = 0; < 32; i++){ std::cout << conductor->data; }

this header file linked }

#ifndef linked #define linked #include <iostream> #include <string> #include "node.h" class linked { public: node *root; linked(int size); void addprogram (std::string name, int size); void printmemory (); }; #endif

i thinnk have problem in constructor.

linked::linked(int size){ node *root; //the start of linked list, remains static ... }

here declare variable root, , not same root in class definition! seek remove line code.

c++ pointers segmentation-fault

No comments:

Post a Comment