Quote:Original post by DarkMatter2008
Quote:
Why are you using void? Are you going to have references to structs other than Poly? If not, forward declare Poly and use pointers to Poly.
Ther reason for the void instead of a Poly pointer is a matter of what comes first, the chicken or the egg?
What you wrote above doesn't compile in C, either, because you didn't typedef your structs (or, alternatively, write "struct Poly" instead of "Poly"). Just forward declare the struct:
typedef struct Vector Vector;typedef struct Poly Poly;struct Vector { Poly *refs;};struct Poly { Vector *vector;};
The same "trick" can be useful in C++ as well.
Quote:
Initally I was going to go with a list as they are easier to write to, but i'll only be writing to them at the initialisation stage, arrays access much faster, hmmm but saying that I'll be reading sequentially so would there be any diffence if i did use a list?
It's not something I know much about, but I believe iteration of an array can be faster due to the array being a single block of memory, but I don't know how much of an effect that would have in the present case.
Quote:
Quote:
If you're using arrays, then you'll want refs to be a pointer to pointer to Poly (Poly **refs;) so that you can point to an array of Poly structs.
I'm not sure what you mean here, is that 2D array? I only want a 1D array of Poly pointers per Vector Struct.
Right, you want an array of pointers. Usually, you work with a pointer to the first element of an array instead of the array itself (e.g. malloc() essentially returns such a pointer). If the array contains pointers, that would be a pointer to pointer. A 2D is something else entirely, but is often emulated by a pointer to pointer.
Quote:
Quote:
And, for what it's worth, // comments are only valid in C++ and C99 not C89.
I'm using VS2008, is there anyway to make it so it conforms to C? it doesn't seem to have just C in the create new project window.
I'm not familiar with VS2008, but it may not be a problem in your current case (these were common extensions to C89 which is why they're in the new version of C, C99). I was just pointing it out so that you'd know what's wrong if it does become a problem.
Quote:
Quote:
vectors.refs = malloc(sizeof(Poly *));
It's generally a good idea to write:
var = malloc(sizeof *var);
since it's a bit more robust in the face of changing the type or typename of the variable var.
again I'm not too sure what you mean here. Despite me decalring refs as a void pointer I only want it for Poly pointers, the type won't change. What I'm trying to do is set up an array of poly pointers in side the vector struct.
It's mostly a style guideline that I find can help to reduce problems down the line. It's also harder to mix up the type because you're basically letting the compiler figure it out for you. In another part of what you posted, you wrote "sizeof(Poly *)" when you meant "sizeof(Poly)", but if you'd used the "sizeof *var" form, it would've been a non-issue.
Quote:
I'm always open to a better solution if one is avalible, I lived a sheltered life in the land of Java so I am finding this a bit challenging to get a grasp of.
Well, for the allocation, just switch the order you do it. Just allocate the space you need right before you add another reference instead of allocating extra space after adding the reference.
The other thing to understand is the difference between pointers and variables, which in Java (I hope I get this right, it's been a while since I touched Java) is the difference between class references and primitive values, except that in Java only classes have reference semantics and only primitives have value semantics whereas in C (and C++) you can have a value of any type and a pointer to any type.
Quote:
Quote:
sizeof(Poly *) returns the size of a pointer to a Poly, which will be 4 on 32-bit platforms. I'm guess you want sizeof(Poly) here. It only works because your Poly consists of one pointer as well -- if it had anything else in there, it wouldn't work.
I thought because I want references instead of actually storing a struct in an array i'd use (Poly *), i'm trying to keep a record of what polygons have refernced each vector. Ideally the poly would have 3 pointers to vectors and other such information that a poly would require, i simplifed it for that demonstation.
Right, but you have to store the actual Poly structures somewhere (otherwise there's nothing to point to). In the code, it looked like you intended to store them in someModel.polys, so you need to store something the size of an actual Poly struct, not the size of a pointer to a Poly struct.
If you'd used the malloc(N*sizeof *var) format, the compiler would've seen that var is of type Poly *, so *var is of type Poly, and would've done the right thing.