You are here:

C++/how to create dead objects

Advertisement


Question
hi friend , can u please tell me how to create the dead objects and also the meaning of dead objects.

Answer
Hi Prasanna!

Thanks for your question. A dead object is an object that can't be reached by the program. For example, if I create an object with a new command, and then fail to free the object after it goes out of scope (after the program is no longer able to use it), then it becomes a dead object. These dead objects are cleaned up using garbage collection. Garbage collection is especially tricky in C++, so it's best to always remember to call delete() on an item when you're finished with it.

Dead objects are bad because they take up space in memory that could be better used for other tasks.

Here's an example of a program that will leave us with some
"dead objects":

void GarbageSource();
int main()
{
GarbageSource();

// We could have a whole program here,
// while that data remained orphaned!

return0;
}

void GarbageSource()
{
char* ArrayOfStuff = new char[30];
UserDefinedData AnObject = new UserDefinedData();
// continue the function
}

Notice that after that function closes, we've got some orphaned data (assuming we never called delete). We can also create orphaned data by resizing an array without first clearing the data that was previously allocated (for example, if we resized the array above to size 20).

Hope that works for you, feel free to let me know if that needs clarifying.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sean Wcisel

Expertise

I can answer most beginner and intermediate level questions. My strengths are various algorithms and data manipulation. My background might be best suited to anything from basic CPP and file I/O, to algorithms involving recursion, hashing, binary search trees, and so on. I've done some tutoring in my free time, both online and in person. I also have quite a bit of debugging experience. I have some experience with game code, specifically using D3D based game engines. I only speak English, but I believe I do so very well :). I'm completely unable to answer questions about coding drivers for hardware, and many library specific issues.

Experience

I've studied CPP for roughly four years now, with a combined eight years of object oriented programming experience. I have worked professionally in the field for about a year. I have also had some tutoring experience in CPP and C#

Organizations
IEEE, Western Michigan University Computer Club

Education/Credentials
Western Michigan University

©2016 About.com. All rights reserved.