04 Sep, 2011, Rarva.Riendf wrote in the 1st comment:
Votes: 0
struct multi_purpose_data {
char *string;
double value;
};

typedef struct multi_purpose_data MP_DATA;

MP_DATA find_path(int in_room_vnum, int out_room_vnum, CHAR_DATA *ch, int depth, bool_t in_zone, bool_t isPath) {
static MP_DATA mp_data;
mp_data.value = -1;
mp_data.string = '\0'; // but this is wrong as I want to free the memory in case I used strdup or something in there

***
return smp_data;
}


Is the only way to know you already entered the method once is to use a static boolean ? how do you initialize a local static data ?
I did not find a good C course about those.
04 Sep, 2011, David Haley wrote in the 2nd comment:
Votes: 0
Why are you using a static variable?
04 Sep, 2011, Rarva.Riendf wrote in the 3rd comment:
Votes: 0
Because I read that returning a local variable of this kind is undefined. (and the reason is to not have to handle the allocated memory elsewhere)
05 Sep, 2011, Runter wrote in the 4th comment:
Votes: 0
If you use a static variable for this purpose there's only one variable to deal with. If you call the function multiple times it's not going to result in multiple return values. It's very similar to having a global variable. Static variables default to binary 0. If you have a struct it means every field in the struct will be 0. Initialization only happens once at bootup, not when the function is entered the first time.

int* function() {
static int value;
return &value;
}

vs
int* function() {
return calloc(0, sizeof(int));
}


In the first example calling this function returns the address of the same variable memory each time. If you call it 10 times the int* returned always points the same. Indeed, you don't have to free the memory because the memory is always used. Just like a global variable.

In the second example you would have to free the memory with free() at some point after the call. But the advantage is you don't always use the memory, and you can possible have multiple instances of the function result around at once. You can't do that int he first example.
05 Sep, 2011, Rarva.Riendf wrote in the 5th comment:
Votes: 0
@Runter: the problem is about the char* in the structure: I may or may not malloc it depending on the algorithm before I return it.
So when I come back in the method, I have to free it. The only way I know to do this is below, I was wondering if there was another/better way to do it:
struct multi_purpose_data {
char *string;
double value;
};

typedef struct multi_purpose_data MP_DATA;

static bool isInit = FALSE;

MP_DATA find_path(int in_room_vnum, int out_room_vnum, CHAR_DATA *ch, int depth, bool_t in_zone, bool_t isPath) {
static MP_DATA mp_data;
mp_data.value = -1;
if (isINIT)
free_string(&mp_data.string);
isInit = TRUE;
if (***)
mp_data.string = '\0';
else
mp_data.string = strdup(something);
***
return mp_data;
}
05 Sep, 2011, Runter wrote in the 6th comment:
Votes: 0
If you're freeing it every time you return to the function then why not just use a static string on the stack? Then you don't have to worry about all that.

char* function() {
static char buffer[256];
strcpy(buffer, "meh");
return &buffer[0];
}


Code that uses the return value should be unconcerned about its allocation method if it isn't managing the memory itself.
05 Sep, 2011, Rarva.Riendf wrote in the 7th comment:
Votes: 0
Runter said:
If you're freeing it every time you return to the function then why not just use a static string on the stack? Then you don't have to worry about all that.

Hmm because I forgot about using strcpy instead of using strdup()…thx
05 Sep, 2011, Rarva.Riendf wrote in the 8th comment:
Votes: 0
Oh and because my multi purpose data used a pointer…and I prefer to reuse code in general. I know there was a reason…I always have a hard time finding the best solution when I have to move char arrays around.
Random Picks
0.0/8