473,378 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Segmentation fault help

4
ok i have narrowed the fault down to two lines 544 and 545, but i cant see anything that is wrong with it here is the code

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. #undef NULL //un defines anything from a class that has the same name as one of your constants
  7.  
  8. typedef int element;
  9.  
  10. const int NULL = 0;
  11. const char sentinel = '#';
  12.  
  13. class listnode{
  14.  
  15.     public:
  16.  
  17.         element data;
  18.  
  19.         listnode* next;
  20.  
  21.     };
  22.  
  23.  
  24. class Llist{
  25.  
  26.     private:
  27.  
  28.         listnode* head;
  29.  
  30.         listnode* tail;
  31.         char vignum;
  32.  
  33.     public:
  34.  
  35.         void read();
  36.  
  37.         void print();
  38.         void clean();
  39.         void ReadBackward();
  40.         char read_element();
  41.         void Inserthead(element);
  42.         void inserttail(element);
  43.         element DeleteHead();
  44.         void steal(Llist&);
  45.         void append(Llist&);
  46.         void duplicate(Llist&);
  47.         void reverse();
  48.         void Mergeordered(Llist&, Llist&);
  49.         void Printinreverse();
  50.         void Mainmenu();
  51.         void Helpmenu();
  52.         void addvigesimalnum();
  53.         void newvigesimalnum();
  54.         void multiplyvigesimalnum();
  55.         int digitconvertor(char);
  56.         char convertback(int);
  57.         void Multiadd(int);
  58.  
  59.         Llist();
  60.         ~Llist();
  61.  
  62.  
  63.  
  64.  
  65.  
  66.     };
  67.  
  68. void Llist::addvigesimalnum(){
  69. //adds to the current vigesimalnum linked list
  70.  
  71.     char usernum;
  72.     char cnumber;
  73.     char cnum1;
  74.     char cnum2;
  75.     int remainder;
  76.     int num1;
  77.     int num2;
  78.     element number;
  79.     Llist helper;
  80.     Llist combo;
  81.     listnode * temp1;
  82.  
  83.     listnode * temp2;
  84.  
  85.     temp1 = head;
  86.  
  87.     temp2 = helper.head;
  88.     helper.head = NULL;
  89.  
  90.     combo.head = NULL;
  91.     remainder = 0;
  92.     num1 = 0;
  93.     num2 = 0;
  94.  
  95.     cout << "What is the vigesimal number you would like to add?" << endl;
  96.     cout << "Enter " << sentinel << " when you are done inputting your number." << endl;
  97.     helper.ReadBackward();    //should make the numbers entered go to helper and not change the native object
  98.  
  99.     cout << "here one" << endl;
  100.  
  101.     while (temp1 != NULL){ //temp1 and temp2 are the temp listnodes that 
  102.  
  103.         cout << "here 2" << endl;        
  104.  
  105.         cnum1 = (temp1 -> data);    // Line 544
  106.         cnum2 = (temp2 -> data);    // Line 545
  107.  
  108.         cout << "here three" << endl;        
  109.  
  110.         num1 = digitconvertor(cnum1);
  111.         num2 = digitconvertor(cnum2);
  112.  
  113.         remainder = (num1) % (num2);
  114.             cout << num1 << " " << num2 << " " << remainder << endl;
  115.         if (remainder == 0){
  116.             number = (num1) + (num2);
  117.             cnumber = convertback(number);
  118.             combo.inserttail(cnumber);
  119.             temp1 = temp1 -> next;
  120.             temp2 = temp2 -> next;
  121.             }
  122.         else {
  123.             number = (num1) + (num2) + remainder;
  124.             cnumber = convertback(number);
  125.             combo.inserttail(cnumber);
  126.             temp1 = temp1 -> next;
  127.             temp2 = temp2 -> next;
  128.             }
  129.         }
  130.     while (temp1 != NULL){
  131.  
  132.         combo.inserttail(temp1 -> data);
  133.  
  134.         temp1 = temp1 -> next;
  135.  
  136.         }
  137.  
  138.  
  139.  
  140.     //phase 3 harvest remaining from second list
  141.  
  142.     while(temp2 != NULL){
  143.  
  144.         combo.inserttail(temp2 -> data);
  145.  
  146.         temp2 = temp2 -> next;
  147.  
  148.         }
  149.     duplicate(combo); // should change the native object to what the combo is
  150.     Printinreverse();
  151.     Mainmenu();
  152. }
  153.  
any help would be awesome!!!
Apr 8 '10 #1
3 2283
donbock
2,426 Expert 2GB
The loop condition (line 540) insures that temp1 is not NULL so the dereference on line 544 ought to be ok. How do you know temp2 isn't NULL? If temp2 became NULL then the dereference on line 545 could be expected to trip a segmentation fault.
Apr 8 '10 #2
SD14
4
when i change the loop condition to (temp1 != NULL)&&(temp2 != NULL) it doesnt enter the loop at all
Apr 8 '10 #3
Banfa
9,065 Expert Mod 8TB
Well that is the correct test to have to ensure you don't dereference any NULL pointers.

This suggest problems in the logic of your code. Look at how temp2 is initialised ...
Line 522 - the variable is created, it is not initialised to anything
Line 526 - temp2 is set to the value of helper.head
Line 540 - temp2 is tested against NULL

What value does helper.head have?
Line 518 - helper is created as an Llist using the default constructor which assigns NULL to head.
Line 526 - temp2 is set to the value of helper.head

The way the function is written temp2 will always have a value of NULL at the start of the loop and one of the first things the loop does is dereference temp2,
temp2 -> data.

This logic is bound to fail.

Since in the rest of the loop code temp2 is only changed by referencing itself

temp2 = temp2 -> next;

Something is wrong in the initialisation of this function which lets temp2 start out as NULL in the first place.
Apr 9 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Justin Tang | last post by:
Hi I am wondering about the segmentation fault in PHP. Namely, I'm running PHP version 4.6.9 on my server right now and when I try to process a large piece of text via textarea(3k+), the resulting...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
4
by: yinglcs | last post by:
Hi, i get this segmentation fault in my c++ program: My question is 'it shows inMethodStr is not null, how come this can have *inMethodStr.Ptr segmentation fault? Program received signal...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.