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

Pages: 1-

help with c structures

Name: Anonymous 2007-03-24 21:50 ID:Hy0hWHCa

hello 4chan,

say i have a structure:

struct X{
char *a;
char *b;
char *c;

};

and I also have an array of strings:

char *str[]={"hey","ho","yeps"};

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.

X.a=malloc(sizeof(char)*strlen(str[0])+1);
X.b=malloc(sizeof(char)*strlen(str[1])+1);
X.c=malloc(sizeof(char)*strlen(str[2])+1);

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: Anonymous 2007-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: Anonymous 2007-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.)

Name: Anonymous 2007-03-24 23:00 ID:Heaven

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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-03-25 17:31 ID:E4jQoS3V

pointers are 128 bits long
What the fuck? Why?

Name: Anonymous 2007-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: Anonymous 2007-03-25 18:53 ID:zh30IF1B

>>11
it's not that hard to write code without assuming pointers are a certain size.

Name: Anonymous 2007-03-25 20:03 ID:Heaven

sizeof(void*)

Name: Anonymous 2007-03-26 9:30 ID:WVCYtnj3

>>13
Unreliable. A void pointer may be smaller than a pointer that's full.

Name: Anonymous 2007-03-26 9:40 ID:VU2hSual

>>14

Don't be so silly

Name: Anonymous 2007-03-26 16:32 ID:o997JKpZ

>>14
I lol'd

Name: Anonymous 2009-01-14 13:27

WHBT

Name: ​​​​​​​​​​ 2010-09-07 14:11

Name: Sgt.Kabu੝kiman砑㘉 2012-05-28 23:48

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

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