Thread: Static Memory allocation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by kermit View Post
    I was just wondering if I had missed something pertinent in one of the posts. As noted, the stack size varies, but whatever the case is not suitable for really large static arrays.
    An array on the stack is by definition not "static."

    I suppose it does not matter much to the OP -- it looks like he is going to have to go ahead and use dynamic memory allocation
    I agree -- what's so hard about calling malloc()?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #2
    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. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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. #4
    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. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  7. #7
    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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  9. #9
    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.

  10. #10
    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. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 06: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, 04:10 PM

Tags for this Thread