Cell wrote:
> I have structures like this in my program -
A problem with "like this" code fragments is that when
someone sees a problem with them and then tries to explain to
you the nature of the problem, it may have nothing to do with
the actual problems of the actual original code.
Long ago when kings were regarded as semi-divine, it was
seen that their divinity did not shield them from illness and
they sometimes needed treatment from doctors. But most doctors
were not of noble blood, and in some places it was unthinkable
for a commoner to touch the Royal Person. So the king would
nominate some less-divine person as a surrogate, who would
imitate the king's malady while submitting to the doctor's
examination. The proprieties were respected, but what do you
think of the likely accuracy of such diagnosis by proxy?
> typedef vector vectorstruct
For example, this line is invalid and will not compile.
It serves no purpose except to distract attention from the
question you are actually interested in.
> {
> double x, y,z;
> } vector;
>
> typedef struct verticesstruct
> {
> vector v;
> } vertex; /*a vertex is a vector */
Seems a rather pointless (pun intended) struct; why
bother having it at all?
> typedef struct trianglestruct
> {
> int v0,v1, v2;
> }triangle;
>
> typedef struct objectstruct
> {
> int nvert;
> int ntri;
> vertex *vert;
> triangle *tri;
> }object;
>
> ...............
> ..............
>
> later somewhere in the program i have a statement like this -
>
> object *obj;
>
> obj->vert = malloc( nvert * sizeof(vertex)); /* Creating an array of
> vertices */
You're carefully allocating space for a bunch of vertices,
but you've forgotten to allocate space for the object. The
pointer variable `obj' has not been initialized; it "has a
garbage value." When you try to store a value in `obj->vert',
anything at all might happen.
(Oh, you say you *do* allocate some space and point obj
at it? Well, why didn't you say so in the first place, by
posting actual code instead of "like this" code?)
If we assume that obj actually points to something usable
and that nvert is an integer that is non-negative and not too
huge, then the allocation is all right. It can be improved
just a little bit by writing it as
obj->vert = malloc(nvert * sizeof *obj->vert);
Either way, the allocation attempt should be followed
(usually immediately, but sometimes a little distance is
permissible) by a test to see whether the attempt succeeded:
if (obj->vert == NULL) die_or_take_corrective_action();
> obj->tri = malloc(ntri * sizeof(triangle));
Same remarks.
> for(i=0;i<obj->nvert; i++)/* trying to take input for each vertex
> which has x, y, z components as it is a vector*/
> scanf("%f %f %f", &obj->vert[i].x, &obj->vert[i].y, &obj->vert[i].z);
>
> ^^^^^^^^ is this above notation of creating an array and then
> accessing the elements correct ??
Yes, under reasonable assumptions about what the code
actually looks like. But you're using scanf() incorrectly:
"%f" is the wrong conversion specifier for double, and any
input failure will escape your notice entirely.
> later on i also do this -
>
> for(i=0;i<obj->ntri;i++)
> scanf("%d %d %d", &obj->tri[i].v0, &obj->tri[i].v1, &obj->tri[i].v2);
As before, the array accesses look fine given similar
assumptions, and as before the use of scanf() is broken.
Real code next time, please.
--
Eric Sosman
lid