TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: All
from: Kurt Kuzba
date: 2003-09-02 02:10:06
subject: Pointers

From: kkuzba{at}centurytel.net
To: c_echo{at}yahoogroups.com

* Author: Bo Simonsen
BS>  Can't i avoid to have a extra pointer?

    Yes, but not if you want to use dynamically allocated memory.
 If you are using dynamic allocation, then you need to keep the
 pointer in order to later free the allocated memory.  If you
 don't, then you've got a memory leak going on.  Having to declare
 all your variables is a bit of a nuisance, but it provides a bit
 of structure and forces you to plan more carefully.  You can use a
 global buffer variable and free it upon closing your app, but that
 is best reserved for private work.  In a team environment, global
 space is best left alone to avoid conflict.  Besides, if the
 pointer is part of a function other than main, it has the same
 lifespan as your stay in that function, and goes away when you
 exit back to the calling function.  You can play with allocation
 using static pointers, though, in the allocating function, and
 be sure to call the function to free() them before exiting.

char* test(int iMake)
{
    static char* cTmp = 0;
    if(cTmp) free(cTmp);
    if(iMake) cTmp = (char*)malloc(iMake);
    return cTmp;
}

    Here you have a static variable, and may use it however you
 like.  Calling the function with a value of zero tests to see if
 memory is allocated and calls free() if needed.  This could be
 used any number of times without causing a memory leak, and you
 would call it just before exiting your app as well, with an
 argument of 0.  It's a bit clumsy, but dynamic allocation is.
 This functions approximates a data object.

>  kkuzba{at}centurytel.net   http://home.centurytel.net/kkuzba
>  Men now fear and misdoubt the Elves, and yet know little of them.

--- SoupGate-Win32 v1.05
* Origin: kkuzba{at}centurytel.net (2:292/516.666)
SEEN-BY: 633/267 270
@PATH: 292/516 854 140/1 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.