I've been working on interfacing my Ti-83 with my Arduino and had trouble with dynamically allocating an array. I tried including the new operator function I found somewhere but that didn't look right and only worked once (it failed when trying to allocate a second array after supposedly deleting the old one).
I don't think the code I used was correct but I'm not sure how to rewrite it.
void * operator new(size_t size);
void * operator new[](size_t size);
void operator delete(void * ptr);
void operator delete[](void * ptr);
void * operator new(size_t size)
{
return malloc(size);
}
void operator delete(void * ptr)
{
free(ptr);
}
void * operator new[](size_t size)
{
return malloc(size);
}
void operator delete[](void * ptr)
{
free(ptr);
}
To my eyes it looks like the new[] operator doesn't do anything more than the new operator does, in other words it allocates a single instance of the object (in my case an unsigned int) but doesn't allocate anything for the rest of the array. It seems like it should have a for loop in there but it seems to only have a single argument.
What I'd like to do is this:
packetData = new unsigned int[packetHeader[2]];
Followed later by this:
delete[] packetData;
Or whatever the proper syntax is. In fact, this may be my main problem. Is that the correct syntax? Or do I need to provide the size again (now that I think about it I think I would).
Like I said, the first time through it seemed to work fine, the second time, however, it just stopped when it tried to new another array.