Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ How to set stack/heap size?

stack

  • Please log in to reply
10 replies to this topic

#1 pancakethirsty

pancakethirsty

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 11 July 2011 - 04:43 AM

Hi, I've 2 questions for you guys :w00t:
1. I'd like to know if it's possible to set the memory limit for both heap and stack, that want in my program. I'm under Windows 7 and I'm using MINGW Developer Studio. I was wondering if I could use some sort of command line argument or maybe there's some special function I didn't manage to find.
2. I'm relatively new to C++, so I'm still experimenting. I noticed that including a header file into my source will include the other subheaders aswell. For example including iostream gives me access to both cstdio and cstdlib. The question is : does that affect in anyway the speed or the memory consumption of my program? What if I include all the headers there are? I heard it affects the executable file size, but I'm concerned about memory and run speed.
Thx :thumbup1:
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 July 2011 - 07:02 AM

Stack size will be dependent on the function calls and non-pointer variables you instantiate. Every pointer you declare will also use some memory. You can create a memory allocation class that can limit the amount of heap memory it will return, but you are adding additional overhead to allocate memory, which will negatively impact run speed.

In general, C++ assumes that there is unlimited memory available unless the run-time environment (Windows 7, in your case) indicates otherwise. It's also true that, under most circumstances, efforts to increase run speed will come at the cost of a decrease in efficiency somewhere else. A classic example from databases is using an index (which requires space in the database) to make queries execute faster. In addition, common query results are often cached into RAM (increased speed, but uses more memory).

You'll want to study algorithms and data structures to gain a better understanding of these concepts. For now, just try to learn the language and avoid doing things that are likely to be really dumb, like infinite loops that allocate memory for pointers but don't deallocate it.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 pancakethirsty

pancakethirsty

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 11 July 2011 - 07:56 AM

Thanks WingedPanther, but I already knew that. What I need is a way to check my memory usage and make sure it doesn't go over the limit I want. To do that manually, it's hard cause it depends on the data my program works with(especially if I dinamically allocate memory, I won't know for sure).. so I was wondering if there is another way to do it. I'm looking for something like this /F (Set Stack Size) (C++) , but that only works for visual I guess.
Also my second question is unrelated to the first one. I just want to know if including lots of headers affects performance in terms of both memory usage and run speed.
:)
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 July 2011 - 12:11 PM

I'd start by doing some research into gprof. Rather than trying to set an artificial limit, start by gaining information.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 pancakethirsty

pancakethirsty

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 11 July 2011 - 01:11 PM

Thanks :) I've looked a bit into gprof, it's pretty good but it only helps if I want to find out what is the execution speed of my programs and time related stuff. If only I could find something similar that does it with memory. I found valgrind but that tool is only available under linux.
  • 0

#6 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 11 July 2011 - 01:39 PM

Since you are using Mingw so gcc is the compiler, and the following command line should work

gcc -Wl,--stack=xxxxx -Wl,--heap=yyyyy

Following thread online-judge.uva.es • View topic - Set heap size and stack size in C++
refers to the above command as well as reference code to create stack dynamically and switching to it on run time using inline assembly.

For your second question regarding header files, first every header file is written using

#ifndef <HeaderName>
#endif
so that even it is included multiple times, originally a single copy is included into project. That copy as you correctly mentioned increases the size of executable because it is inserted (linked statically) while generating the exe. If you have a bigger exe obviously it takes more memory and time to load the exe into memory, but once loaded it doesn't make much of an impact on running time.

Edited by fayyazlodhi, 11 July 2011 - 01:41 PM.
missing phrase

  • -1
Today is the first day of the rest of my life

#7 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 11 July 2011 - 03:18 PM

@fayyazlodhi: You neglected to mention an important part of the Include Guard:
#if !defined(SOME_HEADER_NAME)
[b]#define SOME_HEADER_NAME 1[/b]

...

#endif // SOME_HEADER_NAME
You have to #define the use of that header, or else the guards are worthless since the header name will never be defined. :P
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 11 July 2011 - 10:52 PM

Yeah ZekeDragon i took that implicit. I guess i was mentioning the concept rather than precisely code, but yes for some body not familiar, the clear meaning is only conveyed when define is also written. Thanks for elaborating on that.
  • 0
Today is the first day of the rest of my life

#9 pancakethirsty

pancakethirsty

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 12 July 2011 - 02:38 AM

Thank you very much, fayyazlodhi ! You deserve a pancake :w00t:
That was exactly what I needed. I knew I should've start reading that 700 pages gcc manual haha (:lol: not really), there had to be a command line like that, I just couldn't find it.
About my second question, you're saying it takes time to load all that code into memory so in fact it affects performance a bit. On simpler programs as I mentioned, I include iostream just to use cin and cout, but that includes cstdlib aswell (and who knows many others) which gives me access to tons of other stuff I don't need.
What I want to know is if I should worry about that affecting performance or not, cause the extra time is only spent at the runtime once for loading it into memory and after that it runs at it's normal speed. I want to be as efficient as possible :rolleyes:
  • 0

#10 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 12 July 2011 - 03:08 PM

What I want to know is if I should worry about that affecting performance or not, cause the extra time is only spent at the runtime once for loading it into memory and after that it runs at it's normal speed. I want to be as efficient as possible :rolleyes:


If you've got cygwin you can use the ldd program on your MinGW application, it should return something like this:
C:\Program Files\Geany>ldd foo.exe
        ntdll.dll => /cygdrive/c/WINDOWS/system32/ntdll.dll (0x7c900000)
        kernel32.dll => /cygdrive/c/WINDOWS/system32/kernel32.dll (0x7c800000)
        msvcrt.dll => /cygdrive/c/WINDOWS/system32/msvcrt.dll (0x77c10000)
These are dependencies, meaning they are not compiled in to your application statically. Including iostream and the other barrage of headers will not create a noticeable size difference.

Further, these DLL files are loaded once (likely at system startup) and shared among processes, they occupy a single physical address and do not take up page space (such as virtual memory) so your application will not be hindered by loading all or more of the MS C++ runtime library.

If you are worried about the size of the executable, there are always compiler options such as -O2 or -O3 which take out most of the information required for debugging and similar, you can as well pass it through strip.exe provided by your compiler toolchain and further reduce the size of the executable by stripping function symbols.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#11 pancakethirsty

pancakethirsty

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 12 July 2011 - 09:30 PM

Further, these DLL files are loaded once (likely at system startup) and shared among processes, they occupy a single physical address and do not take up page space (such as virtual memory) so your application will not be hindered by loading all or more of the MS C++ runtime library.

If you are worried about the size of the executable, there are always compiler options such as -O2 or -O3 which take out most of the information required for debugging and similar, you can as well pass it through strip.exe provided by your compiler toolchain and further reduce the size of the executable by stripping function symbols.


That is what I wanted to know so I guess I should not worry about including headers I don't use sometimes, since the performance of my application won't be affected.

Thanks once again for those who replyed, wish I could give a pancake :D for every one of you !
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download