Reputation:
This code uses a while loop to get user input and execute the appropriate command - I've reduced it to 2 commands for sake of brevity.
The Oblock object is created correctly (command "O"), as is the pointer to the base class. It appears that the calls to both objects work correctly as well.
However, after returning to the while loop, the pointer to the object appears to be lost, and attempting to access its members (command "t") causes a segfault.
I've included the example code below - my questions are afterwards.
#include<vector>
#include<iostream>
#include<string.h>
using namespace std;
class Tetramino {
private:
int squareSize;
vector<string> myShape;
public:
void setValues(int size) {
squareSize = size;
myShape = vector<string> ((size*size), ".");
}
string getValues(int i) {
return myShape[i];
}
int getSize() {
return squareSize;
}
};
class Oblock : public Tetramino {
public:
Oblock() {
setValues(2);
}
};
main () {
string input;
bool runProgram = true;
Tetramino *pBlock;
while (runProgram) {
cin >> input;
if (input == "O")
{
Oblock myBlock;
cerr << "0thi: " << myBlock.getValues(0) << endl;
Tetramino *pBlock = &myBlock;
cerr << "0thi: " << pBlock->getValues(0) << endl;
}
if (input == "t")
{
cerr << "0thi: " << pBlock->getValues(0) << endl;
}
}
return 0;
}
Thanks in advance for any advice! I searched for questions similar to this one and couldn't find one appropriate for my needs.
Upvotes: 1
Views: 609
Reputation: 5518
Your pointer becomes garbage after myBlock
goes out of scope (where the if
statement closes) which will be causing the segmentation fault when you try to access it later
if(input == "O"){
Oblock myBlock;
}
^^^^ right here myBlock becomes garbage
Also, if a user inputs "t" before "O" it will cause a segmentation fault because they will be trying to access an uninitialized pointer. You should probably get that looked at.
Upvotes: 0
Reputation: 17278
Answering your first question: Yes, the statement
Oblock yblock;
Creates an instance of Oblock on the stack. It is destroyed when the code leaves the respective block, so any pointers to it become invalid after that. To create an object that lives as long as you wish, use new
to create the object on the heap.
Upvotes: 1
Reputation: 72431
if (input == "O")
{
Oblock myBlock;
cerr << "0thi: " << myBlock.getValues(0) << endl;
Tetramino *pBlock = &myBlock;
cerr << "0thi: " << pBlock->getValues(0) << endl;
}
An object with automatic storage duration (commonly called a function-local variable) has a lifetime beginning after its declaration and ending at the end of the nearest enclosing block }
token. So myBlock
is destroyed at the end of this if statement, and can't be used again.
Also note that you have declared two different pointers named pBlock
. Assigning the inner one does nothing to the earlier one, which is still uninitialized.
Upvotes: 1
Reputation: 20282
Tetramino *pBlock
is local within its scope. You're shadowing over the one in main
with the one within the if
.
Also, myBlock
is local and will be destructed - you'll have a dangling pointer. You should allocate with new
(and delete
...)
Instead of Tetramino *pBlock = &myBlock;
do pBlock = new Oblock;
when you handle the "O" input (and handle delete pBlock
of the previous one).
Upvotes: 3