variable-sized class

Discussion in 'C++' started by Piotr Sawuk, Jul 19, 2004.

  1. Piotr Sawuk

    Piotr Sawuk Guest

    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
    Piotr Sawuk, Jul 19, 2004
    #1
    1. Advertising

  2. Piotr Sawuk

    Piotr Sawuk Guest

    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
    Piotr Sawuk, Jul 19, 2004
    #2
    1. Advertising

  3. On 19 Jul 2004 09:18:17 GMT, Piotr Sawuk <> 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
    John Harrison, Jul 19, 2004
    #3
  4. On 19 Jul 2004 19:13:10 GMT, Piotr Sawuk <> 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
    John Harrison, Jul 19, 2004
    #4
  5. Piotr Sawuk

    Makc Guest

    "John Harrison" <> 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)
    Makc, Jul 22, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sanborne

    variable sized port map

    sanborne, Mar 18, 2006, in forum: VHDL
    Replies:
    6
    Views:
    2,904
    Mike Treseler
    Mar 26, 2006
  2. thechaosengine

    Oddly sized sized password textbox

    thechaosengine, Sep 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    559
    David Hearn
    Sep 15, 2005
  3. me

    Variable sized type

    me, Nov 5, 2004, in forum: C++
    Replies:
    3
    Views:
    282
    Arijit
    Nov 6, 2004
  4. Michael B Allen

    Variable Sized Struct Members?

    Michael B Allen, Sep 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    243
    Derrick Coetzee
    Sep 26, 2004
  5. Chris Johnson

    Global variable-sized arrays

    Chris Johnson, Nov 23, 2004, in forum: C Programming
    Replies:
    5
    Views:
    380
    Mark A. Odell
    Nov 23, 2004
Loading...

Share This Page