Raised This Month: $51 Target: $400
 12% 

AMX Pointers


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 10-23-2012 , 09:53   AMX Pointers
Reply With Quote #1

I have a really dumb question but I need the answer.

If I use MF_GetAmxString to get the pointer of the parameter as string. It's necessary to free it at function end?

And what happens if I want to keep using that pointer during the game?
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 10-23-2012 , 11:34   Re: AMX Pointers
Reply With Quote #2

Try to free it. If testing server will crash, freeing is not required. Otherwise, it's okay.
That's my opinion.
__________________

Last edited by claudiuhks; 10-23-2012 at 11:34.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
micapat
Veteran Member
Join Date: Feb 2010
Location: Nyuu, nyuu (France).
Old 10-23-2012 , 11:58   Re: AMX Pointers
Reply With Quote #3

It's certainly AMXX Module who manages this.
Just look how works "entity_set_string" for instance

If you want to use the string during the game, copy it in another one, or call ALLOC_STRING / STRING.
__________________
micapat is offline
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 10-23-2012 , 12:12   Re: AMX Pointers
Reply With Quote #4

Quote:
Originally Posted by micapat View Post
It's certainly AMXX Module who manages this.
Just look how works "entity_set_string" for instance

If you want to use the string during the game, copy it in another one, or call ALLOC_STRING / STRING.
Yes. Currently I'm copying the string to a newly allocated pointer to be safe.
Thanks
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 10-23-2012 , 12:15   Re: AMX Pointers
Reply With Quote #5

He's talking about MF_GetAmxString, where you have such code :

Code:
...
register char* dest = buffor[id];
char* start = dest;
...
return start;
AFAIK, register tries to keep the same pointer, which means it would not be a good idea to free it.
And since he needs to keep using the same pointer during the game, the answer is he should make a copy of the string.

EDIT: Ninja'd.
__________________

Last edited by Arkshine; 10-23-2012 at 12:16.
Arkshine is offline
gladius
Veteran Member
Join Date: Jul 2008
Location: Santiago, Chile
Old 10-23-2012 , 16:17   Re: AMX Pointers
Reply With Quote #6

Use free() when pointer does point to a block of memory previously allocated with malloc, calloc or realloc. Otherwise, if pointer does not point to a block of memory allocated with the above functions, the behavior is undefined. And if is a NULL pointer, the function does nothing.

Also, if you wanna keep your pointer during the game, copy it and never change original/header Pointer.

Ex(Linked list)
Calling & setting Header...
Code:
Node *head;
head = new Node(value);
On another function...
Calling & Setting Linked list...
Code:
Node *aux = head;
while(aux)
{
         aux = aux->next;
}
aux = new Node(value);
On another function...
Calling & Displaying Linked list...
Code:
Node *aux = head;
while(aux)
{
         cout << aux->GetValue() << endl;
         aux = aux->next;
}
And I will never lose my first datum. (works like your question)
Not the best example but I wanna show you how to keep a pointer and not lose its value.
__________________
Proyects
Kreedz Chile Mod [100%] (Fixing some details).


Last edited by gladius; 10-23-2012 at 16:17.
gladius is offline
Send a message via MSN to gladius Send a message via Skype™ to gladius
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 19:01.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode