473,224 Members | 1,441 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,224 software developers and data experts.

Variably sized instances of a class

I want to create a class with a variably-sized array inlined.
The array size will be known at each instantiation but not
at compile time. Once created, arrays won't grow or shrink
and their contents won't change.

The array length will be accessible via a smart pointer
located before the array. On destruction, it must be fetched
before the smartpointer containing it is destroyed.

The arrays will typically hold smart pointers, so proper
destruction is a must.

Is this feasible with C++?

Unfortunately, overloading new() won't do the trick unless
I resort to nasties such as passing the required extra byte
count in a TLS slot.
Jul 20 '06 #1
7 1159
Thats not possible. The compiler must know at compile-time the exact size of
the every class - having variable sized inline arrays violates this.
---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : Drop-In Explorer GUI Controls For Your Apps (.Net & ActiveX
Editions Available)
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions rapidly in .Net
---------

"Ole Nielsby" <ol*********@snailmail.dkwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I want to create a class with a variably-sized array inlined.
The array size will be known at each instantiation but not
at compile time. Once created, arrays won't grow or shrink
and their contents won't change.

The array length will be accessible via a smart pointer
located before the array. On destruction, it must be fetched
before the smartpointer containing it is destroyed.

The arrays will typically hold smart pointers, so proper
destruction is a must.

Is this feasible with C++?

Unfortunately, overloading new() won't do the trick unless
I resort to nasties such as passing the required extra byte
count in a TLS slot.


Jul 21 '06 #2
"G Himangi" <in**@ssware.comwrote in message
news:uI**************@TK2MSFTNGP03.phx.gbl...
Thats not possible. The compiler must know at compile-time the exact size
of the every class - having variable sized inline arrays violates this.
Except if you use a template class for this, and all array lengths are known
at compiletime.

something like

template <int Qclass myClass
{
private:
char data[Q];
public:
myClass()
{
}
//other stuff
};

myClass<100anInstance();

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Jul 21 '06 #3
G Himangi <in**@ssware.comwrote:
Ole Nielsby <ol*********@snailmail.dkwrote:
>I want to create a class with a variably-sized array inlined.
The array size will be known at each instantiation but not
at compile time. [...]

Is this feasible with C++?

Thats not possible. The compiler must know at compile-time
the exact size of the every class - having variable sized inline
arrays violates this.
"Not possible" is not a phrase in my vocabulary. I'm sure it is
possible, though it may violate some fundamentals of what is
considered sound C++ programming style.

I need it because my program will create millions of these
objects and the overhead of allocating separate array objects
is not acceptable.

So the question is really not "can I do it or not", but rather:
What's the best I can do to perform this hack and wrap it up
nicely?

If I can't create the objects with new , I can still use
malloc() or a homebrew memory manager to get memory
blocks of the needed size. Given a *void pointer, I can cast
it to *MyClass and initialize it. But how do I best perform
this initialization? By copy constructor? Assignment overload?
memcopying a stack allocated instance?

The class has virtual methods, so the vtable must be filled in.

I might need to forsake C++ destructors and define my own
destroy method and use a custom smart pointer template,
but this should still be possible, right???

Is it, by any chance, possible to call constructor methods
explicitly on an uninitialized memory block, and will this
initialize the vtable pointer? Or is there a way to explicitly
initialize it?
Jul 21 '06 #4

Ole Nielsby wrote:
G Himangi <in**@ssware.comwrote:
Ole Nielsby <ol*********@snailmail.dkwrote:
I want to create a class with a variably-sized array inlined.
The array size will be known at each instantiation but not
at compile time. [...]

Is this feasible with C++?
Thats not possible. The compiler must know at compile-time
the exact size of the every class - having variable sized inline
arrays violates this.

"Not possible" is not a phrase in my vocabulary. I'm sure it is
possible, though it may violate some fundamentals of what is
considered sound C++ programming style.
Most important, it violates the C++ standard, which means that whatever
solution you could devise, it may break on the next compiler release or
service pack, and would most probalby be not portable.

I need it because my program will create millions of these
objects and the overhead of allocating separate array objects
is not acceptable.
If you really create millions of object, I beg each array would be
quite small. Can't you fix an upper-bound to the array size and have
all instances use this max size?
>
So the question is really not "can I do it or not", but rather:
What's the best I can do to perform this hack and wrap it up
nicely?

If I can't create the objects with new , I can still use
malloc() or a homebrew memory manager to get memory
blocks of the needed size. Given a *void pointer, I can cast
it to *MyClass and initialize it. But how do I best perform
this initialization? By copy constructor? Assignment overload?
memcopying a stack allocated instance?
placement-new.
The class has virtual methods, so the vtable must be filled in.

I might need to forsake C++ destructors and define my own
destroy method and use a custom smart pointer template,
but this should still be possible, right???
Maybe, but I definitely will not use this kind of hack in production
code...
>
Is it, by any chance, possible to call constructor methods
explicitly on an uninitialized memory block, and will this
initialize the vtable pointer? Or is there a way to explicitly
initialize it?
placement-new. However, it means you cannot instantiate the object on
the stack....

Arnaud
MVP - VC

Jul 21 '06 #5
>I want to create a class with a variably-sized array inlined.
The array size will be known at each instantiation but not
at compile time. Once created, arrays won't grow or shrink
and their contents won't change.

The array length will be accessible via a smart pointer
located before the array. On destruction, it must be fetched
before the smartpointer containing it is destroyed.

The arrays will typically hold smart pointers, so proper
destruction is a must.

Is this feasible with C++?

Unfortunately, overloading new() won't do the trick unless
I resort to nasties such as passing the required extra byte
count in a TLS slot.
You can overload "operator new". If you need define additional arguments in
that "operator new" (eg. new(param1, param2) A(x,y,z) ). Or you can create
"factory" function that allocates required space and calls placement "new"
to initialize instance. You have to compensate you allocation logic with
required deallocation logic.
--
Vladimir Nesterovsky
Jul 21 '06 #6

<ad******@club-internet.frwrote:
>
Most important, it violates the C++ standard, which means
that whatever solution you could devise, it may break on
the next compiler release or service pack, and would most
probalby be not portable.
"not portable" would mean, some of the code might have to
be rewritten for ports. If I wrap the dirty tricks in bag of
templates or whatever, I should be able to minimize the
rewriting to just that.
Can't you fix an upper-bound to the array size [...]
No. Most of them will be small - a few will be huge, and
I want non-virtual (inline) methods to work on both small
and large.
>Is it, by any chance, possible to call constructor methods
explicitly on an uninitialized memory block, and will this
initialize the vtable pointer? Or is there a way to explicitly
initialize it?

placement-new.
Thanks - this seems to be what I need.

Now, if I do a naive delete on a *MyClass created this way, am
I right in assuming that this will call the destructor of MyClass,
and after that, deallocate the memory?

If this is the case, things aren't too bad. The destructor will need
to destroy or zero any smart pointers contained in the array, which
shouldn't be too hard.

This will enable me to stick with a relatively standard coding style,
I'll just have to create the objects by means of factory methods
rather than new'ing them.

Regards/Ole Nielsby
Jul 21 '06 #7

"Ole Nielsby" <ol*********@snailmail.dka écrit dans le message de news:
uE**************@TK2MSFTNGP04.phx.gbl...
>
<ad******@club-internet.frwrote:
>>
Most important, it violates the C++ standard, which means
that whatever solution you could devise, it may break on
the next compiler release or service pack, and would most
probalby be not portable.

"not portable" would mean, some of the code might have to
be rewritten for ports. If I wrap the dirty tricks in bag of
templates or whatever, I should be able to minimize the
rewriting to just that.
>Can't you fix an upper-bound to the array size [...]

No. Most of them will be small - a few will be huge, and
I want non-virtual (inline) methods to work on both small
and large.
>>Is it, by any chance, possible to call constructor methods
explicitly on an uninitialized memory block, and will this
initialize the vtable pointer? Or is there a way to explicitly
initialize it?

placement-new.

Thanks - this seems to be what I need.

Now, if I do a naive delete on a *MyClass created this way, am
I right in assuming that this will call the destructor of MyClass,
and after that, deallocate the memory?
No. You can't call delete on those objects. You must *explicitely* call the
destructor, then free the memory buffer by hand.

Note that if you overload placement-new for your class, you also must
provide a placement-delete operator, but it is used only if the
placement-new throws an exception. For a complete explanation, see
http://www.parashift.com/c++-faq-lit...html#faq-11.14

Arnaud
MVP -VC
Jul 22 '06 #8

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

Similar topics

7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
4
by: Piotr Sawuk | last post by:
Hello, I'm new in this group and new to c++ programming. And I already have my first question which wasn't answered by any text-book on c++ programming I have seen so-far: How can I define a...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
4
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
5
by: Lyle Avery | last post by:
Hello guys, Look at this in c++ file: class T { public: char c; char ca; };
4
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
10
by: Karlo Lozovina | last post by:
Hi, what's the best way to keep track of user-made subclasses, and instances of those subclasses? I just need a pointer in a right direction... thanks. -- Karlo Lozovina -- Mosor
33
by: Peng Yu | last post by:
Hi, __PRETTY_FUNCTION__ is a macro that gives function name, etc. I'm wondering if there is a macro to get the class name inside a member function. Thanks, Peng
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.