Is there a way that I can dynamically allocate the members of X to the size of each corresponding string in the array, using a loop? I don't want to do the following, since my structure has more elements, it just seems downright inefficient.
It looks like I just need a way to index each member...I don't know if that's possible, so if not, how else would i accomplish this?
Name:
Anonymous2007-03-24 22:50 ID:JzN3vnpK
Um, why can't you do, say:
struct X constructX ( const char *a, const char *b, const char *c ) {
X x;
x.a=a;
x.b=b;
x.c=c;
return x;
}
X usage = constructX("hey", "yo", yeps");
usage will fall out of scope and be freed automatically.
Name:
Anonymous2007-03-24 22:52 ID:teS+kxN0
Not reliably/portably with thing as you have them structured now, no, because the compiler may add padding in X. Realistically, yes, you probably could iterate through the members of X if they're all just pointers anyway. Don't do it, though, it's evil.
(And manually malloc'ing every member isn't any less efficient than looping through them. Usually when you encounter ugliness like this, it's time to reconsider your design-- are you sure that this is a logically sound way to maintain and manipulate your data? Perhaps you could come up with a better design.)
Go with whatever you can understand and modify without any hassle. Fuck optimization. It can wait until you're done with the program (or until you're actually in need of it) where you can actually profile various methods and see which is the best.
Name:
Anonymous2007-03-25 6:11 ID:zLX2v719
All pointers are just 32-bit words(4 bytes), so here: memcpy(X.a, str[0], 12)
I have just created EviL code.
Name:
Anonymous2007-03-25 6:34 ID:WSu/sOeN
>>1
C doesn't support introspection to do what you want without copypasta of code. But I wonder why would you want to do that? Why can't you just define your strings in the structure, or have the array of strings as a member of the structure?
Name:
Anonymous2007-03-25 8:24 ID:uwojD6Za
Thanks all.
I think I'll just use fixed length array.
The original problem was that I have a textfile, with 8 string columns. I needed to load each line of the file into a structure, so I wanted to allocate just enough memory for each string.
Name:
Anonymous2007-03-25 8:42 ID:zPbYt2az
You don't want to type 4 extra lines of copypasta code, so you're going to implement it as either (A) Prone to buffer overflow or (B) wasteful of memory. Congratulations! You are now Yet Another Lazy Dev! Come work at my company, you'll fit right in.
Name:
Anonymous2007-03-25 14:01 ID:HQzfGGQb
>>5
Unless you're in a 64-bit environment, in which case pointers are 64 bits long. (On IBM's virtual whatchamacallit architecture, pointers are 128 bits long. Whoop!)
Name:
Anonymous2007-03-25 17:31 ID:E4jQoS3V
pointers are 128 bits long
What the fuck? Why?
Name:
Anonymous2007-03-25 18:07 ID:zLX2v719
>>9
64-bit architectures use different versions of the programs they run not unlike operating systems. That aside, who needs 64-bit registers - probably why I've never seen a 64-bit machine in actuality. Common only in textbooks.
I've always assumed 32-bit x86 architecture. So until I see a reason not to... pointers are 32-bit.
Name:
Anonymous2007-03-25 18:53 ID:zh30IF1B
>>11
it's not that hard to write code without assuming pointers are a certain size.