Thread: mallocing with outside pointers

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    mallocing with outside pointers

    i having problems with this kind of code:

    Code:
    int malloc_me(char *ptr) {
        ptr = malloc(10);
        
        strcpy(ptr, "it works");
    
        return 0;
    }
    
    int main() {
        char *test;
        
        malloc_me(test);
        
        printf("%s\n", test);
        
        free(test);
    
        return 0;
    }
    im attempting to malloc vars in different functions, but the output of this is garbage. why?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a pointer to a pointer:
    Code:
    int foo( int ** v )
    {
        *v = malloc( ... );
    }
    Or do it the same way malloc does, and return the pointer.

    [edit] Fixed variable name mismatch. Damn phones anyway... [/edit]

    Quzah.
    Last edited by quzah; 07-03-2003 at 07:19 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    this is starting to bug me now...

    file a:
    Code:
    int html_createstarttag(char *dest, char tag[]) {
        *dest = malloc( strlen(tag) + 3 );
    
        sprintf(**dest, "<%s>", tag);
    
        return 0;
    }
    file b:
    Code:
    int html_findstarttag(char **src, char tag[]) {
        char *search;
        int tmp;
        
        html_createstarttag(&search, tag);
        tmp = str_whereisstring(src, search);
        free(search);
        
        return tmp;
    }
    
    int main() {
        char test[] = "<test>new idea</test>";
        int x;
        
        x = html_findstarttag(test, "test");
        
        printf("%s\n", x);
    
        return 0;
    }
    i tried throwing in a extra *, still not working right.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    // This is where you want to double pointer.
    int html_createstarttag(char **dest, char tag[]) {
        *dest = malloc( strlen(tag) + 3 );
    
        // Notice we have to dereference the pointer-pointer to get the pointer to the char data (the string).
        sprintf(*dest, "<%s>", tag);
    
        return 0;
    }
    
    // why is src a double pointer? I think you meant just one.
    int html_findstarttag(char **src, char tag[]) {
        char *search;
        int tmp;
        
        html_createstarttag(&search, tag);
        tmp = str_whereisstring(src, search);
        free(search);
        
        return tmp;
    }
    Last edited by Speedy5; 07-03-2003 at 08:51 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int html_createstarttag(char *dest, char tag[]) {
        *dest = malloc( strlen(tag) + 3 );
    
        sprintf(**dest, "<%s>", tag);
    You are trying to do two different things. First off, your function only takes a pointer to a char. Then you try to malloc to that char, which is really only a reference to the original pointer. It will not update what the original pointer points to.

    Use this example:
    Code:
    void fun( char ** c )
    {
        *c = malloc( 30 );
    
        sprintf( *c, "hello!" );
    }
    
    int main( void )
    {
        char *ptr;
    
        fun( &ptr );
        printf("Outside 'fun': %s\n", ptr );
        free( fun );
    
        return 0;
    
    }
    That should illustrate what you need.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    why do i even need a **? why cant a just use a & going to a *?

    wouldnt that give the pointer the address?

  7. #7
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    They would point to the same location, but they wouldn't be the same pointer.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because that's the way it works. It's just like when you pass a variable by value. It makes a copy of it and passes it to the function. Pointers basicly work the same way. Getting the address of a variable which is the argument of a function won't give you the same one as the non-local variable.

    As such, you cannot update what the pointer points at. Consider the following:
    Code:
    void foo( char * ptr )
    {
        printf("The address of ptr is %p.\n", &ptr );
    }
    
    int main( void )
    {
        char * c;
    
        foo( c );
        printf("The address of c is %p.\n", &c );
        return 0;
    }
    There's your answer.

    [edit] Damnit, that's twice I've been beaten to the reply. [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    sorry, but im stilling missing something. maybe something will sink in soon. sorry to keep asking the same question.

    Code:
    int html_findstarttag(char *src, char tag[]) {
        /* src now points to test. */    
    
        char *search;
        int tmp;
        
        /* send the location of search to dest. */
        html_createstarttag(&search, tag);
        tmp = str_whereisstring(src, search);
        free(search);
        
        return tmp;
    }   
    
    int main() {
        char test[] = "<test>new idea</test>";
        int x;
        
        /* send the location of test to src */
        x = html_findstarttag(test, "test");
        
        printf("%s\n", x);
    
        return 0;
    }
    
    -----------------
    
    int html_createstarttag(char *dest, char tag[]) {
        /* dest now points to search. so its the same thing. */
    
        /* the location that dest points to is the return of molloc. */
        &dest = malloc( strlen(tag) + 3 );
    
        sprintf(dest, "<%s>", tag);
    
        return 0;
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look at my example again. If you are trying to set a pointer outside a function, to something new inside the function, then you need to pass a pointer to the pointer you want to change. Otherwise, you cannot change what the pointer is pointing at.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void fun1( char * c )
    {
    	c = malloc( 6 );
    	strncpy( c, "hello", 6 );
    
    	printf("%s\n", c );
    	printf("'c' outside of this function still points to NULL.\n");
    	printf("Memory leak in progress...\n");
    }
    
    void fun2( char **c )
    {
    	*c = malloc( 6 );
    	strncpy( *c, "Hola!", 6 );
    	printf("%s\n", *c );
    	printf("'c' outside of this function now points to allocated memory.\n");
    }
    
    int main ( void )
    {
    	char *c = NULL;
    
    	printf("'c' points to %p\n", c );
    	printf("Calling fun1.\n");
    	fun1( c );
    
    	printf("\n'c' points to %p\n", c );
    	printf("Calling fun2.\n");
    	fun2( &c );
    
    	printf("'c' points to %p\n\n", c );
    	printf("'c' is %s\n", c );
    	free( c );
    
    	return 0;
    }
    In the first function, you just pass a pointer. The pointer doesn't point at anything. You try and make it, but since you're really using a copy of the pointer, you can't change the actual pointer itself.

    This is exactly the same thing that happens with any other variable passed by reference. You cannot change that variable. The difference, and your source of confusion, is that with a pointer, you can change what that pointer points at.

    Since this pointer has nothing it points to, there is nothing to change.

    The second function, we pass a pointer to that pointer. Now you can make that pointer point to something else, because you're using a pointer to it to do it.

    Remember above: you can change what a pointer points at.

    Since you have a pointer that points at another, you change what it points to. Here, we make that pointer point to newly allocated memory.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    this is starting to bug be now...thanks for the help, to bad its not sinking in...

    heres a single file that works:
    Code:
    int main() {
        char test[] = "<test>new idea</test>";
        
        findtag(test, "test");
    
        return 0;
    }
    
    int findtag(char *src, char tag[]) {
        char *search = NULL;
        
        createtags(&search, "TEST");
        
        printf("search = %s\n", search);
    
        return 0;
    }
    
    int createtags(char **dest, char tag[]) {  
        *dest = malloc(20);
        strcpy(*dest, "it works!!");
    
        return 0;
    }
    that was just something i made up, because i was tired of playing with the real code.

    then i then i tried the real code. i get this when i compile it (from gcc)
    html_findtags.c: In function `html_findstarttag':
    html_findtags.c:21: warning: passing arg 1 of `html_createstarttag' from incompa tible pointer type
    and its code
    Code:
    /* html_findtags.c */
    
    int html_findstarttag(char *src, char tag[]) {
        char *search = NULL;
        int tmp;
        
        html_createstarttag(&search, tag);     /* line21 */
        tmp = str_whereisstring(src, search);
        free(search);
        
        return tmp;
    }   
    
    int main() {
        char test[] = "<test>new idea</test>";
        int x;
        
        x = html_findstarttag(test, "test");
        
        printf("%s\n", x);
    
        return 0;
    }
    Code:
    /* html_createtags.c */
    
    int html_createstarttag(char **dest, char tag[]) {
        *dest = malloc( strlen(tag) + 3 );
    
        sprintf(*dest, "<%s>", tag);
    
        return 0;
    }
    i used it like a template, i think.

    what am i missing this time?


    this is really bugging me now, sorry.........

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Well it's consistent with the implementation of the function in html_createtags.c, but is it consistent with the prototype in html_createtags.h ?
    duh!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM