Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

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

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
travishaberman
Asked:
travishaberman
  • 13
  • 10
1 Solution
 
Mikal613Commented:
when you debug line for line which line does it dump on?
0
 
travishabermanAuthor Commented:
Not sure how to do that... I am working on a lynix system from my school..  

-TH
0
 
Mikal613Commented:
ohhhhhhhh
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mikal613Commented:
where do you set Max??

int newmax = max*2;
0
 
travishabermanAuthor Commented:
Max is set by the default constructor.  

max = size;

//where size is an argument for the constructor and the default is 50.
0
 
travishabermanAuthor Commented:
Also,
  max is a member of the private data.

-TH
0
 
Mikal613Commented:
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
 
travishabermanAuthor Commented:
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
 
Mikal613Commented:
so max is the problem just use a tmp variable instead
0
 
travishabermanAuthor Commented:
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
 
Mikal613Commented:
ok lets do it ;)
0
 
travishabermanAuthor Commented:
Would love to "do it"... But what is "it", and how does it help?

Thank you again for all the help,

-TH
0
 
Mikal613Commented:
instead of changing the value of max use a tmp variable
0
 
travishabermanAuthor Commented:
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
 
Mikal613Commented:
jus wanna see i dont trust MS 100%
0
 
travishabermanAuthor Commented:
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
 
travishabermanAuthor Commented:
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
 
travishabermanAuthor Commented:
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
 
Mikal613Commented:
max = newmax;     // increase max by 1.5 times to match our new stack size


this also (just test)
0
 
travishabermanAuthor Commented:
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
 
travishabermanAuthor Commented:
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
 
Mikal613Commented:
it deff should not exceed
0
 
travishabermanAuthor Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 13
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now