Thread: Two dimensional array as buffer for string list

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    596

    Two dimensional array as buffer for string list

    I have a function which sets a pointer equal to a requested list of strings.
    Some of the string lists are fixed:
    Code:
    char *configlist[NCONFIGS] = {"Ser", "Par", "Coil a", "Coil b"};
    char *taperlist[NTAPERS] = {"Lin", "Log1, Log2"};
    Other string lists are created within the function, and they need to be of the same type as the fixed lists (char **) so that either can be assingned to the pointer.
    And so I have:
    Code:
    char *itemlist[MAXITEMS];
    char listbuffer[MAXITEMS][MAXITEMLENGTH]:
    and the string pointers initially assigned:
    Code:
    for(int i = 0; i < MAXITEMS; i++)
        itemlist[i] = listbuffer[i];
    Is it safe to use a two dimensional array as buffer like that?

    Is listbuffer[n] guaranteed to point to listbuffer[n][0] ?

    -

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is it safe to use a two dimensional array as buffer like that?
    Yes, it looks OK.

    > Some of the string lists are fixed:
    > Other string lists are created within the function
    You need to be careful about how you modify strings, if some are indeed fixed.

    > Is listbuffer[n] guaranteed to point to listbuffer[n][0] ?
    listbuffer[n] could have been written as &listbuffer[n][0], so yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by Salem View Post
    > Some of the string lists are fixed:
    > Other string lists are created within the function
    You need to be careful about how you modify strings, if some are indeed fixed.
    Ok, thanks.

    Yes, the fixed strings will remain untouched.

    Is there any advantage or disadvantage to using malloc() to create the buffer in a case like this?

    -

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If every string is close to MAXITEMLENGTH in length (the average is less than a pointer + malloc overhead), then the array is probably best.

    But if you have many strings of widely differing lengths, then mallocing each one to the right length may be better.

    But you also create the additional complication of perhaps having some pointers to allocated memory, and other pointers to string constants. This would complicate things when it comes to freeing memory.

    The malloc overhead is the additional data needed by malloc to manage the memory pool. Each malloc call allocates the 'n' bytes you request.
    But additionally, there may be things like:
    - next/prev pointers
    - the size of the block allocated
    - padding at the end of the block to preserve memory alignment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Thanks. Interesting info about the malloc() overhead.

    Sounds like an array here is best then. These strings will be pretty short, maybe 8 or 10 characters including the null.

    Not sure what you mean about the problems of freeing memory, though.

    If I did use malloc() for the buffer, it would be for only a single instance of the buffer, and that instance would be used throughout the program.

    -

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I mean if you do

    itemlist[i] = "a string";

    then later

    itemlist[i] = malloc(20);

    you may have a problem when it comes to freeing memory if you can't be sure whether itemlist[i] points at malloc'ed memory or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Ok, I see.

    In this case, itemlist would be dedicated to the malloc'd memory.

    It would be a separate pointer that the requested list is assigned to:
    Code:
    char *configlist[NCONFIGS] = {"Ser", "Par", "Coil a", "Coil b"};
    int coilcapvalue[NCOILCVALS] = {50, 60, 70, 80, 90, 100, 110, 120, 130, 140};
    
    int getitemlist(int paramtype, char ***list)
    {
        switch(paramtype)
        {   case PT_COILCAP:
                for(int i = 0; i < NCOILCVALS; i++)
                    sprintf(itemlist[i], "%2d pF", coilcapvalue[i]);
    
                *list = itemlist;
                return NCOILCVALS;
    
            case PT_CONFIG:
                *list = configlist;
                return NCONFIGS;
        }
    
        return 0;
    }
    Only two case examples shown - some cases will assign fixed string lists to the supplied pointer, and other cases will create the required strings in itemlist and assign itemlist to the pointer.

    -
    Last edited by megafiddle; 05-25-2016 at 08:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-10-2011, 07:47 PM
  2. How to put a string in a 2 dimensional character array?
    By atif7865 in forum C Programming
    Replies: 2
    Last Post: 12-05-2008, 10:26 PM
  3. String-length of an multi-dimensional array
    By doxii in forum C Programming
    Replies: 15
    Last Post: 10-30-2008, 02:36 PM
  4. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM

Tags for this Thread