Reputation: 171
I'm practicing with dynamic memory allocation, so I created an object here, a custom data type called vector3d, a math vector I defined in a different file. However, when I try to delete the object using delete, I get a compiler error that says that the test vector was not defined in that scope. However, I have a debug cout print function in the object destructor to make sure it is called and it is not being called.
int main()
{
while(continueProgram == true)
{
vector3d *a;
a = new vector3d("Test vector");
std::cout << a->getName() << std::endl;
vector3d b("Test vector 2");
std::cout << b.getName() << std::endl;
continueProgram = prompt(); // Boolean
}
delete(a); // <-- "Error: 'a' was not declared in this scope"
return 0;
}
Here are the constructor and destructor, as well as the vector3d print function.
vector3d::vector3d(std::string s)
:name(s)
{
std::cout << "Vector constructor initialized" << std::endl;
}
vector3d::~vector3d()
{
std::cout << "Vector destructor initialized" << std::endl;
}
void vector3d::printVector()
{
std::cout << "Name: " << this->name << std::endl;
}
I don't understand why I can't call the delete function here, since I thought the whole point of using the New keyword was to preserve the object past its scope. Otherwise I would just initialize as
vector3d a("Test vector");
as I did with vector3d b, right? I don't understand why I would get a scope error here
Here is the console output of the program at runtime when I comment out the delete line and type 'n' to end the program:
Vector constructor initialized
Test vector
Vector constructor initialized
Test vector 2
Continue program? y/n
n
Vector destructor initalized
So there's the destructor that only appears for vector3d b, but not vector3d a
Upvotes: 0
Views: 1437
Reputation: 48938
It's true that objects allocated with new
exist as long as they aren't delete
d by the program (or the OS after some time). BUT you still need the memory address to delete the object.
Without a pointer to the allocated object, you can't delete it. And that's what your compiler is complaining about: the pointer a
doesn't exist outside of the while loop! And so, at the point where it sees delete(a);
, it has not the slightest idea what a
could be, as as far it is concerned it has not been defined yet.
So, the object itself still exists after the while loop, but not the pointer pointing to the object!
Upvotes: 1
Reputation: 71
In your case you are declaring vector3d *a
inside the while
. Which means you can not use a
outside the scope of while
.
while(true)
{//start of the while scope
int a = 0;
}//end of the while scope
In your code, you are not deleting the vector3d
that you are allocating in the memory every time inside the while using new
.
to delete a
, move delete(a)
inside the scope where a
was defined.
Note: scopes can be created anywhere for example
int main()
{//start of the main scope
int a = 0;
{//start of a new scope
int b = a; //a can be used here because this scope inside the main scope
}//end of scope
b = 2; //error b is out of scope
}//end of main scope
you can read more about scopes here. Hope this helps.
Upvotes: 0
Reputation: 13924
The error message is self explanatory; though the object created using new
continue to exist even after the while
loop, but the local variable a
pointing to it ceases to exist as it goes out of scope upon crossing innermost closing brace }
:
while(continueProgram == true)
{
vector3d *a;
a = new vector3d("Test vector");
//....
} // `a` goes out of scope
delete(a); // <-- "Error: 'a' was not declared in this scope"
Put delete(a)
before the closing braces.
Upvotes: 1
Reputation: 59997
You need the delete in the loop.
I.e. change the lines
}
delete(a); // <-- "Error: 'a' was not declared in this scope"
to
delete(a); // <-- "Error: 'a' was not declared in this scope"
}
So that a
is in scope
Upvotes: 0