Does delete free the content of a struct

S

Steve555

Hi,

Here's my struct:

typedef struct{
double matrix[4][4];
}matrixStruct;

When I create and delete this struct... :

matrixStruct *struct = new matrixStruct;
delete struct;

.... is the double array being correctly freed, or do I have a memory
leak?

--------------------------
Also...

stack<double[4][4]> won't compile, which is why I'm using the above
struct: stack< matrixStruct*>" instead. Have I chosen the best
solutiuon to this?

Thanks

Steve
 
G

Gianni Mariani

Steve555 said:
Hi,

Here's my struct:

typedef struct{
double matrix[4][4];
}matrixStruct;

When I create and delete this struct... :

matrixStruct *struct = new matrixStruct;
delete struct;

... is the double array being correctly freed, or do I have a memory
leak?

it will correctly free.

If that struct is never going to be compiled by a C compiler then loose
the typedef. i.e.

struct matrixStruct
{
double matrix[4][4];
};

--------------------------
Also...

stack<double[4][4]> won't compile, which is why I'm using the above
struct: stack< matrixStruct*>" instead. Have I chosen the best
solutiuon to this?

No. Do you really want to manage the delete of all the matrucStruct
objects ?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Steve555 said:
Here's my struct:
typedef struct{
double matrix[4][4];
}matrixStruct;
When I create and delete this struct... :
matrixStruct *struct = new matrixStruct;
delete struct;
... is the double array being correctly freed, or do I have a memory
leak?

it will correctly free.

If that struct is never going to be compiled by a C compiler then loose
the typedef. i.e.

struct matrixStruct
{
double matrix[4][4];

};
stack<double[4][4]> won't compile, which is why I'm using the above
struct: stack< matrixStruct*>" instead. Have I chosen the best
solutiuon to this?

No. Do you really want to manage the delete of all the matrucStruct
objects ?

Meaning that you should probably use either stack<matrixStruct> or
stack<auto_ptr<matrixStruct> >.
 
G

Gianni Mariani

Erik Wikström wrote:
....
Meaning that you should probably use either stack<matrixStruct> or
stack<auto_ptr<matrixStruct> >.

Are you sure that will work ?
 
S

Steve555

Steve555 said:
Hi,
Here's my struct:
typedef struct{
double matrix[4][4];
}matrixStruct;
When I create and delete this struct... :
matrixStruct *struct = new matrixStruct;
delete struct;
... is the double array being correctly freed, or do I have a memory
leak?
it will correctly free.
If that struct is never going to be compiled by a C compiler then loose
the typedef. i.e.
struct matrixStruct
{
double matrix[4][4];
--------------------------
Also...
stack<double[4][4]> won't compile, which is why I'm using the above
struct: stack< matrixStruct*>" instead. Have I chosen the best
solutiuon to this?
No. Do you really want to manage the delete of all the matrucStruct
objects ?

Meaning that you should probably use either stack<matrixStruct> or
stack<auto_ptr<matrixStruct> >.

Thanks guys. If I use stack<matrixStruct>, can I be sure that the [4]
[4]doubles will all be copied across properly when I do a push() and
top() ? (I thought you had to explicitly copy the contents of structs
and classes; do the container classes know to copy all the contents?)
If it does, given I'm in a time-critical drawing loop, is this copying
of 16 doubles likely to be faster than copying just a pointer, plus a
new, plus a delete?

Thanks

Steve
 
G

Gianni Mariani

Steve555 said:
Thanks guys. If I use stack<matrixStruct>, can I be sure that the [4]
[4]doubles will all be copied across properly when I do a push() and
top() ? (I thought you had to explicitly copy the contents of structs
and classes; do the container classes know to copy all the contents?)
If it does, given I'm in a time-critical drawing loop, is this copying
of 16 doubles likely to be faster than copying just a pointer, plus a
new, plus a delete?

In this case, worry about performance when you have a problem.
 
B

Bernd Strieder

Hello,
Thanks guys. If I use stack<matrixStruct>, can I be sure that the [4]
[4]doubles will all be copied across properly when I do a push() and
top() ? (I thought you had to explicitly copy the contents of structs
and classes; do the container classes know to copy all the contents?)
If it does, given I'm in a time-critical drawing loop, is this copying
of 16 doubles likely to be faster than copying just a pointer, plus a
new, plus a delete?

Theoretically the pointer copying plus new and delete could be faster,
but in general you will see the copying of 16 doubles being faster,
since the default allocators have to regard many more problems than
just speed. Try it out.

Bernd Strieder
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Steve555 wrote:
Hi,
Here's my struct:
typedef struct{
double matrix[4][4];
}matrixStruct;
When I create and delete this struct... :
matrixStruct *struct = new matrixStruct;
delete struct;
... is the double array being correctly freed, or do I have a memory
leak?
it will correctly free.
If that struct is never going to be compiled by a C compiler then loose
the typedef. i.e.
struct matrixStruct
{
double matrix[4][4];
};
--------------------------
Also...
stack<double[4][4]> won't compile, which is why I'm using the above
struct: stack< matrixStruct*>" instead. Have I chosen the best
solutiuon to this?
No. Do you really want to manage the delete of all the matrucStruct
objects ?
Meaning that you should probably use either stack<matrixStruct> or
stack<auto_ptr<matrixStruct> >.

Thanks guys. If I use stack<matrixStruct>, can I be sure that the [4]
[4]doubles will all be copied across properly when I do a push() and
top() ? (I thought you had to explicitly copy the contents of structs
and classes; do the container classes know to copy all the contents?)

Yes, all members of the struct will be copied, the problem is when a
member is a pointer since only the pointer will be copied and not what
it points to (which might or might not be what you want). If this is
what you want you can implement a copy-constructor which does the
copying.
 
S

Steve555

Steve555 said:
Thanks guys. If I use stack<matrixStruct>, can I be sure that the [4]
[4]doubles will all be copied across properly when I do a push() and
top() ? (I thought you had to explicitly copy the contents of structs
and classes; do the container classes know to copy all the contents?)
If it does, given I'm in a time-critical drawing loop, is this copying
of 16 doubles likely to be faster than copying just a pointer, plus a
new, plus a delete?

In this case, worry about performance when you have a problem.


These matrices will be push'd & top'd 100's of times per frame at
60fps.
I think that qualifies as a problem ;-)

(But as suggested, I will go away and time all the alternatives)
 
S

Steve555

Steve555 wrote:
Hi,
Here's my struct:
typedef struct{
double matrix[4][4];
}matrixStruct;
When I create and delete this struct... :
matrixStruct *struct = new matrixStruct;
delete struct;
... is the double array being correctly freed, or do I have a memory
leak?
it will correctly free.
If that struct is never going to be compiled by a C compiler then loose
the typedef. i.e.
struct matrixStruct
{
double matrix[4][4];
};
--------------------------
Also...
stack<double[4][4]> won't compile, which is why I'm using the above
struct: stack< matrixStruct*>" instead. Have I chosen the best
solutiuon to this?
No. Do you really want to manage the delete of all the matrucStruct
objects ?
Meaning that you should probably use either stack<matrixStruct> or
stack<auto_ptr<matrixStruct> >.
Thanks guys. If I use stack<matrixStruct>, can I be sure that the [4]
[4]doubles will all be copied across properly when I do a push() and
top() ? (I thought you had to explicitly copy the contents of structs
and classes; do the container classes know to copy all the contents?)

Yes, all members of the struct will be copied, the problem is when a
member is a pointer since only the pointer will be copied and not what
it points to (which might or might not be what you want). If this is
what you want you can implement a copy-constructor which does the
copying.

Ah, yes, thanks, that's exactly what my confusion was.
 
G

Gianni Mariani

Steve555 said:
Steve555 said:
...

Thanks guys. If I use stack<matrixStruct>, can I be sure that the [4]
[4]doubles will all be copied across properly when I do a push() and
top() ? (I thought you had to explicitly copy the contents of structs
and classes; do the container classes know to copy all the contents?)
If it does, given I'm in a time-critical drawing loop, is this copying
of 16 doubles likely to be faster than copying just a pointer, plus a
new, plus a delete?

In this case, worry about performance when you have a problem.



These matrices will be push'd & top'd 100's of times per frame at
60fps.
I think that qualifies as a problem ;-)

(But as suggested, I will go away and time all the alternatives)

On my 2GHz athlon, this code pushes and pops 100 elements off a vector
200,000 times in 0.86 seconds. This would mean that pushing and popping
a 4x4 matrix would take 0.02% of the execution time and I didn't
optimize anything yet. On an athlon 64 2.2ghz (4200) in 64 bit code, it
runs in 0.24 seconds.

As I said, "In this case", worry about performance when you have a problem.

#include <vector>

struct matrixStruct
{
double matrix[4][4];
};

std::vector< matrixStruct > vec;

void foo()
{
matrixStruct x = matrixStruct();

for ( int i = 0 ; i < 100; ++i )
{
vec.push_back( x );
}

for ( int i = 0 ; i < 100; ++i )
{
vec.pop_back();
}

}


int main()
{

for (int i =0; i < 200000; ++i )
{
foo();
}
}
 
R

Rafik_2

Try oneadther method. (Excuse me for my englsish)
First creat constructore and destructor for your structure , then
overload operators new and delete.
for example

struct Matrix{
double x[4][4];
Matrix();
~Matrix();
friend void operator new(Matrix &obj);
friend void operator delete(Matrix &obj);
}

and I think no problem , if you writing this code.
 
S

Steve555

Try oneadther method. (Excuse me for my englsish)
First creat constructore and destructor for your structure , then
overload operators new and delete.
for example

struct Matrix{
double x[4][4];
Matrix();
~Matrix();
friend void operator new(Matrix &obj);
friend void operator delete(Matrix &obj);

}

and I think no problem , if you writing this code.

Oh sorry Rafi, I can't figure that one out. (How to complete it, or
what the benefit is)
 
H

Howard

Try oneadther method. (Excuse me for my englsish)
First creat constructore and destructor for your structure , then
overload operators new and delete.
for example

Why? What will you gain by overloading new and delete?

-Howard
 
O

Old Wolf

typedef struct{
double matrix[4][4];
}matrixStruct;

Thanks guys. If I use stack<matrixStruct>, can I be sure that the [4]
[4]doubles will all be copied across properly when I do a push() and
top() ? (I thought you had to explicitly copy the contents of structs
and classes; do the container classes know to copy all the contents?)

When you declare a struct, the compiler generates a copy
constructor and assignment operator that creates a perfect
copy of the original. Nothing to do with the container class.

Regardless of what T is, you can write stuff like:

T a;
T b = a;
a = b;

unless of course you have tried to write your own functions to do
that, and got it wrong (or disabled this behaviour on purpose).
If it does, given I'm in a time-critical drawing loop, is this copying
of 16 doubles likely to be faster than copying just a pointer, plus a
new, plus a delete?

To operate your stack without memory allocation you could
pre-allocate all the structs, eg.

std::vector<matrixStruct> all_matrices(100000);

for (size_t i = 0; i != all_matrices.size(); ++i)
the_stack.push_back( &all_matrices );

Then it is only the size of a pointer that is being
moved around each time.
 
S

Steve555

Steve555 said:
Steve555 wrote:
...
Thanks guys. If I use stack<matrixStruct>, can I be sure that the [4]
[4]doubles will all be copied across properly when I do a push() and
top() ? (I thought you had to explicitly copy the contents of structs
and classes; do the container classes know to copy all the contents?)
If it does, given I'm in a time-critical drawing loop, is this copying
of 16 doubles likely to be faster than copying just a pointer, plus a
new, plus a delete?
In this case, worry about performance when you have a problem.
These matrices will be push'd & top'd 100's of times per frame at
60fps.
I think that qualifies as a problem ;-)
(But as suggested, I will go away and time all the alternatives)

On my 2GHz athlon, this code pushes and pops 100 elements off a vector
200,000 times in 0.86 seconds. This would mean that pushing and popping
a 4x4 matrix would take 0.02% of the execution time and I didn't
optimize anything yet. On an athlon 64 2.2ghz (4200) in 64 bit code, it
runs in 0.24 seconds.

As I said, "In this case", worry about performance when you have a problem.

#include <vector>

struct matrixStruct
{
double matrix[4][4];

};

std::vector< matrixStruct > vec;

void foo()
{
matrixStruct x = matrixStruct();

for ( int i = 0 ; i < 100; ++i )
{
vec.push_back( x );
}

for ( int i = 0 ; i < 100; ++i )
{
vec.pop_back();
}

}

int main()
{

for (int i =0; i < 200000; ++i )
{
foo();
}

}

A valid point, well made.
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top