Static Memory allocation

This is a discussion on Static Memory allocation within the C Programming forums, part of the General Programming Boards category; Originally Posted by kermit I am not quite following you here. Where do you get the 1MB from? (Not trying ...

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Quote Originally Posted by kermit View Post
    I am not quite following you here. Where do you get the 1MB from? (Not trying to nitpick, but rather trying to learn )
    Because a typical stack size is 1 MB. It's the default under Windows.
    Not to say it is limited to one MB. It can be larger (or smaller), but I just did not want to type that out.

    Quote Originally Posted by itCbitC View Post
    Considering that a double is 8 bytes on most machines, the storage requirements of those arrays would be "13824*3*3*3*2 + 576*12*2*1*9*9*2 + 576*2" times 8 bytes which is about 6Mb plus the size of the arrays defined in the other subroutines.
    Which would probably be too much for the stack.
    Yes. This is probably the main cause for the problems.

    Physical memory on your machine is about 3 Gb and swap is about 2 Gb; so total virtual memory is 5 Gb not 0.5 Gb; not sure how you get 0.5 Gb.
    There is one computer (laptop) with 3 GB of memory, and one machine (university) with 0.5 GB of memory.
    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.

  2. #17
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    Hey yesterday the message board didn't work...but thank to your advices I got the solution of my problem.
    The fact is that since I started to program in c nobody told me that I could declare global variables... now I declare the arrays outside the main and I got all the memory I need. Thank you very much...bye

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Quote Originally Posted by p3rry View Post
    The fact is that since I started to program in c nobody told me that I could declare global variables... now I declare the arrays outside the main and I got all the memory I need. Thank you very much...bye
    You should not do that!
    Use dynamic memory instead!
    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. #19
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    Why? I heard that dynamic allocation slows down the program and I need much statistic, so I'd prefer a faster simulation.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can make dynamic allocation very slow, if you want to. If you want it fast, then don't do that. (I.e., calling malloc once in the program should not slow your program down to any appreciable degree. Calling malloc once for every element in your array will make your program very slow indeed.)

  6. #21
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Why? I heard that dynamic allocation slows down the program and I need much statistic, so I'd prefer a faster simulation.
    And using global memory for such a thing is stupid. Take your pick, "slow" or stupid?

    Well depending on how you do it, there may be no slow down or close to none (~0%) [a].

    [a] For example grow in chunks of 1 MB as you require memory -- or 10MB
    Last edited by zacs7; 12-22-2008 at 06:33 AM.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Quote Originally Posted by p3rry View Post
    Why? I heard that dynamic allocation slows down the program and I need much statistic, so I'd prefer a faster simulation.
    That is a myth. Regardless if dynamic or static, the OS still needs to allocate it, and it usually goes to the same system call in the end anyway.
    Dynamic memory can be slow if you use is incorrectly. But if you simply need one big block of memory, it should be equal in speed.
    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.

  8. #23
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    i.e. most people who care about performance will operate on their assumptions, which is worse than actually measuring, or in fact, not caring at all.

    Memory allocation is not instantaneous, true, but it is not relevant to user time. Ever.

  9. #24
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Elysia View Post
    That is a myth. Regardless if dynamic or static, the OS still needs to allocate it, and it usually goes to the same system call in the end anyway.
    Dynamic memory can be slow if you use is incorrectly. But if you simply need one big block of memory, it should be equal in speed.
    Well said.
    The "dynamic memory allocation can slow the program" refers to situations that you have no other choice. For example:

    for(alot){
    do stuf
    re-allocate memory
    }

    The reallocation may slow a lot the program compared to "do stuff". But there is no other way to achieve this. So you are warned that allocation isn't free.

    If you just allocate in the beginning of your program memory then it shouldn't really matter (a lot) where you allocate memory. If the program runs main and allocates memory, or pre-allocates memory for globals and afterward calls main should make small difference

  10. #25
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    Ok... but there's any problem in using global arrays?
    (Sorry but my basic knowledge is very poor)

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    Quote Originally Posted by p3rry
    Ok... but there's any problem in using global arrays?
    There would be the problems associated with global variables in general (we have discussed this in recents threads, so you might want to do a search).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

Similar Threads

  1. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 07:45 PM
  2. Replies: 1
    Last Post: 03-30-2004, 02:57 PM
  3. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  4. static memory and dynamic memory
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 08:46 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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