Set an array with unknown dimensions as global

This is a discussion on Set an array with unknown dimensions as global within the C Programming forums, part of the General Programming Boards category; Hi! In my code I use a 2d array which I need in more than two subroutines. This array doesn't ...

  1. #1
    Registered User paok's Avatar
    Join Date
    Nov 2007
    Posts
    8

    Set an array with unknown dimensions as global

    Hi! In my code I use a 2d array which I need in more than two subroutines. This array doesn't have specific dimensions. The user gives them. Is there any way to declare it so it wouldn't be changed every time I need it? I've tried to declare it as global but I couldn't.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't CHANGE the dimensions of an array in C (or in C++ for that matter).

    You CAN use dynamic allocation to build a 2D array - there will be plenty of examples of that.

    Or, if you prefer that method, you can GUESS a "huge" size and just use whatever portion of it that the user wishes to use, e.g. make it 10000 x 10000 (400MB), but only use 1% of it if the user asks for 100 x 100. This of course assumes the code isn't supposed to run on a system with limited memory space.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Come on, matsp, 400 MB? People complain at me for bloat
    A iaximum of 1000 x 1000 I can recommend, but preferably smaller.
    Perferbly a dynamic allocated array with just enough necessary space.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User paok's Avatar
    Join Date
    Nov 2007
    Posts
    8
    Ok! Thank you!!!! I'll do it using dynamic allocated array!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Come on, matsp, 400 MB? People complain at me for bloat
    A iaximum of 1000 x 1000 I can recommend, but preferably smaller.
    Perferbly a dynamic allocated array with just enough necessary space.
    But in any decent OS, the array-space will only be committed to memory if it's ACTUALLY used, so a 100 x 100 usage of a 10000 x 10000 array will only use physical memory for a small amount of the 400MB [roughly 100/10000 -> 1%].

    Yes, it's excessive, but it's definitely a simpler solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    You may be right, but I don't know if we can rely on this always happening. As you say, a good OS won't mind, but a bad one might.
    Well, I think dynamic arrays can be good practice anyway.

    This works to consume seemingly little memory, but I can't access the entire array either. Somewhere around the middle, it gives an access violation.
    But you allocate it in one big, single array, it consumes 400 MB of memory.
    Last edited by Elysia; 12-07-2007 at 12:37 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    You may be right, but I don't know if we can rely on this always happening. As you say, a good OS won't mind, but a bad one might.
    Well, I think dynamic arrays can be good practice anyway.

    Code:
    	int** pNewInt = new int*[10000];
    	for (int i = 0; i < 10000; pNewInt[i++] = new int);
    This works to consume seemingly little memory, but I can't access the entire array either. Somewhere around the middle, it gives an access violation.
    But you allocate it in one big, single array, it consumes 400 MB of memory.
    But you are only allocating one int for each row...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by matsp View Post
    But you are only allocating one int for each row...

    --
    Mats
    As you can see, I'm not very experienced with 2D-arrays

    Code:
    	int** pNewInt = malloc(sizeof(int*) * 10000);
    	for (int i = 0; i < 10000; pNewInt[i++] = malloc(sizeof(int) * 10000));
    	for (int i = 0; i < 10000; i++)
    	{
    		for (int j = 0; j < 10000; pNewInt[i][j++] = 0);
    	}
    OK, so here's the new test.
    Results: 400 MB physical memory gobbled up, memory use by app +400 MB and swap file grew by 400 MB too.
    Took some time to complete on the first run too, but not the second or third. It may just be due to the debugger.

    So in conclusion: it's bad to allocate such a huge array.
    Last edited by Elysia; 12-07-2007 at 12:37 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Isn't it a C forum?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Point to be demonstrated. Do you rather I rewrite it with malloc?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Quote Originally Posted by Elysia View Post
    Point to be demonstrated. Do you rather I rewrite it with malloc?
    I'd rather to point you to post the correct code on the correct forum in the future. There are people here that can be misleaded by such demonstrations.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Code updated to C code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 02:44 AM
  3. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

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