Thread: malloc() strings VS array strings

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    malloc() strings VS array strings

    In other words:
    Code:
    char *chosen = malloc(255 * sizeof(chosen));
    /* Use string here */
    free(chosen);
    VERSUS
    Code:
    char chosen[255];
    /* Use string here */
    Does it even matter? The only reason to use the first approach with pointers would be to make the program run faster? Wouldn't they be the same speed as far as usage?

    This question comes up because I was doing a program that needed a string and I asked myself: "Why am I going like this?"
    Code:
    char *chosen = malloc(255 * sizeof(chosen));
    For any other reason besides practice? I must ask! I must search for a better reason!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There is no pratical reason in your example to use malloc() instead of an array. malloc()'s reason-for-being is dynamic memory allocation. As such, the parameter you pass to it is usually a variable, not a constant. For instance: char *ptr = malloc(runtime_determined_number);.

    The only reason I can think of to use malloc() in your example is to have the flexibility of the pointer (Ever tried array++;? It doesn't work). For instance, you might want to walk through the array. But that's not very good pratice anyway, since you have to find the beginning of the allocated memory again to free() it. A better solution for that would be to have a separate pointer. Something like: char array[255]; char *ptr = array;.

    Make sense?
    Last edited by itsme86; 01-10-2005 at 06:31 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by itsme86
    As such, the parameter you pass to it is usually a variable, not a constant. For instance: char *ptr = malloc(runtime_determined_number);.
    But you could also do that with a regular array:
    Code:
    char szList[SomeVarible];
    Quote Originally Posted by itsme86
    The only reason I can think of to use malloc() in your example is to have the flexibility of the pointer (Ever tried array++;? It doesn't work).
    But you could easily go like this to step through an array:
    Code:
    int i;
    for(i=0; i<LENGTH_OF_ARRAY; ++i)
         /* Do stepthrough code here */
    I need more reason!

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How about that malloc() requires a syscall to brk()/sbrk() which is slower than a normal array which is all allocated when the program is started. Not to mention that you don't have to bother with calling free() when using arrays; one less chance for a memory leak.

    And your method of creating an array of variable size doesn't work in all instances. Example:
    Code:
    {
      char *array = NULL;
      int size;
    
      printf("How big?\n");
      scanf("%d", &size);
      array = malloc(size);  // Try doing this with an array
      free(array);
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    {
      /*char *array = NULL;*/
      int size;
    
      printf("How big?\n");
      scanf("%d", &size);
      /*array = malloc(size);  // Try doing this with an array */
      char array[size];
      // free(array);
    }
    But I see what you mean lol, :P. Especially if you need to realloc or it's a global array!

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    another thing to consider is how large the chunk of data needs to be - most systems grant more dynamic storage than stack space...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  5. how to use malloc for array of strings
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 02-18-2005, 04:19 PM