variable-sized class | | |
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 class who's size is only known at the
time the constructor gets executed -- without the overhead
of pointer-managment (for copying) and without any additional
memory getting allocated for size or location of the variable
sized member-data?
For example, I wanted to do something like this:
class text{
public:
const char txt[];
text(char[] n):txt(n){}
}
and maybe later extend it into
class xtext:public text{
public:
int colour;
xtext(char[] n, int c):colour(c){text(n);}
}
I expected the compiler to create something alike a struct
struct xtext{
int colour;
.... //other stuff neccessary for the class xtext or its parent-class
char txt[TEXTSIZE];
}
with TEXTSIZE being inserted at run-time by the constructor.
But all I got was an error about "incompatible types in
assignment char* to const char[0]".
Do I really need to use such tricks as I have seen in the
sources of XFree86, like for example
class text{
char txt;
text* create_text(char* t){
text* r=(text*)malloc(strlen(t)+sizeof(text)-1);
strcpy(&r->txt,t);
return r;
}
~text(){free this;}
}
or whatever neccessary to override the usual allocation of
memory for the object?
--
Better send the eMails to netscape.net, as to
evade useless burthening of my provider's /dev/null...
P | | | | re: variable-sized class
I wrote:
[color=blue]
> How can I define a class who's size is only known at the
> time the constructor gets executed -- without the overhead
> of pointer-managment (for copying) and without any additional
> memory getting allocated for size or location of the variable
> sized member-data?[/color]
and I should probably add that I'm intending to create a
"container" of such variable-sized (zero-terminated) strings
with an average size of 4-5 bytes, and I consider it to be
a huge waste of memory when each of those strings does
require an equal-sized (or bigger) pointer to get stored
along with the actual string. maybe someone does know of
an alternative solution? is there any compression-implementation
which does heavily use c++ and its classes (and is open-source),
maybe there I could find some example of how variable-sized
classes could get stored without space- and time-overhead?[color=blue]
>[/color]
[color=blue]
> class text{
> char txt;
> text* create_text(char* t){
> text* r=(text*)malloc(strlen(t)+sizeof(text)-1);
> strcpy(&r->txt,t);
> return r;
> }
> ~text(){free this;}[/color]
^^^^^
this probably should be "operator delete", and of course some
constructors need to be called along the way too, if the class
does actually contain more than just the char and optional int...
as I understood overloading "operator new" is not a solution
for me since it doesn't get passed any arguments from the
constructor -- unless I somehow hack gcc to do so...[color=blue]
> }[/color]
--
Better send the eMails to netscape.net, as to
evade useless burthening of my provider's /dev/null...
P | | | | re: variable-sized class
On 19 Jul 2004 09:18:17 GMT, Piotr Sawuk <piotr5@unet.univie.ac.at> wrote:
[color=blue]
> 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 class who's size is only known at the
> time the constructor gets executed -- without the overhead
> of pointer-managment (for copying) and without any additional
> memory getting allocated for size or location of the variable
> sized member-data?
>
> For example, I wanted to do something like this:
>
> class text{
> public:
> const char txt[];
> text(char[] n):txt(n){}
> }[/color]
That is not legal C++. You cannot miss out the array size like that.
[color=blue]
>
> and maybe later extend it into
>
> class xtext:public text{
> public:
> int colour;
> xtext(char[] n, int c):colour(c){text(n);}
> }
>
> I expected the compiler to create something alike a struct
>
> struct xtext{
> int colour;
> ... //other stuff neccessary for the class xtext or its parent-class
> char txt[TEXTSIZE];
> }[/color]
Neither is that legal C++, unless TEXTSIZE is a compile time constant.
[color=blue]
>
> with TEXTSIZE being inserted at run-time by the constructor.
> But all I got was an error about "incompatible types in
> assignment char* to const char[0]".
>
> Do I really need to use such tricks as I have seen in the
> sources of XFree86, like for example
>
> class text{
> char txt;
> text* create_text(char* t){
> text* r=(text*)malloc(strlen(t)+sizeof(text)-1);
> strcpy(&r->txt,t);
> return r;
> }
> ~text(){free this;}
> }[/color]
That trick is also not legal C++, although it is going to work on many
compilers.
[color=blue]
>
> or whatever neccessary to override the usual allocation of
> memory for the object?[/color]
What you are asking for is impossible. You are asking for a variable sized
object text, which nevertheless can be put into an array. That's flat out
impossible in any programming language. Array elements must be constant
size so that the compiler can calculate the position of each element by
multiplying the offset by the element size.
john | | | | re: variable-sized class
On 19 Jul 2004 19:13:10 GMT, Piotr Sawuk <piotr5@unet.univie.ac.at> wrote:
[color=blue]
> I wrote:
>[color=green]
>> How can I define a class who's size is only known at the
>> time the constructor gets executed -- without the overhead
>> of pointer-managment (for copying) and without any additional
>> memory getting allocated for size or location of the variable
>> sized member-data?[/color][/color]
Actually your best solution is probably the most user friendly one as
well. Namely
std::vector<std::string>
Many implementations of the standard library these days implement 'short
string optmisation'. Normally std::string holds pointers to the characters
of the string as you would expect. But if the string is short then short
string optmisation means that the characters are held directly in the
string without using pointers or allocating additional memory.
[color=blue]
>
> and I should probably add that I'm intending to create a
> "container" of such variable-sized (zero-terminated) strings
> with an average size of 4-5 bytes, and I consider it to be
> a huge waste of memory when each of those strings does
> require an equal-sized (or bigger) pointer to get stored
> along with the actual string. maybe someone does know of
> an alternative solution? is there any compression-implementation
> which does heavily use c++ and its classes (and is open-source),
> maybe there I could find some example of how variable-sized
> classes could get stored without space- and time-overhead?[color=green]
>>[/color]
>[color=green]
>> class text{
>> char txt;
>> text* create_text(char* t){
>> text* r=(text*)malloc(strlen(t)+sizeof(text)-1);
>> strcpy(&r->txt,t);
>> return r;
>> }
>> ~text(){free this;}[/color]
> ^^^^^
> this probably should be "operator delete", and of course some
> constructors need to be called along the way too, if the class
> does actually contain more than just the char and optional int...
>
> as I understood overloading "operator new" is not a solution
> for me since it doesn't get passed any arguments from the
> constructor -- unless I somehow hack gcc to do so...[color=green]
>> }[/color][/color]
You can pass arguments to operator new. It's known as placement new
class X
{
void* operator new(size_t bytes, int arg1, int arg2, int arg3);
};
X* p = new (1, 2, 3) X;
john | | | | re: variable-sized class
"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<opsbehupxa212331@andronicus>...[color=blue]
> Array elements must be constant
> size so that the compiler can calculate the position of each element by
> multiplying the offset by the element size.[/color]
Unless you use custom array template, like Borland's DyamicArray. But
then, you can't use pointer math - there's only integer indexing
option (or creating an array of intermediate pointers, which is no way
better) |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|