473,472 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
4 1704
I wrote:
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... }

--
Better send the eMails to netscape.net, as to
evade useless burthening of my provider's /dev/null...

P
Jul 22 '05 #2
On 19 Jul 2004 09:18:17 GMT, Piotr Sawuk <pi****@unet.univie.ac.at> wrote:
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
Jul 22 '05 #3
On 19 Jul 2004 19:13:10 GMT, Piotr Sawuk <pi****@unet.univie.ac.at> wrote:
I wrote:
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?

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?

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


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
Jul 22 '05 #4
"John Harrison" <jo*************@hotmail.com> wrote in message news:<opsbehupxa212331@andronicus>...
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)
Jul 22 '05 #5

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

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
7
by: Greg Collins [MVP] | last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions. What I've...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.