EmbeddedRelated.com
Forums
The 2026 Embedded Online Conference

elements of array not accessible within loop

Started by Oliver Krause November 18, 2015
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 http://www.EmbeddedRelated.com
On 18.11.15 15:41, Oliver Krause wrote:
> 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
The classical cause is that you'll clobber your stack. Which microcontroller, which compiler and how does the linker map look like? -- -TV
On 11/18/2015 8:41 AM, Oliver Krause wrote:
> 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?
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
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 http://www.EmbeddedRelated.com
On 11/19/2015 5:24 AM, Oliver Krause wrote:
> 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.
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
The 2026 Embedded Online Conference