Malloc question,... again!

This is a discussion on Malloc question,... again! within the C Programming forums, part of the General Programming Boards category; Please take a look on this... Code: #include <stdlib.h> //malloc, free, itoa #include <stdio.h> //printf #define POSSIBLE_MAXIMUM_INT_VALUE 10 char *IntToStr(int ...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Talking Malloc question,... again!

    Please take a look on this...
    Code:
    #include <stdlib.h> //malloc, free, itoa
    #include <stdio.h>  //printf
    
    #define POSSIBLE_MAXIMUM_INT_VALUE 10
    
    char *IntToStr(int value)
    {
      char *buffer = malloc(POSSIBLE_MAXIMUM_INT_VALUE);
      itoa(value, buffer, 10);
                       // ^-- base 10
      return buffer;
    }
    
    int main()
    {
      int value = 2008;
    
      printf("%s\n", IntToStr(value));  // >> 2008
      //Question: Should we deallocate the returned value?
    
      //do we need to get the buffer first??
    
      //get the returned value
      char *buffer = IntToStr(value);
    
      //do something with buffer
      printf("%s\n", buffer);           // >> 2008
    
      //deallocate the buffer
      free(buffer);
    }
    Thanks in advance V(^_^)V

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    There is really only 1 question... and the answer is yes.

    Don't forget to check the return value of malloc() before using it.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Then we should warn users to deallocate our returned value in our documentation... *sob*

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You should provide some sort of "deallocation" function. Since the users (or in future; you) may use some other function other than free() (ie you didn't use malloc or some such).

    This ensures that your pointer returned from malloc() will be free'd by its direct sister free(). IMO, a better method in this case is to have the user pass in a char pointer to where they want the data to go;

    Code:
    #include <stdio.h>
    
    int IntToStr(int value, char * str, size_t n)
    {
       /* you can now do error checking and return a result (ie, 0 pass, non-zero fail) */
    
    }
    
    int main(void)
    {
       char x[6];
    
       if(IntToStr(50, x, sizeof(x)) != 0)
       {
          printf("error!\n");
       }
       return 0;
    }

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thanks zacs7!

    I think I should change "the way" to develop a function.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    In a nutshell, because you return a pointer which points to malloced memory, you must free it.
    For every time you call malloc, you call free.
    What did you think there was an exception or were confused?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    What did you think there was an exception or were confused?
    Well, I just curious about this statement:
    Code:
    printf("%s\n", IntToStr(value));
    A wrong way to make a function in C. (~*.*~)"

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    A wrong way to make a function? Huh?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It is the wrong way to format something that is supposed to return an integer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  4. malloc, calloc question
    By chen1279 in forum C Programming
    Replies: 12
    Last Post: 09-07-2006, 05:54 PM
  5. Question about malloc()
    By cdalten in forum C Programming
    Replies: 6
    Last Post: 05-12-2006, 10:57 AM

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