-
How to delete an array of pointers?
Good day!
Offtopic:
I am a student. And I got really used to java. So when it comes to C++ homeworks, situation gets very tricky.
So here is the code:
Code:
#include <iostream>
//
//
//
using namespace std;
struct test_ar {
int *val;
double *val2;
test_ar() {
val = NULL;
val2 = NULL;
}
~test_ar() {
delete val;
delete val2;
cout << "test_ar destroyed" << endl;
}
};
int
main(int argc, char** argv) {
test_ar **test = new test_ar*[10];
int x = 10;
double y = 5.1;
for (int c = 0; c < 5; c++) {
test[c] = new test_ar;
test[c]->val = &x;
test[c]->val2 = &y;
}
for (int c = 5; c < 10; c++) {
test[c] = NULL;
}
delete[] test;
return (EXIT_SUCCESS);
}
It works fine, but I need to get test_ar destructor working, when I delete an array of pointers to test_ar; When console starts, I don't see anything like test_ar destroyed; Am I doing something wrong?
Thanks, Andrey;
-
you did it correctly. But you did not delete the memory properly. For this construct, youll need to loop over the inner pointers and delete them first (the actual objects).
so where you had it assigned to null in that second loop, instead use a delete command.
-
I changed test[i] = NULL to [I]delete test; , but nothing happend.
But then I chenged the lest part of the code
delete[] test; into
Code:
for (int i = 0; i < 10; i++) {
if (test[i] != NULL)
delete[] test[i];
}
and I got this in the console:
test_ar destroyed
test_ar destroyed
test_ar destroyed
test_ar destroyed
test_ar destroyed
test_ar destroyed
test_ar destroyed
Segmentation fault (core dumped)
[Press Enter to close window]
after some debugging, I found out that
delete[] test[0]; instead of
Code:
for (int i = 0; i < 10; i++) {
if (test[i] != NULL)
delete[] test[i];
}
gives the same result.
Why I see test_ar destroyed 7 times instead of 5(or 1)? And how could I fix the
Segmentation fault (core dumped) message?
Maybe this could be helpful information:
I am using linux, and as IDE I use netbeans.
-
you did this?
:
...
for (int i = 0; i < 10; i++) {
if (test[i] != NULL)
delete test[i];
}
delete[] test;
...
test[i] is not an array so you can't use delete[] test[i]
Last edited by blackettle76; 12-13-2006 at 04:53 AM.
-
you repeatedly deleted the same node when you put it to [0] -- deletion does NOT set the value to null, it just gives the memory back to the OS. So.. you tried to delete memory you do not own (anymore) the second time in that loop.
Similar Threads
-
By Tmcclain in forum Java
Replies: 7
Last Post: 02-13-2009, 10:57 PM
-
By PoojaPatel in forum C++
Replies: 22
Last Post: 02-17-2008, 10:34 AM
-
Replies: 3
Last Post: 02-19-2006, 09:37 AM
-
Replies: 6
Last Post: 11-01-2005, 09:05 AM
-
By vbcoder in forum VB Classic
Replies: 9
Last Post: 04-28-2005, 12:36 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks