Hello
It has been a long time since I used C. So therefore I'm trying to refresh my knowledge by simulating the malloc function. I want to use a static array and I want to "malloc" memory from this array.
This is the code I got so far:
My problem is as follow:Code:char memory[10]; void initMemory(){ int i=0; for(i=0; i < 10; i++){ memory[i] = '?'; } } void* malloc_t(int size){ int i=0; int crtSize = 0; for(i=0; i < 10; i++){ if(memory[i] == '?'){ if(crtSize == size){ return &memory[i-size]; } crtSize++; }else{ crtSize = 0; } } } int main(void){ initMemory(); char *string = (char*)malloc_t(5 * sizeof(char)); //strcpy(string,"test\0"); string = "test"; printf("string: %s\n", string); int i=0; for(; i < 10; ++i){ printf("char: %c\n", memory[i]); } return 0; }
When I use the strcpy function in the main to copy my string in the memory this works. My string "test" is nicely placed in the memory. But when I use
this isn't placed in the memory and the values in the memory are still on the initialized values.Code:string ="test"
What am I doing wrong?