Thread: Allocating memory for multidimensional arrays - how to?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Allocating memory for multidimensional arrays - how to?

    Hello,

    I'm doing a project and I need a buffer to hold strings of 6 characters each. Though, the dimension of that array of strings is only known after the line

    scanf("%d", &numofflights);

    And I need to allocate memory for

    char buffercodes[][7];



    I have two problems:
    - First, I don't remember if we can leave one dimension of the array without a value and the other with a value.
    - Second, are this malloc and this type cast correct? buffercodes = (char **) malloc(numofflights* sizeof(char));


    Thanks for you help,
    J. Miranda
    Last edited by SpectreZz; 04-29-2011 at 08:08 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can do this:
    Code:
    #define STRLEN 7
    
    char *mat = malloc(num*STRLEN);
    You would then access (eg) the third string this way:

    Code:
    strcpy(mat + 2*STRLEN, "hello\n");
    Ie, you cannot use the index [] operator. For that, you do have to malloc both levels:

    Code:
    char **mat = malloc(num * sizeof(char*));
    for (i = 0; i < num; i++) {
    	mat[i] = malloc(STRLEN);
    }
    And you have to free() each of those individually before you free(mat). However, it makes everything else easier because you can refer to each string with [].

    Nb. that's sizeof(char*), not sizeof(char). Ask if you do not understand the difference. You could use STRLEN*sizeof(char) in the for loop, altho just "STRLEN" is fine unless you are on some very bizarre OS where a char != a byte.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    char (*buffercodes)[7] = malloc(numofflights * sizeof(*buffercodes) );
    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.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    Code:
    char (*buffercodes)[7] = malloc(numofflights * sizeof(*buffercodes) );
    Well, that looks better doesn't it? Just when I thot I knew everything
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    First, I don't remember if we can leave one dimension of the array without a value and the other with a value.
    No; at least, not in this context.
    Second, are this malloc and this type cast correct? buffercodes = (char **) malloc(numofflights* sizeof(char));
    There is no need for a cast. And the malloc() call is wrong. What you want is something like:
    Code:
    char (*buffercodes)[7];
    buffercodes = malloc(numofflights * sizeof *buffercodes);
    Here “buffercodes” is a pointer to an array of char. So buffercodes[0], [1], [2]... are each a char[7]. The size of each element is thus 7 bytes, or sizeof(char[7]). However, an often-recommended technique is to use the variable name with sizeof, not the type, because there's less of a chance of flubbing the size.

    So, if buffercodes is a pointer to char[7], then *buffercodes (which is the same as buffercodes[0]) is a char[7], and (sizeof *buffercodes) is the same as taking the size of a char[7].

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Code:
    char **mat = malloc(num * sizeof(char*));
    for (i = 0; i < num; i++) {
    	mat[i] = malloc(STRLEN);
    }
    Then for any multidimensional array we start allocating from right to left ? In this example, we fill collumns and then rows, so I can acces any of the strings using mat[row].

    Am I right?

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Quote Originally Posted by MK27 View Post
    Well, that looks better doesn't it? Just when I thot I knew everything
    Yes I prefeer the solution proposed by Salem and cas, but still you helped me remembering something about dynamic allocation


    I think I understood how to do it, I'll rearrange my code now

    Thanks everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocating some memory
    By supaman in forum C Programming
    Replies: 7
    Last Post: 03-09-2006, 10:34 PM
  2. allocating memory?
    By SamuraiDave in forum C Programming
    Replies: 2
    Last Post: 09-21-2005, 02:45 PM
  3. allocating memory
    By viaxd in forum C Programming
    Replies: 2
    Last Post: 10-12-2003, 07:13 PM
  4. Allocating memory...
    By PsychoBrat in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2003, 11:09 PM
  5. Replies: 5
    Last Post: 11-24-2002, 11:33 PM