Thread: Simulate the malloc function using a static array

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    Simulate the malloc function using a static array

    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:

    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;
    }
    My problem is as follow:
    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
    Code:
    string ="test"
    this isn't placed in the memory and the values in the memory are still on the initialized values.

    What am I doing wrong?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    When you are declaring string as
    Code:
    char *string;
    You are not allocating memory, so strcpy has no where to write to

    When you are declaring string as
    Code:
    char *string = "apple";
    The compiler puts "apple" in read only memory and then has string point to it.

    If you want to modify string at runtime, you would need to declare it as an array -> It's your responsibility to make sure that you have enough memory

    Code:
    char string[6];
    
    char string[] = "apple";
    char string[6] = "apple";
    char string[6] = {'a', 'p', 'p', 'l', 'e', '\0'};
    Fact - Beethoven wrote his first symphony in C

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also note that the reason malloc works is because you are allocating memory that you can modify.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-21-2011, 03:26 AM
  2. Replies: 6
    Last Post: 12-13-2007, 08:20 PM
  3. What argument for a function? for static array variable
    By liechmaster in forum C++ Programming
    Replies: 7
    Last Post: 12-10-2005, 05:11 PM
  4. static array of function pointers within class
    By Yarbles in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2005, 02:10 PM
  5. function to simulate sleep
    By golsoje in forum C++ Programming
    Replies: 19
    Last Post: 03-16-2002, 08:27 PM