Server Help

ASSS Custom Projects - C help

Bak - Thu Jul 29, 2004 2:19 am
Post subject: C help
Okay I have a set of functions that represent an integer vector (dynamically allocated array that can expand). Now in my delete function I want to set the pointer to zero, so that I know it's not pointing to valid data. My orginal code had:

Code: Show/Hide
void delete_IntVector(IntVector *iv)
{
   free(iv->data);
   free(iv);
   iv = 0;
}


It's easy to see why this wouldn't work, as iv is a copy of the original pointer, and setting the copy wouldn't affect the original pointer. I know in c++ you can do pointers by reference, and actually change the pointer. This doesn't seem to work in C. My next trial solution was to use a pointer to a pointer:

void delete_IntVector(IntVector **intVec)
{
IntVector *iv = *intVec;
free(iv->data);
free(iv);
iv = 0;
}
and when I call it just do:

delete_IntVector(&iv);

where iv is a IntVector*

Now I don't know why this code didn't set the original pointer to zero. I finally settled on a solution where I set it to zero after I call the function, a poor workaround, in my opinion.

Does anyone know what was wrong with the second version, or how I would accomplish what I wanted to do? Thanks.

EDIT: wtf is up with the code tags... whenever I add more than one it cuts the forum to half a page width...
Mr Ekted - Thu Jul 29, 2004 3:55 am
Post subject:
This is a very good question. All you people trying to learn C/C++ should attempt to understand what is going on here.

Instead of:

Code: Show/Hide
iv = 0;


do:

Code: Show/Hide
(*intVec) = 0;  // don't need the parens, but I like them in this case


The reason is this. You are correct to pass the address to your pointer to the function. This way, the function actually knows where the calling code's pointer is stored. Inside your function, you de-reference the pointer-pointer, so you have a simple pointer to the structure, just as the calling code does. This is local data--only valid inside the function, lost when the function returns. If you want to affect the pointer of the calling code, you need to use the address passed to you, which is intVec--a pointer to the original pointer.
Tommyhawk - Thu Jul 29, 2004 5:26 pm
Post subject:
Agreed good question.

Alternatively he could have done

Code: Show/Hide
*iv = 0;

Mr Ekted - Thu Jul 29, 2004 6:33 pm
Post subject:
True. Same thing. But I find using the original param name shows more clearly that you are affecting something outside the scope of the function.
Bak - Thu Jul 29, 2004 7:52 pm
Post subject:
Tommyhawk wrote:
Agreed good question.

Alternatively he could have done

Code: Show/Hide
*iv = 0;


iv is pointing to the actual IntVec struct in memory. If I do *iv = 0 won't that just change the data in the struct, rather than the pointer?

And If I do it after I delete it, aren't I modifing a struct which has been deleted, and could be being used by other variables?



Doing (*intVec) = 0 makes sense Ekted, thanks.
Mr Ekted - Thu Jul 29, 2004 8:21 pm
Post subject:
Yes, you are right Bak. I take back what I said. I spoke too quickly. "*iv = 0;" is in fact an error, and will not even compile.
Tommyhawk - Fri Jul 30, 2004 12:30 am
Post subject:
Meh mb. I had the arrows on the diagram in my head in slightly different places tongue.gif
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group