Hey guys,
since realloc isn't ANSI C and for that not support by KEIL I tried to create my own realloc function.
But - as usual - I have some pointer trouble :-/ .
Here is my try for this memory reallocation function. But it doesn't compile because of the statement in the for loop:
void* Memrealloc(void* pvMem, uint32_t ulNewSize) { int i; char *ptr; //Pointer for the new memory area ptr = (char*)malloc(ulNewSize); //Allocating the amount of requested memory for (i=0; i<=ulNewSize;i++) { ptr[i]=(char*)pvMem[i]; //Copy the old content of the memory } free (pvMem); //Deallocating the old memory return (void*)ptr; //Returning pointer to the new memory }
Thats the error I get: Source\OS_Custom.c(79): error: #852: expression must be a pointer to a complete object type
What is happening here? What do I have to do to fix it?
"What do I have to do to fix it?"
Feel free to take a closer look at the precedence rules for C.
Or consider using memcpy().
By the way 1: What if malloc() fails?
By the way 2: You think it is a good idea to use dynamic memory - especially dynamic memory that gets reallocated in the middle of execution and not just dynamically allocated at start? What do you do when you get too much memory fragmentation?
by the way 3: do not code embedded as if it was a PC
Erik
Thank's for the hint, Per:
ptr[i]=((char*)pvMem)[i];
works fine.
But thanks to you guys I know now: Realloc works also. And Memcopy exists, too. And I shouldn't use realloc :)