Hi!!!
I using Maple Rev 5, with IDE v0.0.12, I am new in C and Maple.
I try the code using char[] returning from a function but get nothing or trash.
The code is this: https://gist.github.com/3137818
Where I make wrong??
Thanks for help.
Hi!!!
I using Maple Rev 5, with IDE v0.0.12, I am new in C and Maple.
I try the code using char[] returning from a function but get nothing or trash.
The code is this: https://gist.github.com/3137818
Where I make wrong??
Thanks for help.
Your function returns the address of a local variable placed on the CPU stack. This data storage is invalid when the function returns. Local variables are only valid locally in the function where they are defined, they do not exist forever or outside of this function.
Hummmm, basic OOP principles.
Have other means for create pointer, without use global variables?
Thanks.
Here are two ways to make a function that returns a C-style string.
1) The caller provides a buffer and the function fills it in.
2) The function uses new or malloc to allocate memory on the heap, and the caller must use delete or free.
In rare occasions, this might be useful
3) Declare the buffer as a static variable inside the function and return a pointer to that. This acts the same as a global variable, except that the name of it is only visible within the function.
@ColinLewis.
That moment, I just opted for "buffer", also passing with his size. And a pointer when I am doing concatenations to indicate where the end of the string if it has not the "\ 0" to indicate its end.
In the case of the third option, it would also need to deallocate the memory when you no longer need to use the array?
There are a couple ways of getting it done. Here are two examples:
https://gist.github.com/3160604
I didn't compile it, or checked that it worked... Just wrote the general idea.
NOTE: My method 1 and method 2 are reversed from what ColinLewis
listed out.
carlosdelfino,
Using dynamically allocated memory in an embedded environ should generally be avoided unless it seems there is some over riding need to. In many cases you will use the same static buffer for a similar use (reading data from a port for instance) and as long as you are careful you can allocate it once and reuse it often. Writing your code to use case 1) provide the buffer, allows you to use locally scoped variables which will get cleaned up when your function is done with them. It will also work for the other cases as appropriate.
@carlosdelfino
In my 3rd option, the answer is no. The memory is not dynamically allocated, so you can not deallocate it.
carlosdelfino - you might also find stpcpy and stpncpy useful:
http://sourceware.org/newlib/libc.html#stpcpy
http://sourceware.org/newlib/libc.html#stpncpy
Also, IIRC, a sequence of calls to them is recognised by the gcc compiler and optimised in quite a helpful way.
I usually allocate a buffer, static to a source file, which also contains the functions which use it.
For a buffer of a few dozen bytes, it might take less space than malloc support.
More importantly, it has the lovely property that if the program loads, and there are no other bugs, it can never cause the program to fail. Which is a good property to have for embedded programs.
AFAIK, this is not guaranteed by any small-footprint garbage collectors. (But I'm interested in learning of my ignorance:-)
You must log in to post.