basic question about memory management

This is a discussion on basic question about memory management within the C Programming forums, part of the General Programming Boards category; Hi, I have a simple quick question: when i allocate memory for my array let say: int a[50]; and then ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    172

    basic question about memory management

    Hi,

    I have a simple quick question:

    when i allocate memory for my array let say:


    int a[50];

    and then only initialize the 35 element in the array:


    a[34] = 5;

    how much memory will I use. Will all alocated memory be unavailable any more for any other process or just memory location a[34]?

    cheers

    baxy

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    There's no simple easy answer - it depends on how you allocate memory, what OS you're using, and so on.

    Why do you need to know?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > int a[50];
    50 * sizeof(int) gets allocated immediately, and persists until the scope you declared it in disappears.

    Your use of individual elements doesn't matter.


    Now if you have something like
    int large[1000000];
    and you assign
    large[1000] = 42;
    it MAY be the case that large blocks of the array are not located in physical memory all of the time. This is entirely down to the paging strategy of your OS and is outside the scope of your C program.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    172
    aha...

    so I'm talking about unix OS and i wondering if i for example have this 2D table (array) and i fill just several blocks, can i say that u used only 3 memory locations if onla 3 blocks were filled even though the table is, let say 10000 x 10000 mem locations big

    and the reaso why i'm asking is because as i said i have a big table but it is not possible to fill all the positions but only 10000+10000 although the table needs to be 10000x10000. so i'm wondering how much memory am i occupying:

    10000+10000

    or

    10000x10000
    Last edited by baxy; 11-03-2011 at 09:50 AM.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    Typically the OS will allocate memory a page at a time. A page is typically 4KB, or thereabouts. Until you write to memory, some OSes will just mark the memory as allocated to a process but not actually match it up with physical memory (or swap, or whatever). If you read from it, you'll get garbage since it doesn't map to anything real, but that's OK since reading from unitialized memory isn't well-defined anyway.

    Once you write to it, the OS will actually map the page to physical memory. So if you only use a small part of a large array, you're probably not going to use up all the memory. Don't initialize the whole things to 0 first, though, since that will map it to physical locations even if you never use it again (the OS can't see the future or read your mind to know you're never going to see it again).

    But this all depends on which specific Unix OS you're talking about. Linux works as I described it, more or less, so you'll get 10000+10000 instead of 10000x10000 (rounded up to page size boundaries). Others may vary.
    Last edited by KCfromNC; 11-03-2011 at 10:02 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Perhaps you should implement your own sparse matrix, along one of these themes.
    Sparse matrix - Wikipedia, the free encyclopedia

    You might only be writing to 1% of the matrix, but if you try and read the remaining 99% (to see if it is zero), then the pages will become mapped and all of a sudden, your memory footprint jumps up to 0.5GB.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by baxy View Post
    aha...

    so I'm talking about unix OS and i wondering if i for example have this 2D table (array) and i fill just several blocks, can i say that u used only 3 memory locations if onla 3 blocks were filled even though the table is, let say 10000 x 10000 mem locations big

    and the reaso why i'm asking is because as i said i have a big table but it is not possible to fill all the positions but only 10000+10000 although the table needs to be 10000x10000. so i'm wondering how much memory am i occupying:

    10000+10000

    or

    10000x10000
    Fill in the details about how this array is being declared: outside of any function or inside a function.

    If declared inside a function, its runtime memory allocation will be 10000 x 10000 x sizeof(int) as stack frames are not allocated on a piecemeal basis. It's all or nothing.

    Albeit, if it's declared outside any function but some elements are assigned to inside a function its runtime memory allocation depends on how many pages of physmem are allocated to it by the kernel.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by baxy View Post
    and then only initialize the 35 element in the array:

    a[34] = 5;
    That's not initialization. That's assignment.
    Initialization looks like this:
    Code:
    int a[5] = {1,4,9,16,25};
    In fact this isn't about memory management either. Your program's code doesn't actually manage the memory here; It's all taken care of for you. This is simply a matter of memory usage.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by baxy View Post
    Hi,

    I have a simple quick question:

    when i allocate memory for my array let say:


    int a[50];

    and then only initialize the 35 element in the array:


    a[34] = 5;

    how much memory will I use. Will all alocated memory be unavailable any more for any other process or just memory location a[34]?

    cheers

    baxy
    The simple answer is Yes, the remainder of the array remains available until it goes out of scope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic memory question
    By Subsonics in forum C++ Programming
    Replies: 13
    Last Post: 09-14-2010, 04:40 PM
  2. memory management
    By hansigarten2 in forum C Programming
    Replies: 10
    Last Post: 08-19-2010, 01:22 PM
  3. Memory management
    By tempster09 in forum C Programming
    Replies: 2
    Last Post: 12-26-2009, 12:11 PM
  4. Replies: 2
    Last Post: 08-14-2009, 07:15 AM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM

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