Hi,
I wanted to make a function that can create an array of sine waves.
But when I call the function more than one time, it overwrites on the array which I used before. Why is this happening?
Huh. I'd never seen malloc(0) before. but apparently it should work?
This, however is wrong:
realloc(_array, s * 2);
realloc() returns a pointer to the bigger (or smaller) chunk of memory; you can't count on that being at the same place. From the man page:
The realloc() function tries to change the size of the allocation pointed to by ptr
to size, and returns ptr. If there is not enough room to enlarge the memory alloca-
tion pointed to by ptr, realloc() creates a new allocation, copies as much of the
old data pointed to by ptr as will fit to the new allocation, frees the old alloca-
tion, and returns a pointer to the allocated memory.
Allocating memory at run time is a very risky business in the small memory of an Arduino. Much better to allocate space for your arrays at compile time.
You are committing THE cardinal sin of malloc()/alloc()/realloc()/sbrk() use - you never NOT check to see if a NULL pointer is returned.
And I would NOT count on malloc(0) returning a valid allocation block. I would instead initialize the pointer to NULL, and instead of doing a realloc, simply do the malloc there instead. If you MUST use realloc(), first check if the pointer is NULL, and, if so, then do a malloc() at that time. Blindly using what any of those functions return WILL blow up your program at some point.
RayLivingston:
You are committing THE cardinal sin of malloc()/alloc()/realloc()/sbrk() use - you never NOT check to see if a NULL pointer is returned.
And I would NOT count on malloc(0) returning a valid allocation block. I would instead initialize the pointer to NULL, and instead of doing a realloc, simply do the malloc there instead. If you MUST use realloc(), first check if the pointer is NULL, and, if so, then do a malloc() at that time. Blindly using what any of those functions return WILL blow up your program at some point.
Regards,
Ray L.
malloc(0) and realloc(NULL) are both safe. Their behaviors are well defined in the manual.
The place OP needs to NULL check is where he accesses it.