Thread: Declaring dynamic vs fixed 2D char array

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    3

    Declaring dynamic vs fixed 2D char array

    For example I have a list of names to be created. I can use a **char (string array).

    Method 1:

    declaring a dynamic array

    Code:
        char **namelist = (char **) calloc(6, sizeof (char*));
    
    
        for (int i=0; i<6; i++)
        {
            namelist[i] = (char *) calloc(20, sizeof (char));
        }
    Method 2:
    Declare array with fixed size

    Code:
        char namelist[6][20];
    One of my colleague told me the second one is more appropriate because the first one will slow down while allocating small amount of memories in heap. Is that considerable or negligible?

    My second question, If I have to pass this array into another function, which is the right way to declare the array?
    Last edited by imuneer; 01-27-2016 at 02:09 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Common wisdom says that calloc() is slow. There are a bunch of explanations for it. I look at a failed or slow allocation like winning the lottery - you just get very unlucky. Unfortunately there isn't a lot you can do about that, if you need calloc(). Without knowing the compiler vendor's implementation strategy, it can feel impossible to create favorable conditions. Because different implementations can become slow or not work in different ways. For example, free list implementations of malloc() - which calloc() will probably call - might try to merge many small, previous allocations into a larger chunk, which can be slow. You could profile calloc() in the real code and see how slow it actually is - measuring is the only way to be sure. In a very recent thread, the subject came up (albeit in a slightly different context,) and the results were surprising, and favorable(!) when it was actually tested.

    Like I said, winning the lottery. It's just really hard to know what will happen.

    I would declare namelist on the stack because it can fit on the stack. 120 bytes is a very small amount. Eventually I stop thinking hard.

    For your second question, remember the goal is to do something like this:
    Code:
    foo(name);
    To achieve this, simply declaring foo like this actually works:
    Code:
    void foo(char namelist[6][20]);
    But the compiler only pays attention to dimension sizes after the first - so it could look more like:
    Code:
    void foo(char *name[20]);
    Last edited by whiteflags; 01-27-2016 at 04:00 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It should be:
    Code:
    void foo(char (*namelist)[20]);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Right, a pointer to a size 20 char array, not 20 char pointers. What a difference parens will make.

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    3
    Thanks @whiteflags & @laserlight,

    Is it the `calloc()` one which actually slows down or the entire method (method 1) ?

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    3
    With parenthesis it works or else it makes compiler warning and lead to seg fault while running..

    warning: passing argument 1 of ‘print_items_2’ from incompatible pointer type [-Wincompatible-pointer-types]
    print_items_2 (namelist, 6);

    Quote Originally Posted by laserlight View Post
    It should be:
    Code:
    void foo(char (*namelist)[20]);

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by imuneer View Post
    Thanks @whiteflags & @laserlight,

    Is it the `calloc()` one which actually slows down or the entire method (method 1) ?
    calloc() is the slowest part of method 1, so in the larger picture this question is kind of meaningless. If you have to use calloc() or its friends, though, you can allocate in ways that minimize the number of function calls. Read Question 6.16 for more information.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You can minimize your calloc calls like so:
    Code:
    char **namelist;
    
    // no need to use calloc here
    namelist = malloc(numrows * sizeof *namelist);
    
    // calloc all elements at the same time
    namelist[0] = calloc(numrows * numcols, sizeof **namelist);
    
    // assign rows to namelist elements
    for (int i = 1; i < numrows; i++)
      namelist[i] = namelist[0] + i * numcols;
    However, if you can use a fixed array then you may as well:
    Code:
    #define NUMROWS 6
    #define NUMCOLS 20
    //...
    char namelist[NUMROWS][NUMCOLS] = {{0}};;
    Last edited by algorism; 01-27-2016 at 09:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  2. Replies: 3
    Last Post: 05-30-2011, 02:29 PM
  3. weird static char pointer array
    By Anator in forum C Programming
    Replies: 4
    Last Post: 11-16-2009, 07:05 AM
  4. static char array
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2006, 06:47 AM
  5. dynamic char array
    By sufthingol in forum C++ Programming
    Replies: 5
    Last Post: 03-24-2005, 07:14 PM

Tags for this Thread