simple char** practice

This is a discussion on simple char** practice within the C Programming forums, part of the General Programming Boards category; im just wondering if im using a 'char**' (and dynamic memory) properly, if you could let me know. ie, missing ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243

    simple char** practice

    im just wondering if im using a 'char**' (and dynamic memory) properly, if you could let me know. ie, missing another set of []'s or a * or & somewhere.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        
        char* test= "hello there";
        
        char** temp = (char**) malloc(sizeof(char**) * 1);
    
        temp[0] = (char*) malloc(sizeof(char*)* 20);
        
        strcpy(temp[0],test);
        printf("'&#37;s'\n",temp[0]);
        
        free(temp[0]);
        free(temp);
        
        getchar();
    }
    i know this program isnt useful, im just wondering if it is correct.
    thanks
    Last edited by nadroj; 04-12-2007 at 11:44 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Aside from all the pointless allocation typecasts, you're not quite there.
    Code:
    char **ptp;
    
    size_t num = 1; /* Purely for illustration purposes. */
    
    ptp = malloc( num * sizeof *ptp ); /* Allocate one pointer to a character. */
    if( ptp )
        ptp[0] = malloc( /* number of characters to allocate */ );
    ...
    free( ptp[0] ); /* Free allocated characters. */
    free( ptp ); /* Free the pointer to a character. */
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include<string.h>
    
    int main()
    {
        
        char* test= "hello there";
        
        char** temp = (char**) malloc(sizeof(char*) * 1);
    
        temp[0] = (char*) malloc(sizeof(char)* 20);
        
        strcpy(temp[0],test);
        printf("'&#37;s'\n",temp[0]);
        
        free(temp[0]);
        free(temp);
        
        getchar();
    }
    I believe you're allocating the wrong amount of memory, however, since char * and char ** are the same in your case, you shouldn't experience any problems for the first allocation. The second allocation is very different. You were allocating 4 times as much memory by allocating 20 char *'s when you should have been allocating 20 chars.

    Take care to understand what you are malloc()ing. This is why the FAQ on malloc() refers to using a format similar to this:

    Code:
    int *p;
    ...
    p = malloc(sizeof(*p));
    If you're using a C89 compiler, you probably casted malloc() to make the compiler shut up about the error, when the reason for the error was that you forgot to include stdlib.h. On a C99 compiler, you can't get away with that.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    thanks for the response.

    edit: @Quzah:
    im not sure what you mean though.. ie, what did i miss that i 'wasnt there'? (aside from the checking if malloc failed to allocate the memory, which i just overlooked and assumed it worked in my example)

    edit2: i think you were illustrating something that MacGyver said right? (i didnt pay close enough attention to it!)

    edit3: @MacGyver:
    Thanks alot for the info im using DevC++ and didnt get any warnings.. im sure i would have if i was on my linux boot with gcc.

    appreciated folks
    Last edited by nadroj; 04-13-2007 at 12:01 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nadroj View Post
    edit2: i think you were illustrating something that MacGyver said right?
    Considering that he posted after I did, no I wasn't illustrating his post.
    Quote Originally Posted by nadro View Post
    (i didnt pay close enough attention to it!)
    Then what's the point of asking a question here?


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

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i didnt mean you 'copied' off of him or something childish like that because i know you posted before him.

    and i simply overlooked the difference in your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 06:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21