How To Declare and Dynamically Size a Global 2D Array?

This is a discussion on How To Declare and Dynamically Size a Global 2D Array? within the C Programming forums, part of the General Programming Boards category; I think it's time to contact your professor . . . For now, you should probably malloc() an array of ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I think it's time to contact your professor . . .

    For now, you should probably malloc() an array of that size rather than declaring it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Okay, I see what he wants you to do now. Most likely, he wants you to just hardcode to page size for each platform when you recompile the program on that platform.

    >It is also too large to be dynamically created with a call to malloc().
    This is still incorrect.

    >The only remaining option is to make it global.
    This may work, if he's asking what I think he's asking. It's not guaranteed though, which makes the problem dicey at best. Just declare a global array (let's assume for the example that the page size is 4096) and hope that your implementation "correctly" handles the object code for it:
    Code:
    #include <stdio.h>
    
    char global[20480][4096];
    
    int main ( void )
    {
      return 0;
    }
    You would be much better off using malloc, but unless you post the code you're using, we can't help you troubleshoot the warnings you've been getting.
    My best code is written with the delete key.

  3. #18
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by groberts1980
    3 Creating the Matrix
    Once you have determined the size of a page, create a two-dimensional matrix where each element is a character (char) and the size of each row is exactly one page. Your matrix should contain 20480 rows. Youíll notice that the size of the array is large. In fact, it is too large to be placed on the stack. Therefore, it canít be declared locally inside of any function. It is also too large to be dynamically created with a call to malloc(). The only remaining option is to make it global. You may want to experiment with the other options to see the results.
    Correct me if I'm wrong. . . but can one not call malloc() asking for the size of memory and get it regardless of how much is already in memory (assuming that some type of swapping exists)?

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    malloc() is not guaranteed to return newly allocated memory, if that's what you're suggesting. Some systems might not have any free disk space for a swap file or virtual memory, or no swap file support altogether. Or a DOS program might not be allowed to allocate over 64/640KB.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >can one not call malloc() asking for the size of memory and get it regardless of how much is already in memory
    It depends. Any decent memory manager will eat into virtual memory at some point, and you can more or less allocate an "infinite" amount with malloc. However, requests for single large blocks are less likely to be honored than multiple small blocks because the manager still has to manage the fragments. Even if you have a terabyte of free virtual space, malloc may fail if you ask for a terabyte and the space is split in the middle by a single byte reserve.
    My best code is written with the delete key.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char ** argv)
    {
       int getpagesize(void);
       int columns;
    		
       columns = getpagesize();
    	
       char ** p = malloc (20480 * sizeof *p);
    	
       int i;
       for(i = 0; i < 20480; i++)
          p[i] = malloc (columns * sizeof p[i]);
    
       return(0);
    }
    I'm not getting compiler errors this time. I had a bunch of commented out code for later use in the program. Once I took that out, the compiler errors went away. So with the above code, I have a fully initialized array like this?
    Code:
    p[20480][4096]
    I may have misunderstood his requirements. I assumed he wanted us to call getpagesize() and set the value at runtime. Possible that he wants us to hard code the array size for the machine before compiling on that platform.
    Last edited by groberts1980; 11-14-2006 at 02:08 PM.

  7. #22
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    No, not suggesting that malloc() always returns a valid memory location. . . just saying that, for example, on Linux (with a large swap space, ie, 2X memory), one could request for most of memory and get it. Might have to run throug a loop to get the max, but still could get almost all of it.

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Which only supports the argument that you should be able to allocate enough memory.

    Your code uses C99-style mid-block variable declarations, which your compiler might not like. Nor do you free any of that memory that you allocate, which is a bad idea when you allocate so much. Other than that, your code looks good.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #24
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So with the above code, I have a fully initialized array like this?
    Functionally, yes. In reality, that's probably not what you want to test paging and virtual memory because the memory you've allocated probably isn't contiguous. To get it all clumped together in one big block, you would do something more like this:
    Code:
    char *p = malloc ( 20480 * 4096 );
    My best code is written with the delete key.

  10. #25
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I may have misunderstood his requirements. I assumed he wanted us to call getpagesize() and set the value at runtime. Possible that he wants us to hard code the array size for the machine before compiling on that platform.
    I think he wants you to run a separate program which calls getpagesize(). Then once you have the page size create a new program to create and access the global array:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    char [20480][4096];  //Number returned from getpagesize(), in this example 4096
    
    int main()
    {
        //Fill the array, etc
    }

  11. #26
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    I emailed him, this was his response:

    "The page size needs to be hardcoded. I'll explain why in lecture next time. For now, just write a 2-line program to print the value and plug that value into your VirtMem program."

    So, write a program to print the pagesize (already done), run that program on a given platform, hard code that value into my array program, re-compile on that platform, run program. Sound about right?

    He said we would run into problems using malloc to create the array, but I seem to have no problems on my machine. But I do have 1GB of ram and a 2GB swap file for linux. Is that why it runs fine on my system?

  12. #27
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >He said we would run into problems using malloc to create the array
    You can keep repeating what he said, but it doesn't make him any more right. The only time I would expect problems with malloc is on an embedded platform, or an ancient and gimped OS. However, it's generally a bad idea to tell your teacher he's wrong, even if he is. So just do what he wants and keep in mind that he might just be clueless.
    My best code is written with the delete key.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix multiplication help
    By Fredir in forum C++ Programming
    Replies: 16
    Last Post: 12-06-2007, 02:15 PM
  2. Pls help:: function initializing for 2d array
    By ypramesh in forum C Programming
    Replies: 3
    Last Post: 03-29-2006, 02:35 PM
  3. 2D Dynamically allocated pointer arrays
    By Lionmane in forum C Programming
    Replies: 37
    Last Post: 06-11-2005, 10:39 PM
  4. declaring array of strings dynamically!!!!
    By Real in forum C Programming
    Replies: 1
    Last Post: 08-29-2003, 06:59 AM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

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