variable-sized class

P

Piotr Sawuk

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?
 
P

Piotr Sawuk

I said:
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?

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?
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;}
^^^^^
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...
 
J

John Harrison

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){}
}

That is not legal C++. You cannot miss out the array size like that.
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];
}

Neither is that legal C++, unless TEXTSIZE is a compile time constant.
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;}
}

That trick is also not legal C++, although it is going to work on many
compilers.
or whatever neccessary to override the usual allocation of
memory for the object?

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
 
J

John Harrison

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.
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?

^^^^^
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...

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
 
M

Makc

John Harrison said:
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.

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top