Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > struct dynamic

Reply
Thread Tools

struct dynamic

 
 
Philipp Kraus
Guest
Posts: n/a
 
      12-08-2012
Hello,

I would like to create a dynamic struct:

struct reg[] =
{
{ name1, *pointer },
{ name2, *pointer },
....
{ NULL, NULL }
}

name is a char / string and pointer a pointer to a function. I have got
a std::map<std::string, void*>
and need to create the struct above. So how can I create the struct
from my map? I need exactly
the struct structure, because I need to push it to a C function call.

Thanks a lot

Phil

 
Reply With Quote
 
 
 
 
Philipp Kraus
Guest
Posts: n/a
 
      12-08-2012
On 2012-12-08 13:38:18 +0100, Paavo Helde said:

> Philipp Kraus <(E-Mail Removed)> wrote in news:k9v76e$9k9$1
> @ariadne.rz.tu-clausthal.de:
>
>> Hello,
>>
>> I would like to create a dynamic struct:
>>
>> struct reg[] =
>> {
>> { name1, *pointer },
>> { name2, *pointer },
>> ....
>> { NULL, NULL }
>> }
>>
>> name is a char / string and pointer a pointer to a function. I have got
>> a std::map<std::string, void*>
>> and need to create the struct above. So how can I create the struct
>> from my map? I need exactly
>> the struct structure, because I need to push it to a C function call.

>
> typedef std::map<std::string, void*> mymap_t;
>
> struct RegEntry {
> const char* name;
> void* pointer;
> RegEntry(const mymap_t::value_type& x)
> : name(x.first.c_str()), pointer(x.second) {}
> RegEntry(): name(NULL), pointer(NULL) {}
> };
>
> mymap_t mymap = ...;
>
> std::vector<RegEntry> reg(mymap.begin(), mymap.end());
> reg.push_back(RegEntry()); // the (0,0) terminator
>
> callcfunction(&reg[0], ...);
>
> The crucial point to note here is that the map may not be deleted or
> altered while reg is alive, otherwise the name pointers in reg would
> become invalid.


Thanks a lot, I miss the forest for the trees. Defining a struct for
each element
and push it to a vector (which is a linear sequence of a memory block),
and pusing
the reference of the first element to the C function.

I'm very happy Thanks

Phil

 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-08-2012
On Sat, 2012-12-08, Philipp Kraus wrote:
> Hello,
>
> I would like to create a dynamic struct:
>
> struct reg[] =
> {


That's an array, not a struct.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
BGB
Guest
Posts: n/a
 
      12-08-2012
On 12/8/2012 9:04 AM, Jorgen Grahn wrote:
> On Sat, 2012-12-08, Philipp Kraus wrote:
>> Hello,
>>
>> I would like to create a dynamic struct:
>>
>> struct reg[] =
>> {

>
> That's an array, not a struct.
>


yep.

(a lot of what follows can be considered side-information, as it may not
be directly relevant to the OP, and may be biased slightly in favor of
writing interpreters...).


this is a sort of case though where I more commonly use a struct
containing parallel arrays.

one array can identify the fields, another struct can hold the values.


there are different ways to approach this sort of problem, but here is
one possibility (example):
struct FieldInfo_s {
char *name; //name
char *sig; //signature string (identifies field type)
int flags; //access modifiers, ...
....
};

union ValueInfo_s {
void *pv;
int32 i; //substitute appropriate types
int64 l;
float f;
double d;
};

struct DynamicObject_s
{
FieldInfo **vars; //array of field references
ValueInfo *vals; //array of values
int n_vars; //number of vars in object
int m_vars; //max vars in object
};


pros:
more per-field information can be held this way, usually because the
fields can be managed independently of the objects;
more members can be added without re-allocating the object;
....

cons:
if the layout is static or semi-static, there are more efficient approaches;
using a union for the values, makes the union be the size of the biggest
possible value (larger value types may then require pointers, or may
otherwise waste lots of memory);
the little detail of "signature strings" (*1);
....



*1: side-note, on signatures and types:

a signature string, for example, basically being an ASCII string, with
various letters or letter-pairs indicating types.

for example (a small part of my notation):
'i'=int(32-bit), 'j'=uint, 'l'=long(64-bits), 'm'=ulong(64-bits),
'c'=char(8 bit), 'h'=byte, 's'=short, 't'=ushort, 'f'=float, 'd'=double,
'Cd'=complex-double,'Gd'=imaginary-double,
'r'=variant (dynamically-typed-reference, *2),
....

'P'=pointer, ex: "Pi"="int*", "PPv"="void **",
'A256;i'=int[256], "Qd"=double[],
'U<name>;'=user (named) type,
'X<name>;'=struct/union/value-class type,
'L<name>;'=reference class/interface type,
....

with "(...)..." basically giving the syntax for functions/methods.
"(Pcd)i" meaning "int _(char *, double);".

sadly, there is a bit more notation than this...


an advantage of ASCII here is "human readable" signature strings, vs say
using a binary representation and displaying them as hex, which is much
less readable.

note that signature strings may be orthogonal to the use of
"type-names", or basically, a higher-level name used to describe a type.


it is also generally more flexible and versatile than another
potentially tempting strategy: bit-packing all of the type information
into a 32 bit integer (say, with various internal bit fields giving the
type, size for small arrays, ...).

it may seem at first like 32-bits is plenty, but soon enough it gets to
be an ugly mess (with lots of conditionally-existent fields and flags, ...).

another case is using a struct.
structs may have the advantage of being faster and easier to work with
in many cases, but with drawbacks as well. hence, it may make sense to
use structs for some uses internally, but leave signature strings mostly
as the "canonical" representation for serialized types.


*2: dynamic types

usually, these take an entirely different approach.
fairly common is to by some means either encode the type into the
pointer/reference, or within the pointed-to object.

one popular option here is the use of "tag bits", where the low 3 or 4
bits or so of a pointer is used to encode parts of the type. a downside
though is that this forces any such pointers to be aligned (and also
typically involves bit-twiddly on the pointers).

another strategy (the one I am using), was to have special "magic"
address ranges (generally outside usable parts of the address space)
which encode certain types (such as small integers held in pointers).
this doesn't require any particular alignment for general pointers, but
may reduce the number of bits available for "special" types (for
example, only 28-bits for a "fixnum" type, rather than 30 bits, ...).
another minor advantage is that it doesn't require bit-twiddly on the
pointers (a person instead checks ranges and adds/subtracts a base address).


with most everything else, the type can be determined by the pointed-to
object (say, using a memory manager which is able to remember the types
of objects, which may be given when the object is allocated).

typically, in my case, these objects are identified via a type-name
rather than via a signature. some people prefer magic type-numbers, but
a drawback here is that they need to be centrally assigned to avoid
clashes, whereas with a string it is much easier to avoid a clash (and
they can be mapped to integer values internally).

the usual downside of dynamic typing though is that dynamic
type-checking isn't free (so, usually, it is much more preferable to
find ways around having to check the type).


or such...

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-08-2012
On Sat, 2012-12-08, Juha Nieminen wrote:
> Jorgen Grahn <(E-Mail Removed)> wrote:
>> On Sat, 2012-12-08, Philipp Kraus wrote:
>>> Hello,
>>>
>>> I would like to create a dynamic struct:
>>>
>>> struct reg[] =
>>> {

>>
>> That's an array, not a struct.

>
> Actually it's neither, because it's not valid syntax.


Oops. True.

> The original poster should specify whether he wants a struct or an
> array. (What he wrote seemed like what he wants is an array.)


I think it was pretty clear if you read between the lines: he has an
API which wants a pointer to a struct Foo array, with the last
element being a "special" terminator one. Like e.g. GNU's
getopt_long().

But it's hard to offer specific help when the OP is obviously confused
about basic aspects of the language.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
BGB
Guest
Posts: n/a
 
      12-08-2012
On 12/8/2012 1:26 PM, Juha Nieminen wrote:
> Jorgen Grahn <(E-Mail Removed)> wrote:
>> On Sat, 2012-12-08, Philipp Kraus wrote:
>>> Hello,
>>>
>>> I would like to create a dynamic struct:
>>>
>>> struct reg[] =
>>> {

>>
>> That's an array, not a struct.

>
> Actually it's neither, because it's not valid syntax.
>
> The original poster should specify whether he wants a struct or an
> array. (What he wrote seemed like what he wants is an array.)
>


it may have been intended more as a sort of free-form "informal" syntax
rather than as true/valid syntax...


 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      12-08-2012
Juha Nieminen wrote:

> The original poster should specify whether he wants a struct or an
> array. (What he wrote seemed like what he wants is an array.)


Maybe he wanted an aggregate data type, which includes, but isn't limited
to, arrays.


Rui Maciel
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
struct in struct Gunnar G C++ 14 06-02-2004 06:43 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments