Thread: malloc for structs and arrays

  1. #1
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    malloc for structs and arrays

    Hey guys,

    First of all I know there have been previous threads like this and that there's also a faq item about this.
    But I am unable to apply any of those examples to my problem, simply because I haven't done this before and I dont understand the instructions I've read. Therefore I hope you can help me with this specific problem.

    Here's my problem:
    I have a LINE_COUNT defined as 10000:

    Code:
    #define LINE_COUNT                    10000
    And I have some structs and arrays defined that use this LINE_COUNT value:
    Code:
       char buf[BUFSIZ], id_file[256], id_file_array[LINE_COUNT][30], id_array[LINE_COUNT][30];
    
       struct CAN_line
       {
          int cnt;
          int time;
          int identifier;
          int datalength;
          int db[8];
       };
       struct CAN_line linenr[LINE_COUNT];
    Now when I increase the LINE_COUNT to say 11000 the program hangs. I figured it would have to do with memory available and came across the malloc function as a possible solution for this.
    My question is, how should I use malloc in this situation?
    Do I request memory for every array and the struct that use this LINE_COUNT? Or maybe I can simply request an amount of space when I launch the program?
    Anyway, I have tried the following line but I haven't got a clue if it's right or not and when it is I wouldn't even know how to check if it actually works...

    Code:
    q = (char *) malloc(2 * sizeof(struct CAN_line));
    ...
    free(q);
    Sorry for the noob level of this question It's just so not clear for me how this works.

    Any help would be greatly appreciated!

    René
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    malloc will return a NULL pointer if the size of the block to be allocated is zero, or if there was insufficient memory

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rkooij
    My question is, how should I use malloc in this situation?
    Do I request memory for every array and the struct that use this LINE_COUNT?
    Yes. Something a bit like this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LINE_COUNT 10000
    
    struct CAN_line
    {
       int cnt;
       int time;
       int identifier;
       int datalength;
       int db[8];
    };
    
    int main()
    {
       char buf[BUFSIZ], id_file[256];
       char **id_file_array;
       char **id_array;
       struct CAN_line *linenr;
       int i;
    
       id_file_array = malloc(LINE_COUNT * sizeof *id_file_array);
       if ( id_file_array != NULL )
       {
          for ( i = 0; i < LINE_COUNT; ++i )
          {
             id_file_array[i] = malloc(30 * sizeof *id_file_array[i]);
             if ( id_file_array[i] == NULL )
             {
                /* ADD CLEANUP */
                return 0;
             }
          }
       }
    
       id_array = malloc(LINE_COUNT * sizeof *id_array);
       if ( id_array != NULL )
       {
          for ( i = 0; i < LINE_COUNT; ++i )
          {
             id_array[i] = malloc(30 * sizeof *id_array[i]);
             if ( id_array[i] == NULL )
             {
                /* ADD CLEANUP */
                return 0;
             }
          }
       }
    
       linenr = malloc(LINE_COUNT * sizeof *linenr);
       if ( linenr == NULL )
       {
          /* ADD CLEANUP */
          return 0;
       }
    
       /* ... */
    
       for ( i = 0; i < LINE_COUNT; ++i )
       {
          free(id_file_array[i]);
          free(id_array[i]);
       }
       free(id_file_array);
       free(id_array);
       free(linenr);
       return 0;
    }
    Quote Originally Posted by rkooij
    Or maybe I can simply request an amount of space when I launch the program?
    Possibly. Are these variable declared within a function? Could they instead be global?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Thank you very much for the example. I have tried it but I get an error message:

    invalid conversion from `void*' to `char**'
    For example on this line:

    Code:
       id_file_array = malloc(LINE_COUNT * sizeof *id_file_array);
    Have I done something wrong?

    Thanks again, René
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Looks like you try to compile this c-code as c++.
    In C a void* is compatible with any pointertype.
    Kurt

  6. #6
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by ZuK
    Looks like you try to compile this c-code as c++.
    In C a void* is compatible with any pointertype.
    Kurt
    Ok, I changed to a C file but now I get looooads of other errors... is 'bool' as a definition not C compatible? What can I do about that?

    syntax error before "exit_flag"
    Code:
    bool exit_flag=FALSE, error_flag=FALSE, file_flag=FALSE, capture_flag=FALSE;
    I use Dev-C++ 4.9.9.2 by the way...

    Thanks!
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    no there is no bool in c.
    if you want to compile your code as c++ you have to use new instead of malloc ( the preferred way ) or cast the return of malloc to the right type.
    but if it's just the bool that makes troubles you could simply put a
    Code:
    typedef int bool;
    in your code
    Kurt

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by ZuK
    no there is no bool in c.
    if you want to compile your code as c++ you have to use new instead of malloc ( the preferred way ) or cast the return of malloc to the right type.
    but if it's just the bool that makes troubles you could simply put a
    Code:
    typedef int bool;
    in your code
    Kurt
    or if you have a c99 compatible compiler

    #include <stdbool.h>


    int main(void)
    {
    bool b = false;

    return 0;
    }

  9. #9
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Dave_sinkula thanks so much for the example and the rest of you guys for the tips regarding boolean. I used Laserve's tip to include stdbool.h and it's all working perfectly fine now!
    Have on question left though... in the example I see the comment line:
    Code:
    /* ADD CLEANUP */
    What exactly is meant with that line? I dont have a clue what to add...

    Thanks again, this was the only thing left for me to create a stable base for my program.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    For every address returned by malloc :
    In order to free memory use the function free().

    like
    Code:
    char  *p;
    
    if ( ! ( p = malloc(20)) )
       {
       printf("malloc error\n");
       return;
       }
    
    /* do things with p */
    
    
    free(p); /* Let the program reuse this memory space
                     for other malloc calls */

  11. #11
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by dude543
    For every address returned by malloc :
    In order to free memory use the function free().

    like
    Code:
    char  *p;
    
    if ( ! ( p = malloc(20)) )
       {
       printf("malloc error\n");
       return;
       }
    
    /* do things with p */
    
    
    free(p); /* Let the program reuse this memory space
                     for other malloc calls */
    Hehe, errmm sorry... is that an answer to the question in my last post?
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Yes. Remember to /*add cleanup*/ by free()-ing all your memory when you're done.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  13. #13
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by jafet
    Yes. Remember to /*add cleanup*/ by free()-ing all your memory when you're done.
    Sorry guys, I still don't understand what to add. The memory request works perfectly as it is now, and I use free() at the end of the function to clear the memory space. I can see that it works in the task manager.
    What do you mean with cleanup? Is there something I forget or is the above sufficient for the right functionality of my program?

    Edit: hmmmm, do you mean I need to free the memory there as well...? So when "if ( id_file_array[i] == NULL )" is true, that means the memory allocation failed?

    Sorry, for the noobness
    Last edited by rkooij; 05-03-2006 at 01:35 AM.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You've added the free() already? Well... pat yourself on the back, that's whap "cleanup" means
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  15. #15
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by jafet
    You've added the free() already? Well... pat yourself on the back, that's whap "cleanup" means
    Hahahaha! Yay!
    I haven't added free() on that line but at the end of the function, my guess according to the above is that I have to put it at the /* Add Cleanup */ line as well
    Thanks for the help
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  2. Confused about malloc and arrays
    By mc61 in forum C Programming
    Replies: 5
    Last Post: 01-15-2008, 06:22 PM
  3. Large arrays, malloc, and strcmp
    By k2712 in forum C Programming
    Replies: 1
    Last Post: 09-24-2007, 08:22 PM
  4. malloc of char arrays
    By St. Jimmy in forum C Programming
    Replies: 3
    Last Post: 02-21-2005, 04:40 AM
  5. arrays with malloc
    By loobian in forum C Programming
    Replies: 3
    Last Post: 10-15-2001, 01:25 PM