can this be possible?

A

Alef.Veld

Some reassurance here please. This crashes on my computer. When you're
in a while loop, maybe a very tight one, is it in any case possible
that the free in this case would occur before the first fprintf thus
crashing the program ?

Code:
while(tmp!=NULL) {
         fprintf(stderr,"reassign for : %s from %s %s\n",tmp-[QUOTE]
hostname,tmp->x,tmp->y);[/QUOTE]
         free(tmp->x);
         free(tmp->y);
         free(tmp->z);
         assign_coordinate(tmp);
         fprintf(stderr,"to: %s to %s\n",tmp->x,tmp->y);
         tmp=tmp->next;
        }
}
 
R

Richard Heathfield

(e-mail address removed) said:
Some reassurance here please. This crashes on my computer. When you're
in a while loop, maybe a very tight one, is it in any case possible
that the free in this case would occur before the first fprintf thus
crashing the program ?
No.

while(tmp!=NULL) {
fprintf(stderr,"reassign for : %s from %s %s\n",tmp-
free(tmp->x);
free(tmp->y);
free(tmp->z);
assign_coordinate(tmp);

Depending on what that does...
fprintf(stderr,"to: %s to %s\n",tmp->x,tmp->y);

....this may or may not be using indeterminate values of tmp->x and tmp->y
and it's impossible to tell which without knowing what assign_coordinate()
does.
 
A

Alef.Veld

(e-mail address removed) said:

Ok, that's all i wanted to know. Then there's a different problem. Let
me ask you
another question then; if i have a pointer to a struct like :

struct bla {
char *x;
char *y;
char *z;}

I free those values, and then pass the pointer (mind you, only the
pointer to the
struct, not the x,y and z pointers inside the struct which have been
freed) to a
function where i assign new values to x,y and z. Like so:
function(tmp).
Would this work? Do i not need to pass the specific address of the x,y
and z
pointers as you would do when you want to use that specific pointer
for allocation,
for example? like function(&tmp), and in this case, function(&tmp-
x,&tmp->y,&tmp->z) ?

Or are these not neccesary because they are part of a struct. I would
not think so,
after all, they are still pointers which need to be initialized
right ?

I hope i'm making sense.
 
R

Richard Heathfield

(e-mail address removed) said:

Ok, that's all i wanted to know. Then there's a different problem. Let
me ask you
another question then; if i have a pointer to a struct like :

struct bla {
char *x;
char *y;
char *z;}

I free those values, and then pass the pointer (mind you, only the
pointer to the struct, not the x,y and z pointers inside the struct
which have been freed) to a function where i assign new values to
x,y and z. Like so:
function(tmp).
Would this work?

Yes, you can do this:

#include <string.h>
#include <stdlib.h>

int function(struct bla *p)
{
int rc = 0;
p->x = malloc(6);
if(p->x != NULL)
{
++rc;
strcpy(p->x, "hello");
}
p->y = malloc(6);
if(p->y != NULL)
{
rc += 2;
strcpy(p->y, "world");
}
p->z = malloc(7);
if(p->z != NULL)
{
rc += 4;
strcpy(p->z, "series");
}
return rc;
}
 
M

Michael

(e-mail address removed) said:

It is, however, possible that your output is buffered in such a way
that the output of the first fprintf might not occur on your terminal
until after the frees have executed. (So in other words, the fprintf
executes, but the characters it generates float in OS la-la land for a
bit before they make it to your eyes.) In that case, if you're
relying only on fprintf results to let you know what's working and
what isn't, you might be misled.

It might be very useful to use a debugger with this code.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top