Solved

Why am I getting a Segmentation fault (cored dump) ????

Posted on 2004-10-18
23
250 Views
Last Modified: 2010-04-17
Hello experts,

I am getting a segmentation fault at the end of running my program.  I am trying to make a dynamic array implementation of a stack.  I am fairly certain that the "segmentation fault is cause by my resize function"  I know this because,  the resize function is called in the event that I try to push on 1 more element (in this case integer) to the stack than it has room for.  I call the resize function to create a new bigger array that can handel the extra element.  I only get the "S.. fault" when this function is called.  The data comes out fine on the other end.  But upone exiting the program the "S.. Fault" is thrown.  If the function was not used then the fault is not thrown.

Here is my class definition and my resize() function.

for all of my functions, driver, and class definition.. go here and grab an extra 500 pts for solving this..

http://www.experts-exchange.com/Programming/Q_21173153.html

Please HELP,

-TH

=====================================================================================================

//resize helper function
bool stack::resize(){

       int *tempptr;
       int newmax = max*2;
       tempptr = new int[newmax];      //declare a new temporary pointer and point it at a new array 1.5 times as big as original

  //copy all the data from smaller array to new bigger array using for loop
      for(int i=0;((stackptr[i] != NULL) || (i<max)); i++){tempptr[i] = (stackptr[i]);}


   
       delete []stackptr;        //delete the thing that stackpointer is pointing at (old array)
       stackptr = tempptr;    //repoint stack pointer to the thing that tempptr is pointer is pointing too (new bigger stack)
       delete tempptr;         // delete tempptr

       max = newmax;        // increase max by 1.5 times to match our new stack size

       return true;                 // return success!
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <cstdlib>
#include <string>

using namespace std;

#ifndef STACK_H
#define STACK_H

class stack
{
   public:

      //default constructor
            stack(int size=50);

      //member functioins
            stack copy();
            bool push(const int& d);
            int pop();
            bool isempty() const;
            bool isfull() const;

     //destructor
            ~stack();

     //temporary helperfunctions
             int returntop();
             int returnmax();

     private:

              int *stackptr;      // need to declare a pointer to an array
              int max;            //declare max to keep track of array capacity
              int top;             //declare top to keep track of last item put on stack
              bool resize();   //helper function to resize stack to fit more data
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0
Comment
Question by:travishaberman
  • 13
  • 10
23 Comments
 
LVL 48

Accepted Solution

by:
Mikal613 earned 500 total points
ID: 12343886
when you debug line for line which line does it dump on?
0
 

Author Comment

by:travishaberman
ID: 12343894
Not sure how to do that... I am working on a lynix system from my school..  

-TH
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12343907
ohhhhhhhh
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12343913
where do you set Max??

int newmax = max*2;
0
 

Author Comment

by:travishaberman
ID: 12343921
Max is set by the default constructor.  

max = size;

//where size is an argument for the constructor and the default is 50.
0
 

Author Comment

by:travishaberman
ID: 12343924
Also,
  max is a member of the private data.

-TH
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12343981
how did you Declare Max

Because perhaps it may be a Const or static

Your changing the value of max maybe you should declare a Tmp holder
0
 

Author Comment

by:travishaberman
ID: 12344009
max is not declared as const or static.. Also, here is an interesting twist.  If in my driver, I use the copy constructor to copy astack, and then I pop the values off that stack instead of the original... I DONT GET A "SEGMENTATION DEFAULT"...

here is my copy constructor..

=====================================================================

//copy constructor
stack::stack(const stack& astack){

  //Note: need to preform memmory check here
  max = astack.max;
  top = astack.top;
  stackptr = new int[max];

  for(int i=0; i<=top; i++) {stackptr[i]=astack.stackptr[i];}
}

============================================================================

0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12344028
so max is the problem just use a tmp variable instead
0
 

Author Comment

by:travishaberman
ID: 12344029
I am willing to try this but I am not sure how that will help or what it is that you would have me do...

-TH
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12344046
ok lets do it ;)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:travishaberman
ID: 12344050
Would love to "do it"... But what is "it", and how does it help?

Thank you again for all the help,

-TH
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12344061
instead of changing the value of max use a tmp variable
0
 

Author Comment

by:travishaberman
ID: 12344063
How is it that max is the problem?  I should have access to this data because I am working within a private function of the class that contains the data.. Right?

hmmm..

-TH
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12344076
jus wanna see i dont trust MS 100%
0
 

Author Comment

by:travishaberman
ID: 12344079
When I exit this function how will my data structure keep track of max?  max needs to increase to the correct size of the array.  I am using the function to increase the size of the array so, in my mind, it would seem reasonable to increase max to match the array size...  I am not doubting you here but I am still not sure what it is that a temp variable will help with..

Also,  doesn't the line of this function

newmax = max*2;

.... work for my temp?

-TH
0
 

Author Comment

by:travishaberman
ID: 12344085
So.. If I understand you right I am going to put max into a temp. then use the temp,.,,  but I still will change max at the end?

I will try right now..  Give me a second..

-TH
0
 

Author Comment

by:travishaberman
ID: 12344107
OK.. I if I understood you right then here is my new copy function..

but sadly I didn't meet w/any success...
//////////////////////////////////////////////////////////////

//resize helper function
bool stack::resize(){
  int tempvar = max;
  int *tempptr;
  int newmax = tempvar*2;
  tempptr = new int[newmax];  //declare a new temporary pointer and point it at a new array 1.5 times as big as original

  //copy all the data from smaller array to new bigger array using for loop
  for(int i=0;((stackptr[i] != NULL) || (i<tempvar)); i++){tempptr[i] = (stackptr[i]);}


  //delete stackptr;  //delete the thing that stackpointer is pointing at (old array)
  delete []stackptr;
  stackptr = tempptr; //repoint stack pointer to the thing that tempptr is pointer is pointing too (new bigger stack)
  delete tempptr;     // delete tempptr

  max = newmax;     // increase max by 1.5 times to match our new stack size

  return true;        // return success!
}

///////////////////////////////////////////////////////////////////////////



-TH
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12344122
max = newmax;     // increase max by 1.5 times to match our new stack size


this also (just test)
0
 

Author Comment

by:travishaberman
ID: 12344147
Nope.. That didn't do it.. But I think I might have found the problem.. if I go into the function and cout the value of i and max (shown below) then at some point, i exceeds max by 1 value!!! that means that its ouside the array right?

-TH


=========================================
//resize helper function
bool stack::resize(){
  int tempvar = max;
  int *tempptr;
  int newmax = tempvar*2;
  tempptr = new int[newmax];  //declare a new temporary pointer and point it at a new array 1.5 times as big as original

  //copy all the data from smaller array to new bigger array using for loop
  for(int i=0;((stackptr[i] != NULL) || (i<tempvar)); i++){tempptr[i] = (stackptr[i]);

  cout << i << " " <<  max << endl;
}


  //delete stackptr;  //delete the thing that stackpointer is pointing at (old array)
  delete []stackptr;
  stackptr = tempptr; //repoint stack pointer to the thing that tempptr is pointer is pointing too (new bigger stack)
  delete tempptr;     // delete tempptr

  tempvar = newmax;     // increase max by 1.5 times to match our new stack size

  return true;        // return success!
}
0
 

Author Comment

by:travishaberman
ID: 12344167
OK ... We are so close..  again thank you thank you for the help...

the above output gives...  

i  / max
-----------
92  100
93  100
94  100
95  100
96  100
97  100
98  100
99  100
100  100
101   100   <- I am not sure why this happens but this would appear to be the problem!  any thoughts?

200  101

200  102

200  103


-TH
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12344209
it deff should not exceed
0
 

Author Comment

by:travishaberman
ID: 12344251
I am going to sign off on this for now.. I think I can find it from here .. Thank you for all the help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 78
strCopies  challenge 17 74
move one pixel 4 42
Tembedded WB animatid gifs not animated on some pcs 2 36
This article will show, step by step, how to integrate R code into a R Sweave document
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now