A
atv
at least to me it is. I can't figure out for the life what it is i'm
doing wrong here.
i have a function called assign_coordinate. usually, i pass a malloced pointer
to it, then individual pointers are malloced within that function. No
problem here.
Now i have a situation in this software, where i need to assign new coordinates
to all nodes in the list.
The struct looks something like this:
struct test {
other variables;
char *x;
char *y;
char *z;
struct test *next;
struct test *prev;
}
So i first free the pointers x,y,z like so:
while(tmp!=NULL) {
free(tmp->x);
free(tmp->y);
free(tmp->z);
Then call assign_coordinate:
assign_coordinate(tmp); (Note that tmp itself is NOT free, only x,y,z
pointers)
and so on:
tmp=tmp->next;}
assign_coordinate looks like this:
GLvoid assign_coordinate(objects *tmp)
{
objects *search=o_head;
if(!tmp->prev&&object_count==1)
srand((unsigned int)time(NULL));
/* Assign coordinate planes */
if((tmp->x=malloc(sizeof(float)+7))==NULL) {
fprintf(stderr,"assign_coordinate: Could not allocate memory for x plane\n");
perror("malloc");
exit(1);}
snprintf(o_tail->x,sizeof(float)+7,"%.0f",((grid_size-grid_start)*rand())/RAND_MAX+grid_start);
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);
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)))
{
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");
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. 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.
Thanks for any help.
Rgds,
-alef
doing wrong here.
i have a function called assign_coordinate. usually, i pass a malloced pointer
to it, then individual pointers are malloced within that function. No
problem here.
Now i have a situation in this software, where i need to assign new coordinates
to all nodes in the list.
The struct looks something like this:
struct test {
other variables;
char *x;
char *y;
char *z;
struct test *next;
struct test *prev;
}
So i first free the pointers x,y,z like so:
while(tmp!=NULL) {
free(tmp->x);
free(tmp->y);
free(tmp->z);
Then call assign_coordinate:
assign_coordinate(tmp); (Note that tmp itself is NOT free, only x,y,z
pointers)
and so on:
tmp=tmp->next;}
assign_coordinate looks like this:
GLvoid assign_coordinate(objects *tmp)
{
objects *search=o_head;
if(!tmp->prev&&object_count==1)
srand((unsigned int)time(NULL));
/* Assign coordinate planes */
if((tmp->x=malloc(sizeof(float)+7))==NULL) {
fprintf(stderr,"assign_coordinate: Could not allocate memory for x plane\n");
perror("malloc");
exit(1);}
snprintf(o_tail->x,sizeof(float)+7,"%.0f",((grid_size-grid_start)*rand())/RAND_MAX+grid_start);
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);
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)))
{
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");
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. 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.
Thanks for any help.
Rgds,
-alef