Ok say I have this

char* construct_keyname(char *key, char *postfix){
        char *keyname;
        keyname = (char *)malloc(KEY_SIZE*sizeof(char)); //allocate
memory that is freed inmemcache_gstr and memcache_gid

        snprintf(keyname,
                (
                       
(strlen(MEMCACHE_PRFX)+strlen(postfix)+strlen(key))
                        +3
                ),
                "%s%s_%s",
                MEMCACHE_PRFX,
                key,
                postfix);

        return keyname;
}

I have allocated memory for the prefix name

char *memcache_gstr(char *key, int id){
        char *ret;
        char *keyname;
        char keyid[4];
        sprintf(keyid,"%d",id);
        keyname= construct_keyname(key,keyid);
        printf("key: %sn", keyname);
        ret = getmc(keyname);
        free(keyname);
        return ret;
}
then I call it here and since the return value of the keyname
constructor is allocated memory can I free it with the pointer that I
assigned to it? ie: keyname

keyname is a pointer that points to the return value of
construct_keyname() and the return value is allocated on the "heap".
Is that right?
On 04/01/2012 at 7:51 AM, Paul J Stevens  wrote:On 04/01/2012 02:38
AM, skr...@hushmail.com wrote:
> https://github.com/skrapsrwt/dbmail-memcache/

I looked at the 'push change and added some comments.

Most importantly you need to understand C doesn't have automatic
allocation/de-allocation of memory like PHP does.

If you want to assign something to a chunk of memory that memory must
be
allocated, either on the 'stack', or on the 'heap'.

The stack is local to the scope. So it can be passed 'down-stream' if
you are careful, but it can never be returned to the calling scope.

The heap is where malloc/free do their thing: a global memory space
that
you can use - if you use it following the rules. Heap memory is always
safe to pass around.

- you must allocated everything you need.
- for each malloc call, there must be a free call.

stack allocation:

char foo[128]; // char array on the stack you can assign 128 bytes to

char *foo = g_new0(char, 128); // same size char array on the heap
g_new0 is a simple wrapper around calloc. It allocates memory and
cleans
it out.

If you allocate on the stack it is always a good idea to clean it
explicitely:

char foo[128];
memset(foo, 0, sizeof(foo));
-- 
________________________________________________________________
Paul J Stevens        pjstevns @ gmail, twitter, skype, linkedin

  * Premium Hosting Services and Web Application Consultancy *

           www.nfg.nl/i...@nfg.nl/+31.85.877.99.97
________________________________________________________________
_______________________________________________
Dbmail-dev mailing list
Dbmail-dev@dbmail.org
http://mailman.fastxs.nl/cgi-bin/mailman/listinfo/dbmail-dev
_______________________________________________
Dbmail-dev mailing list
Dbmail-dev@dbmail.org
http://mailman.fastxs.nl/cgi-bin/mailman/listinfo/dbmail-dev

Reply via email to