elements of array not accessible within loop

Hi there

Im pretty new to embedded programming and so I stumbled upon a problem. I do:

int i; char foo[2048]; char bar[3000]; for (i = 0; i < sizeof(foo); i++) foo[i] = bar[i];

The code seems ok and the compiler doesnt complain, but when I load that program to the microcontroller it hangs up. I noticed that when I decrease the size of foo to about 900 it all works fine. So that looked like a RAM problem to me. But Im able to access the last element of foo without having any trouble (foo[2047] = bar[2047]) which shouldnt work if foo where partly located out of RAM, right?

So why isnt the code working? What am I doing wrong?

Im using a LPC2478 from NXP.

Could anybody please provide some help for me?

Sincerely, Oli

--------------------------------------- Posted through

formatting link

Reply to
Oliver Krause
Loading thread data ...

The classical cause is that you'll clobber your stack.

Which microcontroller, which compiler and how does the linker map look like?

--

-TV
Reply to
Tauno Voipio

You might be overwriting your stack. You can look at the linker/mapper output to see where in memory things are being placed. Often the stack is at the top of RAM and grows downward. When your data is written it is likely overwriting the top of the stack.

--

Rick
Reply to
rickman

You guys where right. It was a problem with the stack. Now I write the critical array into heap, via calloc(). I also had to look into my startup file and set the size of the heap (standard size is 0). But even then it didnt work, so I increased the size of the stack, too. Now it runs fine.

Therefor: Great thanks!

Since this code is part of my thesis, my next task is to really understand whats all behind this and to do an accurate calculation about how much memory space is actually needed.

Sincerely, Oli

--------------------------------------- Posted through

formatting link

Reply to
Oliver Krause

On 2015-11-19 Oliver Krause wrote in comp.arch.embedded:

Good you found it. In embedded systems, dynamic memory allocation is often not desirable. In this case you could allocate the variable as a (module) global variable, or as a static in the function.

This avoids memory fragmentation and the size requirements can be checked compile time instead of run time (do you handle any arrors from your calloc() call?).

Downside is the memory is always "in use" and can not be re-used elsewhere (unless you write your code to explicitly do so).

--
Stef    (remove caps, dashes and .invalid from e-mail address to reply by mail) 

Tip the world over on its side and everything loose will land in Los Angeles. 
 Click to see the full signature
Reply to
Stef

You might want to consider using the Forth language. Rather than the tool allocating memory for you and having to dig out what it was that the tool did, you can allocate memory yourself and know exactly what is happening. I find it much easier to using in embedded applications than conventional tools like C.

--

Rick
Reply to
rickman

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.