Thread: Dynamic array (or something like that) in C

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    30

    Dynamic array (or something like that) in C

    Hello,

    I have a structure like this:

    Code:
    typedef struct
    {
    	char *name;
    	char keys[20][6];
    } row;
    However, I don't want to specify how many keys I can have in the array.
    Can I replace it with something like **keys and then reallocate memory when I add data ? Any suggestions how to do it ?

    Best wishes,
    D5

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can have either a pointer to a fixed array:
    Code:
    char (*keys)[6]
    or a pointer to pointer and allocate two arrays.
    Code:
    char **keys;
    In a 32-bit machine, if your key is always 6 chars, you loose 4 bytes per key if you go with the second approach.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    You can do it like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct
    {
    	char *name;
    	char **keys;
    } row;
    
    int main()
    {
        row temp;
        int i;
    
        temp.name = malloc(50 * sizeof(char));
        strcpy(temp.name, "Banana Man");
    
        temp.keys = malloc(5 * sizeof(char*));
    
        for ( i = 0; i < 5; ++i )
        {
            temp.keys[i] = malloc(3 * sizeof(char));
            memcpy(temp.keys[i], "1234567890" + i, 3);
        }
    
        puts(temp.name);
    
        for ( i = 0; i < 5; ++i )
        {
            int j;
    
            for ( j = 0; j < 3; ++j )
            {
                putchar(temp.keys[i][j]);
            }
    
            putchar('\n');
        }
    
        return 0;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The program never frees the memory. Bad.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by matsp View Post
    In a 32-bit machine, if your key is always 6 chars, you loose 4 bytes per key if you go with the second approach.
    Forgive my ignorance, but how are you losing 4 bytes?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cs32 View Post
    Forgive my ignorance, but how are you losing 4 bytes?
    Because you'll have an extra pointer for every array element, that you won't need in this particular case since you have fixed-width keys.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    The program never frees the memory. Bad.
    I'm sure you have my best interests at heart and weren't just trying to be a know it all, so thanks for the nitpick. Anyway, my OS will free memory when the program ends and there's no point being anal about that kind of thing when it's just a little example for this thread. You might as well blast me for using // comments in C, or using magic numbers, or mixing tabs and spaces for indentation while you're at it.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I could blame for all of it, but not freeing memory is not a good example for others, sooo...
    I know the OS frees up resources at the end of a program, but I find it very bad practice to rely on that. There was a discussion about it before, and most agreed that it might be an "optimization," but certainly not a good idea just to be sloppy.
    It's going to burn you someday, especially in C++.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Anyway, my OS will free memory when the program ends and there's no point being anal about that kind of thing when it's just a little example for this thread.
    I raised this point before, and while the opinion was divided, a general consensus is that we should teach best practices here. A simple solution is to show a code snippet instead, with the expectation that the reader should either know the rest, or better find out for himself.

    You might as well blast me for using // comments in C
    Those are standard in C99.

    or mixing tabs and spaces for indentation while you're at it.
    Elysia is well known for doing that, I do it myself when I find the indentation unacceptable for reading.
    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

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    most agreed that it might be an "optimization," but certainly not a good idea just to be sloppy.
    Because I relied on an "optimization" it means I was being sloppy?
    It's going to burn you someday, especially in C++.
    I doubt it, but you can believe whatever you want to.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Banana Man View Post
    Because I relied on an "optimization" it means I was being sloppy?
    Yes. It's sloppy not to free memory. There was no need for such an optimization here.

    I doubt it, but you can believe whatever you want to.
    Really? How well do you actually know C++?
    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.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    a general consensus is that we should teach best practices here
    Yeah, but who dictates what best practices are?
    Those are standard in C99.
    I thought "best practice" was not to use C99.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Banana Man View Post
    Yeah, but who dictates what best practices are?
    Describe to me how not freeing memory is good practice.
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, but who dictates what best practices are?
    Common sense: if you do not free what you allocate, you end up with a memory leak. That the OS may act like a garbage collector is irrelevant.

    I do not entirely agree with this stand myself (it sounds a little too harsh), but I have to agree that since we are concentrating on standard C, and standard C is not concerned with whether the OS does such a reclamation of memory, we should teach as if this is not the case, and indeed it might not be the case.

    You can read the discussion for yourself: Concerning delete/delete[] at program exit

    Note that it is in the C++ programming forum, not here.

    I thought "best practice" was not to use C99.
    That's Prelude's opinion, but since that version of the C standard exists, to say that // is not part of C would be factually incorrect.
    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

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Describe to me how not freeing memory is good practice.
    Sorry, but I don't want to take over this thread until desmond5 says the original question is answered. I also think that you're not going to take any of my opinions seriously, so explaining them to you is pointless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM