Thread: How would you optimize this code? (Memory allocation for character input)

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    101

    How would you optimize this code? (Memory allocation for character input)

    This is maybe the last exercise I will do of my course (yet finished). We have 6 exercises that our instructor left in the last class, 6 exercises about memory allocation, this is the fourth one.

    Statement: Insert characters through keyboard one by one; the program must ask Y/N choice to continue inserting characters (one by one). Use realloc function for memory allocation. The program must print all the characters inserted.

    I have been working in the program. I am not lazy at least, you know. I tried to solve it and I'm happy, I solved it. The code works "like a charm" but I want to use the correct code according to your recommendations. Please, don't blame me too much, I promise you I am making the effort to go to the right way, but still I need more practice. I am using some code thanks to laserlight. I know there is room for improvements, that's the reason I'm posting the code here. Compiled in C Now, the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_SIZE 10//No magic numbers, I took your advice seriously men
    
    
    int main()
    {
        int cont=0;
        char *ptr;//Pointer to store characters
        char c;
        char choice;//To read characters one by one through Y/N choice
    
    
        ptr=calloc(MAX_SIZE, sizeof(char));//To allocate at least 10 characters
    
    
        if (!ptr) {//Code by laserlight
            fprintf(stderr, "There is not enough memory.\n");
            return EXIT_FAILURE;
        }
    
    
        do {//do while loop to work at least once
            printf("Insert a character: ");
            scanf(" %c", &c);
            ptr=realloc(ptr, MAX_SIZE * sizeof(char));//Expand to allow 10 extra characters
    
            if (!ptr) {//Code by Laserlight
                fprintf(stderr, "The re-allocation failed due to lack of memory.\n");
                return EXIT_FAILURE;
            }
            printf("Insert more characters: (Y/N)");//Y/N choice to insert characters one by one
            scanf(" %c", &choice);
            ptr[cont++]=c;
        } while (choice=='Y' || choice=='y');
    
    
        ptr[strcspn(ptr, "\n")] = '\0';
        printf("Characters inserted: %s\n", ptr);
        free(ptr);//To free memory
    
    
        return 0;
    }
    I have a question, should I put another free above after calloc or is ok right now? I know there is room for improvements. What do you think? I am working with C because I compiled in C and I am in C forum.

    I feel the Cristiano Ronaldo of programming . No, that was a joke to relax the environment.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, we know - stop double-posting if your post is moderated.
    How would you optimize this code? (Memory allocation for character input)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2017, 04:33 AM
  2. Optimize memory management
    By merafiq in forum C Programming
    Replies: 8
    Last Post: 10-06-2015, 09:42 AM
  3. Help me to optimize this code
    By Fn00 in forum C Programming
    Replies: 12
    Last Post: 09-27-2015, 04:28 PM
  4. How can i optimize this code
    By ArunS in forum C Programming
    Replies: 15
    Last Post: 08-08-2011, 02:11 PM
  5. character input using fgetc() code???
    By lesrhac03 in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 10:55 PM

Tags for this Thread