Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
19 years ago

Problem about heap & stack memory

Hi, everyone

I downloaded the full_featured_1s10 project from the examples of altera development kits to my stratix board without much modification in SOPC builder.

I have a project I am working on and I found out that if I allocate my program data memory somewhere else that is closer to the CPU rather than the far-far away SDRAM, the perforamcne of my program can be improved.

Before I actually allocate my data memory, I was trying to use the on-chip memory and tightly-coupled memory for heap and stack memory just to test out. In the system library properties, I first tried to set the memory allocation for my heap and stack memory to on-chip 64K bytes memory. The project compiles but the program won't run properly and gmon.out wouldn't come out either.

I found out from the software handbook that programmer has to ensure that

their programs operates within the limits of available memory by themselves.

But I don't know where and how to check if there is enough space for the heap & stack memory of my project.

Anyone had this problem before?

7 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Several things to suggest here:

    1. Use the "Run time stack checking" option, available on the System Library Properties page in the IDE. "Debug..." (don't "Run...") your code after setting this option.

    - This should give an idea of how much space your code requires, during runtime.

    2. gmon.out will not be produced unless your code successfully exits (by calling exit()).

    - If your code doesn't exit (most does not), you've got to change it such that it does.

    - Changing your code so that it returns from main() should be enough.

    Cheers,

    - slacker
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks for your reply, slacker http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif

    As I don&#39;t have very clear concept as to what kind of stuff are stored in heap &stack memory. I didn&#39;t know where to look for to get an idea how much I might need to allocate for them. is it the registers that I should be looking at??

    When i was compiling my c files, the following info was printed to the console in NIOS II IDE, does this suggest the size that are estimated to be needed for heap and stack at the compilation time?

    Info: (four_regs_write.elf) 175 KBytes program size (code + initialized data).

    Info: 16208 KBytes free for stack + heap.

    many thanks

    Tony
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    originally posted by ikuo005@Jul 17 2006, 07:54 PM

    thanks for your reply, slacker http://forum.niosforum.com/work2/style_emoticons/<#emo_dir#>/smile.gif

    as i don&#39;t have very clear concept as to what kind of stuff are stored in heap &stack memory. i didn&#39;t know where to look for to get an idea how much i might need to allocate for them. is it the registers that i should be looking at??

    --- Quote End ---

    This is something that must be determined at run time. You cannot determine it statically...

    <div class='quotetop'>QUOTE </div>

    --- Quote Start ---

    When i was compiling my c files, the following info was printed to the console in NIOS II IDE, does this suggest the size that are estimated to be needed for heap and stack at the compilation time?

    Info: (four_regs_write.elf) 175 KBytes program size (code + initialized data).

    Info: 16208 KBytes free for stack + heap.[/b]

    --- Quote End ---

    This is telling you how much free space you have for the stack and the heap, combined. The heap starts from the bottom and increments and the stack starts from the top and decrements. Eventually, depending on how your code runs, they can collide.

    You can reduce your code&#39;s memory footprint in several ways. These methods are mentioned, inline, in several of the software example templates (hello world small comes to mind, though I think hello led has something as well)

    Cheers,

    - slacker
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi Slacker,

    I didn&#39;t make myself clear, I meant i tried using the Debugger with the Run time stack checking" option on. But I was not sure where to look at for stack and heap memory in the Debug interface in NIOS II IDE. Where should I look into in the debug interface to look at the current stack memory and heap memory while I am debugging.

    Thanks

    Tony

    --- Quote Start ---

    originally posted by slacker+jul 18 2006, 06:12 pm--><div class='quotetop'>quote (slacker @ jul 18 2006, 06:12 pm)</div>

    --- quote start ---

    <!--quotebegin-ikuo005@Jul 17 2006, 07:54 PM

    thanks for your reply, slacker http://forum.niosforum.com/work2/style_emoticons/<#emo_dir#>/smile.gif

    as i don&#39;t have very clear concept as to what kind of stuff are stored in heap &stack memory. i didn&#39;t know where to look for to get an idea how much i might need to allocate for them. is it the registers that i should be looking at??

    --- Quote End ---

    This is something that must be determined at run time. You cannot determine it statically...

    <div class='quotetop'>QUOTE </div>

    --- Quote Start ---

    When i was compiling my c files, the following info was printed to the console in NIOS II IDE, does this suggest the size that are estimated to be needed for heap and stack at the compilation time?

    Info: (four_regs_write.elf) 175 KBytes program size (code + initialized data).

    Info: 16208 KBytes free for stack + heap.[/b]

    --- Quote End ---

    This is telling you how much free space you have for the stack and the heap, combined. The heap starts from the bottom and increments and the stack starts from the top and decrements. Eventually, depending on how your code runs, they can collide.

    You can reduce your code&#39;s memory footprint in several ways. These methods are mentioned, inline, in several of the software example templates (hello world small comes to mind, though I think hello led has something as well)

    Cheers,

    - slacker

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=16974)</div>

    [/b]

    --- Quote End ---

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    originally posted by ikuo005@Jul 19 2006, 09:28 PM

    hi slacker,

    i didn&#39;t make myself clear, i meant i tried using the debugger with the run time stack checking" option on. but i was not sure where to look at for stack and heap memory in the debug interface in nios ii ide. where should i look into in the debug interface to look at the current stack memory and heap memory while i am debugging.

    thanks

    tony

    --- Quote End ---

    It doesn&#39;t work that way. The "Run time stack checking" just inserts a break statement in such a way that a stack overflow will trigger it.

    A while back, however, there was a pretty decent analysis (http://www.embedded.com/showarticle.jhtml?articleid=47101892) of this problem in ESD. The article refers to a Perl script (http://www.cs.utah.edu/~regehr/stacktool/) that might be able to help you out, as well. I know this isn&#39;t for the Nios II processor, but it gives you an idea that this is not a trivial problem...

    Best of luck....that&#39;s really the limit of my knowledge on the subject.

    Cheers,

    - slacker
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    <div class='quotetop'>QUOTE </div>

    --- Quote Start ---

    Where should I look into in the debug interface to look at the current stack memory and heap memory while I am debugging.[/b]

    --- Quote End ---

    Hi,

    NiosII have SP&FP registers to record that where the heap&stack pointer point to. When debugging, watch these two registers then you&#39;ll get what you want.http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/happy.gif

    David