#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Working with memory aloocation and chars


    Hello,

    I have a task to make which in it I have to recieve input until an enter appears, or EOF.

    I cannot use a linked list, so my milestones here are:
    1. Aloocate memory for initial input.
    2. Check if there's memory left.
    a. If there is memory, countinue.l
    b. If there isn't print input, free memory and reallocate memory.
    3. Print the input anyways, because of that there is a flag variable.

    My question is, should I use free before reallocate, or opposite?

    Thank you, I would love to get feedbacks for my program.

    The code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_CHARS_INPUT 200
    
    void printWithMalloc(){
    	char *ptr_input;
    	int c; int flag=0;
    	ptr_input=(int*)malloc(MAX_CHARS_INPUT*sizeof(int));
    	int i=0;
    	printf("please enter a string\n");
    	while((c=getchar())!=EOF && c!='\n')
    	{
    		flag=0;                
                    ptr_input[i]=(c=getchar());
    		if (*ptr_input)
    			ptr_input[i++]=c; //get input to an array to print later
    		else
    		{
    			while(i>=0)
    			{
    				printf("%c",ptr_input[i--]; //print input to free memory	
    			}
    		free(ptr_input);
                    //reallocate          memory
    		ptr_input=(char*)realloc(ptr_input,MAX_CHARS_INPUT*sizeof(char));
                   //input printrd so flag should be 1
    		flag=1; 
    		}
    		//print output if it hasn't been done yet
    		if (!flag)
    			while(i>=0)
    				printf("%c",ptr_input[i--]; 	
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    You can choose among 3 reasonable patterns.

    Code:
    malloc
    if malloc failed take correct action // you ignored this step although it's explicitly in the instructions
    
    repeat until done
      realloc
      if allocation failed take action // you ignored this step although it's explicitly in the instructions
    
    done:
    free(pointer_to_memory)
    
    
    
    
    
    pointer_to_memory = NULL
    repeat until done
      realloc
      if allocation failed take action // you ignored this step although it's explicitly in the instructions
    
    done:
    free(pointer_to_memory)
    
    
    
    
    //and I forget the third scheme
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by nerazzurri10
    My question is, should I use free before reallocate, or opposite?
    Haven't you read the manual? Here is a link to thousands of copies of the documentation for realloc: http://linux.die.net/man/3/realloc
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by dwise1_aol
    Haven't you read the manual? Here is a link to thousands of copies of the documentation for realloc: http://linux.die.net/man/3/realloc
    Thank you for this link. I'll read it briefly.

IMN logo majestic logo threadwatch logo seochat tools logo