Malloc() help?

This is a discussion on Malloc() help? within the C Programming forums, part of the General Programming Boards category; I have to read in a huge array of numbers from a file as if it were stdin. The first ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Malloc() help?

    I have to read in a huge array of numbers from a file as if it were stdin.

    The first two numbers in the file are the array dimensions. Then the numbers in the array are below.

    Like this:
    244 456
    33 56 43 7 97 336 56
    323 789 64 569 4
    ...
    and so on.

    I am able to read in the numbers and print them out just fine.
    I am also able to print out the array horizontally and vertically with this code:

    Horizontal:
    Code:
    array[x][y];
        for (i=0; i<y; i++){
            for(j=x-1; j>=0; j--){
                printf("%d\n", array[j][i]);
            }
    
            j=x-1;
        }
    Vertical:
    Code:
        for (i=y-1; i>=0; i--){
            for(j=0; j<x; j++){
                printf("%d\n", array[j][i]);
            }
    
            j=0;
        }

    However, this is for school and they want me to do it another way.

    For the horizontal part, they want us to malloc() a row's worth of pixels.
    Im guessing is this how you do it?
    Code:
     
      int **row;
      row = (int **) malloc(sizeof(int *)*x);
    Then I probably read in each number of that row, and immediately print it out backwards in the same loop right? I dont know how to even read in something from malloc so .. can someone please help?


    For the vertical part, I need to allocate an array of row (int *)'s. Then initialize each one of those (int *)'s by calling malloc() to allocate an array of column ints. Read each row of pixels into one of these arrays, and then print them out backwards.
    Is this done like this?
    Code:
      int **col;
      col = (int **) malloc(sizeof(int *)*x);
      for (i = 0; i < x; i++) {
        col[i] = (int *) malloc(sizeof(int)*y);
      }
    If not can someone correct me please.. Im really lost when it comes to malloc.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Aside from casting the result of malloc, see the FAQ, it looks good.

    Everything else in the code (like your array usage) should be the same as it is now.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you need to store let's say 400 ints, then you would need to acquire enough memory to store 400 ints:
    Code:
    int *holder_of_ints = malloc(400 * sizeof(int))
    Now you can access the memory as though holder_of_ints was an array, by using [index] notation.

    If you need to store let's say a 300 x 400 grid, you would need 300 of the above, so:
    Code:
    int **holder_of_2d_ints = malloc(300 * sizeof(int *));
    for (int i = 0; i < 300; i++)
        holder_of_2d_ints[i] = malloc(400 * sizeof(int)); /* just like above */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21