C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Creating a data type dynamically

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Pramod
Guest





PostPosted: Thu Jul 15, 2004 11:51 pm    Post subject: Creating a data type dynamically Reply with quote



I need to create a data type whose size is determined at runtime. A
part of the program looks like this:

class CMI {
size_t objSize;
public:
union {
char data[objSize];
CMI * next ;
};
CMI(size_t s):objSize(s) {}
void func() { ... }
};

and this class will be used like this:

class AUI {
static CMI auicmi ;
public:
void foo() { auicmi.func() }
};
CMI AUI::auicmi(sizeof(AUI));

Is there any way to achieve this?

"char data[objSize]" is wrong since objSize is not a compiler
constant.
Can templates help?

Thanks
-Pramod

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Fri Jul 16, 2004 11:14 am    Post subject: Re: Creating a data type dynamically Reply with quote



Pramod wrote:
Quote:
I need to create a data type whose size is determined at runtime.

Impossible.

Quote:
A part of the program looks like this:

class CMI {
size_t objSize;
public:
union {
char data[objSize];
CMI * next ;
};
CMI(size_t s):objSize(s) {}
void func() { ... }
};

Now for the workarounds:
1. you could make it a template, taking the objSize as one argument, i.e.
making different types for different sizes.
2a. you could create external storage with new/malloc, or, better, use
std::vector or something like that.
2b. You could even allocate that storage so that the fixed parts of the
object are on the same slice of memory as the non-fixed ones.
A short sketch of what I mean[1]:
CreateCMI(size_t size){
void* ptr = ::new(sizeof (CMI) + size); //TODO: padding/alignment
new (ptr)CMI(size);// placement new
return ptr;
}
char* CMI::get_storage(){
return static_cast<char*>(this+1);
}
3. you could change your design. Without knowing what you are trying to
achieve it is impossible to tell though.

Uli

[1] A GCC extension allows things like
struct header
{
size_t size;
size_t used;
char array[0];
};

This struct is then allocated with malloc() or similar tools and the array
gets the overallocated storage. Are there any pros/cons to that technique?

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michiel Salters
Guest





PostPosted: Fri Jul 16, 2004 1:59 pm    Post subject: Re: Creating a data type dynamically Reply with quote



[email]pramod_iitg (AT) yahoo (DOT) com[/email] (Pramod) wrote in message news:<581b1e22.0407150426.b7e7292 (AT) posting (DOT) google.com>...
Quote:
I need to create a data type whose size is determined at runtime. A
part of the program looks like this:

class CMI {
size_t objSize;
public:
union {
char data[objSize];
CMI * next ;
};
CMI(size_t s):objSize(s) {}
void func() { ... }
};

and this class will be used like this:

class AUI {
static CMI auicmi ;
public:
void foo() { auicmi.func() }
};
CMI AUI::auicmi(sizeof(AUI));

Is there any way to achieve this?

"char data[objSize]" is wrong since objSize is not a compiler
constant.

There is quite a lot wrong with such a design. There are much saner
implementations, which would do what you want. But basically, yes,
templates will work: (ignoring xs)

class CMI_base {
virtual void func() = 0;
};
class CMI_link : public CMI_base {
void func() { ; }
CMI_base* next;
};
template< typename TheObjectToStore >
class CMI_data : public CMI_base {
TheObjectToStore obj;
void func() { ; }
};

class AUI {
static std::auto_ptr<CMI_base> auicmi ;
public:
void foo() { auicmi->func(); }
};
std::auto_ptr<CMI_base> AUI::auicmi(new CMI_data<AUI>);

Regards,
Michiel Salters

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.