hi,
I'm running into issues about malloc.
part 1:
which is the correct way for malloc in a local function to get the same array as
#define ARRLEN_MAX 256
int16_t buf[ARRLEN_MAX];
I tried
void optimizeArray(int16_t * array, int16_t arrlen) {
// dynamic allocation meant to replace static array definition above:
int16_t * buf;
buf = (int16_t *) malloc(arrlen);
//...optimization code
free (buf);
}
void main() {
int16_t input[ARRLEN_MAX];
optimizeArray(input, ARRLEN_MAX);
}
with the static buf definition the optimization code works, with the dynamic local allocation it does not.
What am I missing about
int16_t * buf;
buf = (int16_t *) malloc(arrlen);
:?:
What is happening? Are you getting NULL? Do you have enough RAM?
It might be better to just create an auto and allocate on the stack.
unfortunately the features of this forum about attachements of pictures and (above all) lenght of code are far too poor.
I will post it later.
I first just would like to know if - in principle - my malloc syntax code
(// passed by value: arrlen=256;)
int16_t * buf;
buf = (int16_t *) malloc(arrlen);
is correct to match the global array definition?
#define ARRLEN_MAX 256
int16_t buf[ARRLEN_MAX];
system
June 27, 2016, 5:03pm
5
There doesn't seem to be anything wrong with the snippet you posted, other than the fact that it is a snippet.
If your code is too long to post, maybe you've got other stuff that is hogging the RAM.
Who knows?
unfortunately the features of this forum about attachements of pictures and (above all) lenght of code are far too poor.
I don't recall anyone who has even got near to breaking the code attachment limit.
I am copying parts from 1 array (array) to another (buf) and then copy back:
memset(buf, 0, sizeof(buf) );
for (i=signalstart; i<=signalend; ++i) buf[i-signalstart]=array[i];
memcpy(array, buf, sizeof(buf) );
free (buf);
what I get correctly by the global array is this:
what I get by the local malloc buf is this:
here is the complete topic:
Can't you just tell us what the stinking error is?
it should copy just the "word" in the middle of the old array (between signalstart and signalend) to the start of the 2nd and cut away all the rest
(red word in the 1st photo)
instead, with the malloc array, it copies the whole array without cutting away anything
(2nd photo)
A) malloc doesn't know you are allocating space for 16-bit ints so it expects the length to be in bytes.
int16_t * buf = malloc(arrlen * sizeof (int16_t));
B) when 'buf' is a pointer the length is 2 so this won't work:
memcpy(array, buf, sizeof(buf) );
To move the whole array you want the size of the array in bytes (again):
memcpy(array, buf, arrlen * sizeof (int16_t));
It might be good to define a local variable named 'arraybytes' initialized to 'arrlen * sizeof (int16_t)' to make it clear what the calculation is used for.
thank you!
but with the global array memcpy works, but not with the malloc array...
why is that so?
ps,
@johnwasser :
I tried your code, but it didn't compile.
I changed it to
int16_t * buf;
buf = (int16_t *)malloc(arrlen * sizeof (int16_t) );
...
memset(buf, 0, sizeof(buf) );
for (i=signalstart; i<=signalend; ++i) buf[i-signalstart]=array[i];
memcpy(array, buf, arrlen * sizeof (int16_t));
but then it's the same fault as before
system
June 27, 2016, 5:46pm
12
OK, thanks for letting us know.
ArthurD:
but then it's the same fault as before
So if fixing the obvious errors in the part of the code we were shown doesn't fix the problem it seems likely that there are errors in the part of the code we have not been shown.
system
June 27, 2016, 7:04pm
15
Sorry, don't read German too well.
like me for English
but it's just about the malloc and the memcpy thing, and that's just C.
system
June 27, 2016, 7:24pm
17
So, when you post a single sketch (here, obviously) that demonstrates the problem, I'll comment.
Who knows, in working through your problem, you may find the answer.
what?
I linked to all the code. If you can't help or if you're too lazy to start thinking about it, just let it be - maybe someone else finds the mistake.
The forum editor stupidly does not allow to post it here in a code box.
I meanwhile assume that it's a native Arduino compile error, it seems on the Pi I don't get that error.
(I use IDE 1.6.5)
system
June 27, 2016, 7:38pm
19
If you can't be bothered to help others to help you, or you're too lazy to try, please stay over at the toy forum.
what?
that sounds like rubbish - what do you want to communicate?
If you do not want to help, please keep off of this topic.