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

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

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

    I need to use a 2D array of chars. The first field (rows) is set at 20480. The second field (columns) needs to be set at runtime (4096 being one possible value). The size of the 2D array will be too big to fit on the stack, so it needs to be global, putting it on the heap. It is also too large to be created dynamically with a call to malloc(). I can't seem to find the syntax I need to declare a global array, then set the size of it at runtime.

    Once it is created, I will then need to set every element in the array, then come back and access every element in the array.

    Also, would something like this work for accessing the array?
    Code:
    /* set each value */
    int i, j;
    for(i = 0; i < 20480; i++)
    {
       for(j = 0; j < columns; j++)
       {	
          array[i][j] = 'a';	
       }
    {
    
    /* access each value */
    char a;
    for(i = 0; i < 20480; i++)
    {
       for(j = 0; j < columns; j++)
       {	
          a = array[i][j];	
       }
    {

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The first field (rows) is set at 20480. The second field (columns) needs to be
    >set at runtime (4096 being one possible value).
    That's pretty big. Do you really need to store it all at once?

    >It is also too large to be created dynamically with a call to malloc().
    Not really, unless your system is fickle:
    Code:
    char **p = malloc ( 20480 * sizeof *p );
    
    for ( i = 0; i < 20480; i++ )
      p[i] = malloc ( columns * sizeof *p[i] );
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    I'm afraid I do need to store it all at once. The program is for an Operating Systems course, and the current subject matter is virtual memory. We are supposed to create this 2D array, set each value and access each value in column major order, then row major order, timing each one to see how long it takes.

    The column field comes from the page size of whatever platform the program is running on with a call to getpagesize().

    When I wrote it like your example, I get two warnings:
    implicit decleration of function 'malloc'
    incompatible implicit decleration of built-in function 'malloc"

    In the specifications for the project, the prof stated that "it is too large to be dynamically created with a call to malloc()." And that, "it is too large to be placed on the stack." I take that to mean he doesn't want us to create it using malloc(), or try placing it on the stack.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    #include <stdlib.h>

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    Quote Originally Posted by Kennedy
    #include <stdlib.h>
    Okay, I typed that into my program. Doesn't seem to help :-)

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >implicit decleration of function 'malloc'
    >incompatible implicit decleration of built-in function 'malloc"
    Please post the actual messages (as I doubt your compiler is misspelling declaration), and any relevant code that would help us tell you what you're doing wrong. Ideally, you should write a small test program that exhibits the error and won't take up too much space in your post.

    >the prof stated that "it is too large to be dynamically created with a call to malloc()."
    Well, he's wrong.

    >And that, "it is too large to be placed on the stack."
    That's probably true.

    >I take that to mean he doesn't want us to create it using malloc(), or try placing it on the stack.
    Then you can either store it in a file, or deem the problem impossible.

    >Doesn't seem to help :-)
    Neither does your description of the problem. :-)
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    Can I not just declare the array as global? Can we not just talk about the syntax to do that? Its a simple question. I'm not using malloc(), nor am I storing the array in a file. Saying the professor is wrong does not help the situation, nor does it help my progress with this problem.

    Global decleration. Number of rows: 20480. Number of columns: decided at run-time.

    Seem simple enough?

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    >Global decleration. Number of rows: 20480. Number of columns: decided at run-time.

    Cannot be done without the use of malloc().

    >Seem simple enough?
    Yes, use malloc() and a pointer.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i dont know the answer but whether the variable is global or local to a function does that deteremine the maximum size it can be?

    another thing to ask your teacher is why does this variable NEED to be global? global variables should be kept to a minimum, so maybe he is wrong with things prelude has mentioned.

    when you create an array with the size decided at run time, it IS a dynamic array and the only way to create that (i thought) is with malloc. also, as previously mentioned i think the limit on malloc is deteremined by the amount of memory available and nothing else.

    is there an outline for the problem provided by the professor? can you post that exactly as is?

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by nadroj
    when you create an array with the size decided at run time, it IS a dynamic array and the only way to create that (i thought) is with malloc.
    I think you can do dynamic arrays with C99 (without calling malloc).

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    in your previous post didnt you say the same thing?
    "Cannot be done without the use of malloc()."

  12. #12
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by nadroj
    in your previous post didnt you say the same thing?
    "Cannot be done without the use of malloc()."
    Unfortunenately this happens a lot to me. . . that was an afterthought. . . I work with C89 for the most part (my GCCs all allow // for comments, though). I don't do much of the dynamic arrays (other than calling malloc).

    Sorry about that.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    another thing to ask your teacher is why does this variable NEED to be global? global variables should be kept to a minimum, so maybe he is wrong with things prelude has mentioned.
    If a variable is global, it doesn't go on the stack, but rather on the heap. If an array is too large to be declared on the stack, it might fit in the heap.

    Of course, if you just want a variable on the heap, you don't need to use a global variable; static variables are also stored on the heap.
    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.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can I not just declare the array as global?
    No, that has no effect on the size limitations of an array.

    >Its a simple question.
    The simple questions are the ones that tend to have the most complicated answers.

    >I'm not using malloc(), nor am I storing the array in a file.
    Then you can't do it.

    >Saying the professor is wrong does not help the situation, nor does it help my progress with this problem.
    Quite the contrary. Saying the professor is wrong means that you can use malloc to do what you want. I gave you your answer; you simply failed to provide enough information for it to be a complete answer. Now if you want to whine and complain that you're not getting exactly what you want, I'll be happy to lock the thread. Or you can give us the information we ask for and we'll work toward a solution in a nice civil manner. Naturally, I would prefer the latter as we avoid flames and you get the help you need.

    >Seem simple enough?
    It's perfectly simple, but you keep telling me that it can't be done even though I'm know that it can be done both the way I've described and in numerous variations. So unless you're holding some critical piece of information from us that suddenly invalidates everything I've said, I'm right and you're just making things complicated.

    >If a variable is global, it doesn't go on the stack, but rather on the heap.
    Be careful with your terminology. When people talk about the heap, they mean the dynamic store specifically used by malloc. Global variables are not stored there. Static storage isn't the same thing as the heap, can't be determined at run-time, and most certainly isn't a desirable place for your "too large" objects.

    >If an array is too large to be declared on the stack, it might fit in the heap.
    However, a global variable is bound to the same array size restrictions as a local variable. To get the increased potential size from the heap, you need to use malloc or something equivalent.
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    Here is the revelent part of the assignment. This was copied and pasted directly from a PDF file that we were given. I don't know what else to say. This is what the professor gave us. Keep in mind this is a relatively small university, not Harvard or MIT. Our professors aren't exactly the smartest computer scientists out there. All I can do is complete the assignment to his specifications.

    When I get back to my Linux machine I'll post the code I wrote, along with the compiler errors. I typed them while looking at the CLI window, but I'll copy & paste this time.

    1 Overview
    This assignment will explore the use of large arrays and the effects of virtual memory. You will write a single program and execute it on several different platforms. The program will compare the time requiredto read and write element of large arrays in both row-major and column-major order.

    2 Page Size
    The first part of this assignment is to determine the size of a page on the machine you are using. Each architecture may have different page sizes. A simple, one line program can be written to print the page size of the current machine. I suggest you use your man page skills to find a way to write such a program.

    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.

    4 Accessing the Elements
    You are to access the elements of the array four different times. The first access will place the character ’a’ in every element. The elements are to be accessed in row-major order. The second access will be to read the value of each element into a temporary variable in row-major order. The write and read tests are to be repeated using column-major order. Write the character ’b’ to every element, and then read the value of each element into a temporary variable.
    Last edited by groberts1980; 11-14-2006 at 02:49 PM.

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, 03:15 PM
  2. Pls help:: function initializing for 2d array
    By ypramesh in forum C Programming
    Replies: 3
    Last Post: 03-29-2006, 03: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