Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
C help

 
Post new topic   Reply to topic Printable version
 View previous topic  Loading a formula from an INI file Post :: Post %macro expander  View next topic  
Author Message
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Thu Jul 29, 2004 2:19 am    Post subject: C help Reply to topic Reply with quote

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...
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Thu Jul 29, 2004 3:55 am    Post subject: Reply to topic Reply with quote

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.
_________________
4,691 irradiated haggis!
Back to top
View users profile Send private message Add User to Ignore List
Tommyhawk
Newbie


Joined: Jul 14 2004
Posts: 14
Offline

PostPosted: Thu Jul 29, 2004 5:26 pm    Post subject: Reply to topic Reply with quote

Agreed good question.

Alternatively he could have done

Code: Show/Hide
*iv = 0;
Back to top
View users profile Send private message Add User to Ignore List
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Thu Jul 29, 2004 6:33 pm    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Thu Jul 29, 2004 7:52 pm    Post subject: Reply to topic Reply with quote

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.




pointerDiagram.PNG - 4.13 KB
File downloaded or viewed 45 time(s)
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Thu Jul 29, 2004 8:21 pm    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
Tommyhawk
Newbie


Joined: Jul 14 2004
Posts: 14
Offline

PostPosted: Fri Jul 30, 2004 12:30 am    Post subject: Reply to topic Reply with quote

Meh mb. I had the arrows on the diagram in my head in slightly different places tongue.gif
Back to top
View users profile Send private message Add User to Ignore List
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> ASSS Custom Projects All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 668 page(s) served in previous 5 minutes.

phpBB Created this page in 0.462493 seconds : 34 queries executed (78.4%): GZIP compression disabled