Forum: ARM programming with GCC/GNU tools malloc, strncpy on STM32F4 not working

von Marcel F. (kellertuer)

Rate this post
0 useful
not useful
Hi, I am trying to get strncpy working. At the moment I know that it has 
something to do with the newlib. I am using coocox with 

How do I include the newlib into my toolchain? Or am I wrong with newlib 
an the problem lies somewhere else?

I included the syscalls from the repository: "The component implement 
the minimal functionality required to allow libc to link." I also 
included libc in the "linked libraries section". But it seems as if 
behind strncpy is only a declaration: "char   *_EXFUN(strncpy,(char *, 
const char *, size_t));"

I do not have any compile errors, but the strncpy is not working.

von Marcel F. (kellertuer)

Attached files:

Rate this post
0 useful
not useful
OK, I was searching a little bit for the reason. I am actually 
asm-debugging the code. It seem that the strncpy is linked correct, the 
program jumps to a routine called strncpy. Also the operations like copy 
first address of sourcestring to r2, copy first address of destination 
address to.. and so on. But the line is working: strb.w r5, [r4], #1
At this line the value 0x36 ("6") should be copied to the new address 
(0x08003984), right? But nothing is happening, the old value "y" is 
still there! The disassembly is at the picture.

Whats wrong?

von Marcel F. (kellertuer)

Rate this post
0 useful
not useful
So, I found the solution:

If I declare
char to[6];
 then the var will be somewhere in RAM and the value can be changed.
I declare
char to = "yyyyyy";
 then the var will be somewhere in Flash and the value can not be 

Whats the reason for this and how can I declare on which place in memory 
I want my vars?

von Stefan Rand (Guest)

Rate this post
-1 useful
not useful
Marcel F. wrote:
> Whats the reason for this

Because the standard demands that a string literal will not be changed. 
And the compiler saves precious RAM depending on that stipulation.

A good reason to learn C, wouldn't you think?

> and how can I declare on which place in memory I want my vars?

Read your compiler's documentation. There is probably some attribute or 
pragma to put it in whatever type of memory you want.

von Marcel F. (kellertuer)

Rate this post
-1 useful
not useful
thanks, I already got the solution. The solution is:

If I declare
char *to = "yyyyyy";
, then it is a constant value, but if I declare like
char to[7] = "yyyyyy";
 then it is not a constant.

This information helped me in another forum. Your "help" was no help. If 
you are telling everyone with a question that he should learn more or 
read a book, what kind of help is this? Better save your time :)

von Stefan Rand (Guest)

Rate this post
0 useful
not useful
You learned from my answer that the behaviour is correct and to be 
expected. You didn't know that and were confused. So this point alone 
was real help.

Additionally I directed you to the best place to find the answer to your 
explicit question.

Since you don't seem to be intellectually able to understand answers, 
and furthermore you have some really nasty sense of entitlement, please 
don't post here again. You're simply not welcome.

von Tobias (Guest)

Rate this post
0 useful
not useful
char to="asdf";

is just wrong, since you're trying to assing an array field to one 
single element.

char to[]="asdf";
char to[5]="asdf";

is correct.
first one as mentioned is only a single element. The compiler should 
recognize that this is not correct, maybe add "-Wall" to your compile 
command to show up a warning message.

von Marcel F. (kellertuer)

Rate this post
0 useful
not useful
Yes, Tobias, in my third post i forgot the "*" :) sorry for that


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.