Re: weird problem




On Jan 24, 7:51 pm, j...@xxxxxxxxxxx (Jens Thoms Toerring) wrote:

}
assign_coordinate looks like this:
GLvoid assign_coordinate(objects *tmp)What's 'objects' for a kind of type? You don't show the typedef you
obviously must be using. 'GLvoid' is probably only a fanvy name for
'void', isn't it?

GL is a prefix for using variables with GLUT. Yes they are the same.
objects is a typedef for a struct like:

typedef struct objects {
char *x;
char *y;
char *z;
} objects;



{
objects *search = o_head;What's 'o_head'?

o_head is a objects pointer pointing to the first node in the linked
list i'm using.


if ( ! tmp->prev && object_count == 1 )Where is 'object_count' defined and what's its type?

object_count is a global (declared extern in header and intialized in
other function). type is int.

srand((unsigned int)time(NULL));

/* Assign coordinate planes */
if ((tmp->x = malloc(sizeof(float) + 7)) == NULL) {What is using "sizeof(float)" in the calculation of how much memory you
need when printing a float or double value out in ASCII good for? This
doesn't make any sense and is probably dangerous since sizeof(float)
can differ between different machines while the ASCII string you want
to output stays the same.
I know this, i will just remove the sizeof float and put the amount of
bytes in there i need. I still need to change this, didn't get around
to it yet.


fprintf(stderr, "assign_coordinate: Could not allocate memory for x plane\n");
perror("malloc");Using perror() here is useless since malloc() doesn't set set errno if
it fails to obtain as much memory as requested. perror() is meant to
print out a human-readable string for what errno is set to. And if
there's nothing that woul have set errno what it prints out is com-
pletely bogus.


Hmm, yes kind of a shame malloc doesn;t use errno. I use perror because
i want to do consistent error reporting so i use it with every error,
if errno is used or not.
exit(1);
}
snprintf(o_tail->x, sizeof(float) + 7, "%.0f",
((grid_size - grid_start) * rand()) / RAND_MAX +
grid_start);Where is 'o_tail' defined? Are you sure you don't intend to write into
the string 'tmp->x' you just allocated before? And where are 'grid_start'
and 'grid_size' defined what what type do the have? Are they floats or
doubles or something else?

o_tail is a global pointing to the last node in the linked list. I'm
not sure you mean by 'i don't intend to write into
the string tmp->x you just allocated before'. I want to write into the
string, i just freed them remember? grid_start
and grid_size are floats. they are set in the beginning of the program
to -10 and grid_size is calculated dynamically, depending on how many
objects there are.

if ((tmp->y = malloc(sizeof(float) + 7)) == NULL) {
fprintf(stderr,"assign_coordinate: Could not allocate memory "
"for y plane\n");
perror("malloc");
exit(1);
}
snprintf(o_tail->y, sizeof(float) + 7, "%.0f", grid_object );Where's the definition of grid_object?

float, value is 2.0



if ((tmp->z = malloc(sizeof(float) + 7)) == NULL ) {
fprintf(stderr,"assign_coordinate: Could not allocate "
memory for z plane\n");
perror("malloc");
exit(1);
}

snprintf(o_tail->z, sizeof(float) + 7,"%.0f",
((grid_size - grid_start) * rand()) / RAND_MAX +
grid_start );

/* Check for possible collision */
while (search != NULL) {
if (collisions > 0 &&
collisions ==
((grid_size - grid_start) * (grid_size - grid_start))) {Where is 'collisions' defined and what type does it have? Looks like an
integer counter, but you seem to compare it to some float or double value,
which, if these assumptions should be true, is rather dubious to say the
least. But since I have no idea what it's meant to test I can't make any
suggestions on how to correct it.

collisions is int and defined as a global (and declared extern) . It is
only reset when the grid_size is increased, otherwise it is incremented
for every collision it matches.

fprintf(stderr,"assign_coordinate: Sector collisions "
"100%%.Collisions: %d - Objects: %d - Grid size: "
"%.0f [%.0f^2 sectors]\n",
collisions, object_count,
(grid_size - grid_start) * (grid_size - grid_start),
grid_size - grid_start);
perror("assign_coordinate");Again, using perror() is useless if nothing happened that would have set
errno.

I know :-)



exit(1);
}

if (tmp != search &&
!strcmp(tmp->x, search->x) &&
!strcmp(tmp->y, search->y) &&
!strcmp(tmp->z, search->z))
{
fprintf(stderr,"%s %s %s %s - %s %s %s %s\n",
tmp->hostname, tmp->x, tmp->y, tmp->z,
search->hostname, search->x, search->y, search->z);
collisions++;
free(tmp->x);
free(tmp->y);
free(tmp->z);
assign_coordinate(tmp);
}

search=search->next;
}
}
I have no snprintf check for error in there yet, but i checked with
printf statements if it
assigned properly, and it did.What did you printf() out? E.g. 'tmp->x' would point to uninitialized
memory, while printing out 'o_tail->x' probably won't tell you anything
about 'tmp->x' (unless you're hinding some code where you assign 'tmp'
to 'o_tail').

But, when going in the search loop,
apparently tmp->x y and z and search->x,y,z are not valid anymore
because they are both NULL so it seems. That's
why i get 100% collisions all the time, or so i gather. I don't
understand, because i'm
excluding myself (and thus my freed x,y,z) and i did not yet free
future nodes x,y,z in the list.Sorry, but nothing you write here makes much sense.
Eek :-P

How do you deter-
mine that tmp->x or tmp->y or tmp->z are NULL? And since you don't
show what 'search' is and how it got set up there's no way someone
could figure out what's going wrong.

search is just a tmp pointer set to the 'head' of the list. I don't
understand
why i would want to test x y and z for NULL. I free them before i dive
into
assign_coordinate, and allocate them right back after. So they are
always
allocated. The other pointers search encounters, are not yet freed and
still
valid.

If you post code it's best if
it's a complete, compilable program. If that is really not possible
take care to include definition of the variables being used and at
least give a hint what they are set to. And only post the code you
were actually using, not something you copied manually.

I'm using everything i posted, albeit somewhat simplified posted maybe.
I hope this clear things up, if not, i will try and post longer
listings of code with the variables
in there.

.



Relevant Pages

  • Re: short float ???
    ... takes at least 2 bitcounts to specify a float: ... typedef names in the hypothetical new header. ... would add clutter to an already cluttered system of predefined ... support, say, both 16-bit and 24-bit FP, while keeping 32-bit float. ...
    (comp.std.c)
  • Re: Float comparison
    ... If so why not use a typedef? ... It is a short form of PRECision. ... For testing purposes I must to compile and run program with double or float types. ...
    (comp.lang.c)
  • Re: Shortcircuitting with -Inf float value.
    ... > float a, b, d; ... a now contains -HUGE_VAL (errno might be set to ERANGE also) ... > Can we skip evaluation of sinusing compiler, since it is known that d ...
    (comp.lang.c)
  • Re: Float comparison
    ... It seems to obscure the code. ... Is PREC Italian ... for "large float" or something? ... If so why not use a typedef? ...
    (comp.lang.c)
  • Re: Bug/Gross InEfficiency in HeathFields fgetline program
    ... I would recommend: ... where Number is a typedef somewhere. ... precision class, but at least it will work transparently on float, ...
    (comp.lang.c)