473,385 Members | 2,013 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,385 software developers and data experts.

Memory Management

Hi!

Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].
Besides, would it be faster to use malloc() and free() instead of new
[] and delete [] ? I can make sure that the types used for those
operations will be simple types (most of the time an unsigned char)
only so no constructors / destructors stuff etc.

Thanks + Regards
Alex

Jul 13 '07 #1
11 2546
Alexander Adam wrote:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it?
Yes. Consider it shortened. Next problem!...

No, really. If you just need it shortened, simply ignore the
elements that are beyond your "new end" of the array.
I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].
Besides, would it be faster to use malloc() and free() instead of new
[] and delete [] ?
Usually, no. 'new' and 'delete' ensure that objects that need to be
constructed (and destroyed) are actually constructed (and destroyed)
properly.
I can make sure that the types used for those
operations will be simple types (most of the time an unsigned char)
only so no constructors / destructors stuff etc.
For those types it shouldn't matter, but if you're concerned, test it
and see.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 13 '07 #2
Alexander Adam <co*****@emiasys.comwrote:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].
If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing the
memory yourself. std::vector is compatible with functions that expect
native arrays, if that is one of your concerns.
Besides, would it be faster to use malloc() and free() instead of new
[] and delete [] ? I can make sure that the types used for those
operations will be simple types (most of the time an unsigned char)
only so no constructors / destructors stuff etc.
Also, don't just automatically assume that the performance will be less
when using vector instead of an array (or malloc()/free() versus
new[]/delete[]). The only way to see is to profile, and you may find
that there is no performance impact. Besides, many implementations of
new/delete internally use malloc/free anyway. Also, std::vector will
handle constructors/destructors appropriately.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '07 #3
Marcus Kwok wrote:
Alexander Adam <co*****@emiasys.comwrote:
>Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].

If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing
the memory yourself. std::vector is compatible with functions that
expect native arrays, if that is one of your concerns.
>Besides, would it be faster to use malloc() and free() instead of new
[] and delete [] ? I can make sure that the types used for those
operations will be simple types (most of the time an unsigned char)
only so no constructors / destructors stuff etc.

Also, don't just automatically assume that the performance will be
less when using vector instead of an array (or malloc()/free() versus
new[]/delete[]). The only way to see is to profile, and you may find
that there is no performance impact. Besides, many implementations of
new/delete internally use malloc/free anyway. Also, std::vector will
handle constructors/destructors appropriately.
It is probably worth mentioning that standard containers do put some
requirements on the types of contained objects (Assignable and Copy-
Constructible) which may actually force somebody to use arrays instead
of 'std::vector'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 13 '07 #4
On Jul 13, 10:22 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Alexander Adam <cont...@emiasys.comwrote:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].

If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing the
memory yourself.
If actually reclaiming the memory is of cocern to the OP, then even
vector::resize() is not sufficient. I guess it depends on his intent.

- If all you want is to easily reclaim the additional memory without
having to dirty up your code with multiple function calls to achieve
this, use vector. It handles all the dirty work for you. Just be
aware that resize() won't reclaim the memory. One common way to
reclaim the memory immediately though is to do this

vector<charvec1;
//fill vec1 with lots of data.
vec1.swap(std::vector<char>());

- If what you want is the speed of not having to delete/reallocate
memory, you can do what victor suggested above and keep track of some
variable that specifies your "true" size. (i.e. the size that you are
asserting the array is). Even if your array is allocated to be 700
items, as long as your variable says 5, you know that it's really only
holding 5 items.

- If you want both of the above at the same time, then your
requirements are that a) at any given time, there must never be more
memory allocated than necessary, and b) it must happen in such a way
that you does not involve the two step process of deleting the entire
array and reallocating it with a different size. There is no way to
achieve this.

Jul 13 '07 #5
Victor Bazarov <v.********@comacast.netwrote:
Marcus Kwok wrote:
>Alexander Adam <co*****@emiasys.comwrote:
>>Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].

If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing
the memory yourself. std::vector is compatible with functions that
expect native arrays, if that is one of your concerns.

It is probably worth mentioning that standard containers do put some
requirements on the types of contained objects (Assignable and Copy-
Constructible) which may actually force somebody to use arrays instead
of 'std::vector'.
Good point. I probably should have mentioned them explicitly, but I
guess it could be covered by the "If it satisfies your requirements..."
:)

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '07 #6
Zachary Turner <di***********@gmail.comwrote:
On Jul 13, 10:22 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
>Alexander Adam <cont...@emiasys.comwrote:
Is there an efficient way to "shorten" an array with C++ Operators
without the need to delete it, then reallocate it? I've thought about
using reallocate which might do the job but its C and besides that I
am using C++ operators new [] and delete [].

If it satisfies your requirements, you could try using std::vector
instead of your dynamic arrays. This way, you can use the
vector::resize() method, plus you don't have to worry about managing the
memory yourself.

If actually reclaiming the memory is of cocern to the OP, then even
vector::resize() is not sufficient. I guess it depends on his intent.

- If all you want is to easily reclaim the additional memory without
having to dirty up your code with multiple function calls to achieve
this, use vector. It handles all the dirty work for you. Just be
aware that resize() won't reclaim the memory. One common way to
reclaim the memory immediately though is to do this

vector<charvec1;
//fill vec1 with lots of data.
vec1.swap(std::vector<char>());
Almost; you got it switched. It needs to be:

std::vector<char>().swap(vec1);

vector::swap() takes a non-const reference as its parameter, and so it
cannot bind to a temporary. However, you may call non-const methods on
a temporary object.

[snip rest]

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '07 #7

Zachary Turner <di***********@gmail.comwrote in message...
>
vector<charvec1;
file://fill vec1 with lots of data.
vec1.swap(std::vector<char>());
Can also be written:

std::vector<char>().swap( vec1 );

Use which ever is your style.

--
Bob R
POVrookie
Jul 13 '07 #8
BobR <re***********@worldnet.att.netwrote:
Zachary Turner <di***********@gmail.comwrote in message...
>vec1.swap(std::vector<char>());

Can also be written:

std::vector<char>().swap( vec1 );

Use which ever is your style.
Except the first one doesn't work... see my other post about it.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '07 #9

Marcus Kwok wrote in message...
BobR wrote:
Zachary Turner wrote in message...
vec1.swap(std::vector<char>());
Can also be written:

std::vector<char>().swap( vec1 );

Use which ever is your style.

Except the first one doesn't work... see my other post about it.
Doh, that's what I get for not testing his code myself (I assumed he had!).

Thanks.

[ Next time, tell me you are going to post the answer so I don't duplicate
the effort! <G( ....what?!? Your crystal ball is not working?) ]
--
Bob R
POVrookie
Jul 13 '07 #10
On Jul 13, 2:19 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
Marcus Kwok wrote in message...
Except the first one doesn't work... see my other post about it.

Doh, that's what I get for not testing his code myself (I assumed he had!).
:( That's what I get for posting about something I haven't actually
used in a while.

Jul 13 '07 #11
On Jul 13, 5:48 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

[...]
It is probably worth mentioning that standard containers do put some
requirements on the types of contained objects (Assignable and Copy-
Constructible) which may actually force somebody to use arrays instead
of 'std::vector'.
And that new[] puts other constraints on contained objects, such
as having a default constructor. (In practice, I can't conceive
of any use of new[] that didn't also require assignment. It's
the only way of changing the default constructed values, and in
general, arrays in which all elements are required to have
eternally the same value aren't very useful.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 14 '07 #12

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

Similar topics

0
by: Richard Jones | last post by:
Garbage Collection & Memory Management Summer School 20-21 July 2004, Canterbury, UK The performance of today's memory-hungry applications depends on efficient dynamic memory management,...
2
by: DANIEL BEAULIEU J | last post by:
Basically i am a student taking an operating systems course which is c++ intensive. Familiar with Java, and so not so familiar with memory management. Looking for suggestions of exercises or web...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
8
by: Chad | last post by:
hello, i am losing memory each time i make this call to a C++ dll (I make frequent calls). 'in VB.Net Declare Function grab Lib "grabber.dll" _ (ByRef lpBuf As Byte, ByVal lnum As Integer)...
1
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots...
0
by: erez_acount | last post by:
***************************************************************************** Call For Papers The 2006 International Symposium on Memory Management (ISMM'06) Co-located with PLDI 2006 ...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
9
by: benoit808 | last post by:
I don't have a lot of experience with C++ so I apologize if this is a stupid question. I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory leak but I don't think there's one. Here...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.