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

in-place new allocations

Hello all

I'm having a problem with an Array template class I've written. Please
don't' suggest using std::vector, this is what I'm trying to get away
from (to resource hungry & to many layers + many other reasons). For the
most part My Array class works perfectly until I need an array of
pointers, eg:

Array<SomeObject> myarr; // works just fine
Array<SomeObject *> myarr; // returns an error, see below

<internal>:100: error: too many arguments to function `void* operator
new(unsigned int)'

As far as I can tell is trying to call the new[]() operator instead of
the new() operator for in-place allocation. Here's a snip of the class
in question.

template <typename Ty>
class Array {
public:
...

void push(Ty &obj) {
if (obj_cnt >= buf_cnt) alloc_request(obj_cnt + 1);
construct(obj_cnt++, obj);
}

...

protected:
...
inline void construct(size_t idx, Ty &val) {
new(&(buffer[idx])) Ty(val); // Here's where the problem lies :P
}
inline void destroy(size_t idx) { (&(buffer[idx]))->Ty::~Ty(); }

private:
size_t idx;

size_t hint;
size_t obj_cnt;
size_t buf_cnt;
Ty *buffer;
}

This is compiled with g++ 3.3.5. Any help would be very greatly
appreciated.

Thanks in advance ;)
Jul 23 '05 #1
4 8601
* Ron Wills:

I'm having a problem with an Array template class I've written. Please
don't' suggest using std::vector, this is what I'm trying to get away
from (to resource hungry & to many layers + many other reasons). For the
most part My Array class works perfectly until I need an array of
pointers, eg:

Array<SomeObject> myarr; // works just fine
Array<SomeObject *> myarr; // returns an error, see below

<internal>:100: error: too many arguments to function `void* operator
new(unsigned int)'
Did you remember to #include <new>?

As far as I can tell is trying to call the new[]() operator instead of
the new() operator for in-place allocation. Here's a snip of the class
in question.

template <typename Ty>
class Array {
public:
...

void push(Ty &obj) {
if (obj_cnt >= buf_cnt) alloc_request(obj_cnt + 1);
construct(obj_cnt++, obj);
}
Shouldn't the argument be const?

Also, isn't (obj_cnt <= buf_cnt) a class invariant, and if it is,
why check for >, and if it isn't, why isn't it?

And shouldn't you increment obj_cnt as _part_ of reallocating so that
it's in synch -- the postfix ++ in there is IMO Really Bad Practice?

Anyway, reallocating while increasing the size by just 1 is extremely
inefficient, unless you have a very smart allocator.

You'd be much better off using std::vector (not to mention Boost arrays)
if you do it that way, and since presumably you didn't know that, you
will with a high probability be much better off -- except in experience &
understanding gained -- by not reinventing the wheel, not matter whether
you fix the reallocation scheme.

...

protected:
...
inline void construct(size_t idx, Ty &val) {
new(&(buffer[idx])) Ty(val); // Here's where the problem lies :P
}
The keyword 'inline' is superflous here; the member function is already
'inline' by virtue of being defined in the class definition.

AFAICS there's nothing technically wrong with the code you've _shown_,
except that it could be improved wrt. safety by writing "::new" instead of
"new"; the following compiles nicely using MSVC 7.1:

#include <new>

template< typename T >
void foo()
{
T buffer[10];

T v = T();
::new( &buffer[0] ) T(v);
}

int main()
{
foo<int*>();
}
inline void destroy(size_t idx) { (&(buffer[idx]))->Ty::~Ty(); }
Shouldn't a few other member variables be updated here?

private:
size_t idx;

size_t hint;


General advice: hinting is a very unreliable form of communication.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Alf P. Steinbach wrote:
* Ron Wills:
I'm having a problem with an Array template class I've written. Please
don't' suggest using std::vector, this is what I'm trying to get away
from (to resource hungry & to many layers + many other reasons). For the
most part My Array class works perfectly until I need an array of
pointers, eg:

Array<SomeObject> myarr; // works just fine
Array<SomeObject *> myarr; // returns an error, see below

<internal>:100: error: too many arguments to function `void* operator
new(unsigned int)'

Did you remember to #include <new>?
As far as I can tell is trying to call the new[]() operator instead of
the new() operator for in-place allocation. Here's a snip of the class
in question.

template <typename Ty>
class Array {
public:
...

void push(Ty &obj) {
if (obj_cnt >= buf_cnt) alloc_request(obj_cnt + 1);
construct(obj_cnt++, obj);
}

Shouldn't the argument be const?

Also, isn't (obj_cnt <= buf_cnt) a class invariant, and if it is,
why check for >, and if it isn't, why isn't it?

And shouldn't you increment obj_cnt as _part_ of reallocating so that
it's in synch -- the postfix ++ in there is IMO Really Bad Practice?

Anyway, reallocating while increasing the size by just 1 is extremely
inefficient, unless you have a very smart allocator.


There is a smart allocator that is instructed to allocate by a set
amount. (obj_cnt is the number of actual object and buf_cnt is the size
of the allocated buffer).
You'd be much better off using std::vector (not to mention Boost arrays)
if you do it that way, and since presumably you didn't know that, you
will with a high probability be much better off -- except in experience &
understanding gained -- by not reinventing the wheel, not matter whether
you fix the reallocation scheme.

I understand this code is quite ugly at the moment. Initially
std::vectors were used. The software project is a type of highly dynamic
OO scripting engine (both embeddable and extendable) and requires a lot
of std::vector and std::map objects. Initially (using vector & map) the
most basic embedded application is showing 10M virt memory used, 1M res
memory used and 0 bytes swapped which is basically showing me that only
1M of memory is actually live and there is another 9M memory allocated
but never used :P.
Further testing shows that vectors only deallocate memory when they
are destroyed and in certain situations allocate double the amount of
fields than is required. The map class is even harder to test how much
it is allocating. So currently I'm writing some thin objects to see if I
can get the resource management under control. If I can't then I have to
drop C++ and rewrite in C, which understandably I don't want to do.
...

protected:
...
inline void construct(size_t idx, Ty &val) {
new(&(buffer[idx])) Ty(val); // Here's where the problem lies :P
}

The keyword 'inline' is superflous here; the member function is already
'inline' by virtue of being defined in the class definition.

AFAICS there's nothing technically wrong with the code you've _shown_,
except that it could be improved wrt. safety by writing "::new" instead of
"new"; the following compiles nicely using MSVC 7.1:


Slick, ::new fixed the problem.
#include <new>

template< typename T >
void foo()
{
T buffer[10];

T v = T();
::new( &buffer[0] ) T(v);
}

int main()
{
foo<int*>();
}

inline void destroy(size_t idx) { (&(buffer[idx]))->Ty::~Ty(); }

Shouldn't a few other member variables be updated here?

private:
size_t idx;

size_t hint;

General advice: hinting is a very unreliable form of communication.


hint is used to control the allocator, probably a bad name for this, but
if this class (plus a couple more built from it) solves the resource
issues, this class will get a good clean up ;). Currently enough for
testing purposes.

Thanks Alf for your help, I can move forward again ;)
Jul 23 '05 #3
* Ron Wills:
Further testing shows that vectors only deallocate memory when they
are destroyed


Yes, that's how they behave. To clear a vector's memory usage, "swap" it
with a temporary empty vector using std::vector::swap. When that temporary
is destroyed it deallocates the buffer.

Off the cuff:

template< typename T >
void reallyAbsolutelyClear( std::vector<T>& v, std::size_t newSize = 0 )
{
std::vector<T> temp(newSize);
v.swap( temp );
}

Now you can _forget_ that insight about global placement new... ;-)
PS: If std::vector still turns out to be too inefficient, consider
checking out the Boost array/matrix classes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
Alf P. Steinbach wrote:
* Ron Wills:
Further testing shows that vectors only deallocate memory when they
are destroyed

Yes, that's how they behave. To clear a vector's memory usage, "swap" it
with a temporary empty vector using std::vector::swap. When that temporary
is destroyed it deallocates the buffer.

Off the cuff:

template< typename T >
void reallyAbsolutelyClear( std::vector<T>& v, std::size_t newSize = 0 )
{
std::vector<T> temp(newSize);
v.swap( temp );
}

Now you can _forget_ that insight about global placement new... ;-)
PS: If std::vector still turns out to be too inefficient, consider
checking out the Boost array/matrix classes.


I just might try Boost out. On the other hand my requirement for these
types of objects are minimal. I need just enough for the dynamic
framework (mapping methods and attributes, listing parents, globals,
locals, and so on). So hopefully with these tiny objects I'll get a
better foot print and it just might increase its performance (which it
looks like so far). But only more testing will tell ;)

Jul 23 '05 #5

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

Similar topics

3
by: Curious Expatriate | last post by:
Hi- I'm completely stumped. I'm trying to write some code that will parse a file and rewrite it with all URLs replaced by something else. For example: if the file looks like this: <b>click...
1
by: JS Bangs | last post by:
I started using PHP's object-oriented stuff a little while ago, which has mostly been a joy. However, I've noticed that they don't seem to echo as I would like. Eg: $this->field = 255;...
5
by: lawrence | last post by:
I've waited 6 weeks for an answer to my other question and still no luck, so let me rephrase the question. I know I can do this: <form method="post" action="$self"> <input type="text"...
0
by: Ben Eisenberg | last post by:
I'm trying to run a php script setuid. I've tried POSIX_setuid but you have to be root to run this. The files are located on a public access unix system and have me as the owner and nobody as the...
2
by: Felix | last post by:
Hi, I've a problem: I want to have the result of my Mysql Query in a Table in my php file. Now I've this: <?
1
by: James | last post by:
What is the best way to update a record in a MYSQL DB using a FORM and PHP ? Where ID = $ID ! Any examples or URLS ? Thanks
1
by: Patrick Schlaepfer | last post by:
Why this code is not working on Solaris 2.8 host. Always getting: PHP Fatal error: swfaction() : getURL('http://www.php.net' ^ Line 1: Reason: 'syntax error' in /.../htdocs/ming2.php on...
1
by: phpkid | last post by:
Howdy I've been given conflicting answers about search engines picking up urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3 Do search engines pick up these urls? I've been considering...
1
by: lawrence | last post by:
What is the PHP equivalent of messaging, as in Java?
3
by: Quinten Carlson | last post by:
Is there a way to conditionally define a function in php? I'm trying to run a php page 10 times using the include statement, but I get an error because my function is already defined. The docs...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.