473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

malloc and new...

Hi,

i use a dynamic 3D matrix of structure that yields to a 90Mb of RAM
allocated.
To do that i use new operator. here a portion of code

struct SInfo
{
int i1;
int i2;
float f1;
float f2;
}

SInfo ***pCoord = new SInfo **[iELM_MAT_R];

for(j=0; j<iELM_MAT_R; j++)
{
pCoord[j] = new SInfo *[iELM_MAT_C];
for(k=0; k<iELM_MAT_C; k++)
{
pCoord[j][k] = new SInfo[MAX_ELM];
}
}
and it works...but i read that it is better to use malloc and free
instead of new and delete to avoid memory leaks.

i don't see error in my code but i'd like to know if there is something
i'm skipping...

thanks

Davide
Nov 3 '06 #1
6 1581
VJ
dade wrote:
Hi,

i use a dynamic 3D matrix of structure that yields to a 90Mb of RAM
allocated.
To do that i use new operator. here a portion of code

struct SInfo
{
int i1;
int i2;
float f1;
float f2;
}

SInfo ***pCoord = new SInfo **[iELM_MAT_R];

for(j=0; j<iELM_MAT_R; j++)
{
pCoord[j] = new SInfo *[iELM_MAT_C];
for(k=0; k<iELM_MAT_C; k++)
{
pCoord[j][k] = new SInfo[MAX_ELM];
}
}
and it works...but i read that it is better to use malloc and free
instead of new and delete to avoid memory leaks.

i don't see error in my code but i'd like to know if there is something
i'm skipping...

thanks

Davide
It is bad to allocate many chunks of memory - better allocate one huge
chunk, and access it differently

SInfo *pCoord = new SInfo[ iELM_MAT_R * iELM_MAT_C * MAX_ELM );
or
std::auto_ptr< SInfo pCoord;

you should probably make a class to use coordinates to calculate index
in this big structure

....or you can forget about this, and use vectors ;)
Nov 3 '06 #2
dade wrote:
Hi,

i use a dynamic 3D matrix of structure that yields to a 90Mb of RAM
allocated.
To do that i use new operator. here a portion of code

struct SInfo
{
int i1;
int i2;
float f1;
float f2;
}

SInfo ***pCoord = new SInfo **[iELM_MAT_R];

for(j=0; j<iELM_MAT_R; j++)
{
pCoord[j] = new SInfo *[iELM_MAT_C];
for(k=0; k<iELM_MAT_C; k++)
{
pCoord[j][k] = new SInfo[MAX_ELM];
}
}
and it works...but i read that it is better to use malloc and free
instead of new and delete to avoid memory leaks.
Huh? Where did you read that? The problems of memory management using
malloc/free are essentially the same as with using new/delete.
i don't see error in my code but i'd like to know if there is something
i'm skipping...
Well, your code leaks in the event that an allocation after the first is
unsuccessfull and throws a bad_alloc. You should consider using

vector< vector< vector< SInfo >

instead. That will take care of memory management for you. Be sure to use
reserve() for efficiency.
Best

Kai-Uwe Bux
Nov 3 '06 #3
In article <38********************@twister1.libero.it>,
dade <ce****@libero.itwrote:
Hi,

i use a dynamic 3D matrix of structure that yields to a 90Mb of RAM
allocated.
To do that i use new operator. here a portion of code

struct SInfo
{
int i1;
int i2;
float f1;
float f2;
}

SInfo ***pCoord = new SInfo **[iELM_MAT_R];

for(j=0; j<iELM_MAT_R; j++)
{
pCoord[j] = new SInfo *[iELM_MAT_C];
for(k=0; k<iELM_MAT_C; k++)
{
pCoord[j][k] = new SInfo[MAX_ELM];
}
}
Better would be a class:

template < typename T >
class Array3D
{
public:
Array3D( unsigned x, unsigned y, unsigned z ):
l( x ), h( y ), w( z ), rep( x * y * z ) { }

T& at( unsigned x, unsigned y, unsigned z ) {
assert( x < l && y < h && z < w );
return rep[ x * l * h + y * h + z ];
}

const T& at( unsigned x, unsigned y, unsigned z ) const {
assert( x < l && y < h && z < w );
return rep[ x * l * h + y * h + z ];
}

private:
unsigned l, unsigned h, unsigned w
std::deque< T rep;
};

(Someone double check me on the above, I didn't test it.)
and it works...but i read that it is better to use malloc and free
instead of new and delete to avoid memory leaks.
That's silly.
i don't see error in my code but i'd like to know if there is something
i'm skipping...
You should use const unsigned for your array bounds rather than defines,
you need to remember to delete all the memory you newed, and the code
you have above needs to be made exception safe.

Use a class like the one I have above instead. It is a complete solution
in fewer lines of code, easer to maintain and more likely to be correct.

--
To send me email, put "sheltie" in the subject.
Nov 3 '06 #4
Daniel T. wrote:
In article <38********************@twister1.libero.it>,
dade <ce****@libero.itwrote:
>Hi,

i use a dynamic 3D matrix of structure that yields to a 90Mb of RAM
allocated.
To do that i use new operator. here a portion of code

struct SInfo
{
int i1;
int i2;
float f1;
float f2;
}

SInfo ***pCoord = new SInfo **[iELM_MAT_R];

for(j=0; j<iELM_MAT_R; j++)
{
pCoord[j] = new SInfo *[iELM_MAT_C];
for(k=0; k<iELM_MAT_C; k++)
{
pCoord[j][k] = new SInfo[MAX_ELM];
}
}

Better would be a class:

template < typename T >
class Array3D
{
public:
Array3D( unsigned x, unsigned y, unsigned z ):
l( x ), h( y ), w( z ), rep( x * y * z ) { }
what about

typedef std::size_t size_type;

and then using size_type instead of unsigned throughout?
T& at( unsigned x, unsigned y, unsigned z ) {
assert( x < l && y < h && z < w );
return rep[ x * l * h + y * h + z ];
I get x*h*w + y*w + z

}

const T& at( unsigned x, unsigned y, unsigned z ) const {
assert( x < l && y < h && z < w );
return rep[ x * l * h + y * h + z ];
}

private:
unsigned l, unsigned h, unsigned w
std::deque< T rep;
Is there a reason to use deque? You are not planning on changing the size
anyway, are you? This is a case, where std::vector with a call to reserve
might be best (or even, gasp, a T* with a new in the constructor and a
delete [] in the destructor.
};

(Someone double check me on the above, I didn't test it.)
>and it works...but i read that it is better to use malloc and free
instead of new and delete to avoid memory leaks.

That's silly.
>i don't see error in my code but i'd like to know if there is something
i'm skipping...

You should use const unsigned for your array bounds rather than defines,
you need to remember to delete all the memory you newed, and the code
you have above needs to be made exception safe.

Use a class like the one I have above instead. It is a complete solution
in fewer lines of code, easer to maintain and more likely to be correct.

Best

Kai-Uwe Bux
Nov 3 '06 #5
dade:
i use a dynamic 3D matrix of structure that yields to a 90Mb of RAM
allocated.
Your code does not exhibit a 3-dimensional array. The following is a 3-
dimensional array:

int arr[A][b][C];

, and is sits in memory exactly as if it were an int[A*B*C];

What you have is something like the following:

int i;

int *p;

int **pp;

p = &i;

pp = &p;

You have an array of pointers, and each pointer points to an array.

--

Frederick Gotham
Nov 3 '06 #6
Kai-Uwe Bux <jk********@gmx.netwrote:
Daniel T. wrote:
>>
Better would be a class:

template < typename T >
class Array3D
{
public:
Array3D( unsigned x, unsigned y, unsigned z ):
l( x ), h( y ), w( z ), rep( x * y * z ) { }

what about

typedef std::size_t size_type;

and then using size_type instead of unsigned throughout?
Works for me. I felt doing so would confuse the OP even more.
> T& at( unsigned x, unsigned y, unsigned z ) {
assert( x < l && y < h && z < w );
return rep[ x * l * h + y * h + z ];

I get x*h*w + y*w + z
Yea, that's what I was thinking I got wrong. Thanks.
> }

const T& at( unsigned x, unsigned y, unsigned z ) const {
assert( x < l && y < h && z < w );
return rep[ x * l * h + y * h + z ];
}

private:
unsigned l, unsigned h, unsigned w
std::deque< T rep;

Is there a reason to use deque?
Less likely to fail if the memory is fragmented. The OP is planaing on
making a 90MB block after all.
You are not planning on changing the size anyway, are you?
The OP may be planing on changing the size, I wouldn't know.
This is a case, where std::vector with a call to reserve
might be best (or even, gasp, a T* with a new in the constructor and a
delete [] in the destructor.
As written a call to reserve is meaningless since the container would be
created with all elements.

The nice thing about this class for the OP is that he has the option of
replacing the container type without the rest of the code caring. For
example, it may be the case that he doesn't really need to allocate the
whole 90MB at construction, this class allows for a lazy initialization
of cells.

--
To send me email, put "sheltie" in the subject.
Nov 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.