Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C dynamic structure

Name: Anonymous 2011-12-05 10:24

Something I've been wondering,

Is it possible at runtime to dynamically create a struct? I don't mean dynamic array size, I mean the variables and such inside a structure.

Name: Anonymous 2011-12-05 10:25

void* C style (RTTI)Runtime type inference
inb4 FV

Name: Anonymous 2011-12-05 10:26

Of course it is. Make a hash table mapping symbols to values.
It's the basis of Greenspunning!

Name: Anonymous 2011-12-05 10:26

I do not know if there is a simpler way, but anything is possible through reflection.
http://en.wikipedia.org/wiki/Reflection_(computer_programming)

Simply put C can do this by running two (or more) programs that constantly modify and recompile one another. Thus anything that is possible at write-time is possible at run-time by utilizing this technique. Efficiency is another question.

Name: Anonymous 2011-12-05 10:30

>>2
Not sure what you mean, but I'll do some googling

>>4
This seems awfully complex.

Name: 2 2011-12-05 10:33

>>5
you could create a place holder void*(or a couple void** etc) for a and try mapping the struct to it with some serious unsafe dark magic

face it OP, this type of stuff isn't really what C is designed for

Name: Anonymous 2011-12-05 10:34

>>5
It is incredibly complex, and only pays off for certain very complicated programs.

Name: Anonymous 2011-12-05 10:39

>>6
Oh I already assumed so. It's more or less just being curious and I kind of thought I'd like to try it.

Name: Anonymous 2011-12-05 10:58

>>6
Black magic is best magic. I bet you can pull of some wizardry with an array of dynamically-sized arrays, each of which represents an object, using an hash table to identify each sub-array. The type of a variable can be encoded into the last few bits of every variable in each sub-array.

It probably won't work very well, but it sounds fun.

Name: Anonymous 2011-12-05 11:02

>>9
afters hours of segfaults, stackdumps, random bugs,wtf-logic
you can into wizardry (aka 'achieve satori') or instanity, not that normalfags can tell the difference

Name: Anonymous 2011-12-05 11:15

[code]
template <class T> struct something {
    T something_Good;
};
[code]

I think it's time you moved up to C++. It's not technically runtime, but it's damn close to what you want

Name: Anonymous 2011-12-05 11:16

>>11

Fuck, forgot to use /

Name: Anonymous 2011-12-05 11:17

Perhaps, but then he wouldn't be able to get closer to satori through trying 9.

Name: Anonymous 2011-12-05 11:18

Does making everyday dynamic type data structures in C make me a wizard?

Name: Anonymous 2011-12-05 11:18

Can you please sage posts in threads that are on the first page and are not a major contribution? This is the board's culture and I'd appreciate it if you respected it. Thanks.

Name: Anonymous 2011-12-05 11:20

>>15
Suck my anus

Name: Anonymous 2011-12-05 11:21

>>14
It merely means that you are on the path, as many are.

Name: Anonymous 2011-12-05 11:22

>>16
I'd prefer to program it.

Name: Anonymous 2011-12-05 11:37

>>1

The very question you've proposed seem to loudly denounce the fact that you know very few of C itself.

Name: Anonymous 2011-12-05 13:55

>>19
If it ain't lisp, it's crap.

Name: Anonymous 2011-12-05 14:14

>>20
Lisp is shit.

Name: Anonymous 2011-12-05 15:37

Yes, it's possible. Just grab a chunk of memory with malloc() and divide it up as you see fit.

The hard part is keeping track of what's stored where.

Name: Anonymous 2011-12-05 15:55

>>22
The hard part is keeping track of what's stored where.
Sure, mapping a few symbols or numbers to pointer is ``hard''.

Name: Anonymous 2011-12-06 2:50

A struct serves a mapping from member names to values in the struct. In C, a struct is efficiently implemented as a block of memory that has been reserved, say of size n bytes. There is an associated mapping from member names to offsets in the struct: f: {names} -> {offsets}. When you use structs in C, the function f is defined at compile time, and it is applied to the member names at compile time to determine offsets into a struct to be used at runtime. If you would like to dynamically create and use structure definitions at run time, then you will need to manage the mapping, f with some kind of implementation that will exist at run time. Basically, you are asking if it is possible to have a data structure where given string keys, you can get values. And yes, there are many choices you can use for this, like hash maps, or trees. You could try to compactify the data structure into something like a C struct, where you have a chunk of memory with offsets into it reserved for certain values. You could then manage the size of the block of memory and the mapping to the offsets using another data structure, or maybe a function. If you wanted to, you could say that an array is like a struct, but the member variable names are numbers, instead of strings.

Name: Anonymous 2011-12-06 5:07

>>24
That's what >>3 said but in a more concise way.

Don't change these.
Name: Email:
Entire Thread Thread List