Thread: Functions for C strings

  1. #31
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by officedog View Post
    Code:
    buffer = realloc(buffer, sizeof(char) * baseSize);
    I guess I should also add a test for a NULL pointer too
    Yes, much better. And just nit-picking here, but you could probably bypass the part that makes a smaller copy by simply starting with a smaller buffer size (say 8 or 16, maybe). Just a thought.

  2. #32
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Really appreciate the help. Thanks. Just for completeness, I post the final(ish) version of the function. I made initial buffer as 64 to decrease likelihood of resizing

    Code:
    char * GC_GetUserInputDyanmicV2() {
        int baseSize =  64;
        char c;
        
        //dynamic allocation of buffer
        char * buffer = malloc(sizeof(char) * baseSize);
        if (buffer == NULL) {
            return buffer;
        }
        
        int index = 0;
        
        while ((c = fgetc(stdin)) != '\n'  && c != EOF) {
            //resize buffer if not enough room
            if (index > baseSize -2) {
                baseSize += baseSize;
                buffer = realloc(buffer, sizeof(char) * baseSize);
                if (buffer == NULL) {
                    return NULL;
                }
            }
            buffer[index] = c;
            index++;
        }
        //adds '\0' to end of the string (needed for string functions)
        buffer[index] = '\0';
        
        //checks length of string
        int len = strlen(buffer);
        printf("\nLength of final string (without 0) = %d", len);
        
        //Dynamic allocation of correct sized memory
        char * stringReturn = malloc(sizeof(char) * (len + 1) );
        
        //copies buffer to return string
        strncpy(stringReturn, buffer, len+1);
        //adds '\0' to end of return string
        stringReturn[len] = '\0';
        
        //frees buffer
        free(buffer);
        buffer = NULL;
        
        return stringReturn;
    }

  3. #33
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Looks fine, except that you might want to check that malloc didn't return NULL in allocating 'stringReturn'.

  4. #34
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    oh yes, missed that one. updated version:

    Code:
    char * GC_GetUserInputDyanmicV2() {
        int baseSize =  64;
        char c;
        
        char * buffer = malloc(sizeof(char) * baseSize);
        if (buffer == NULL) {
            return buffer;
        }
        
        int index = 0;
        
        while ((c = fgetc(stdin)) != '\n'  && c != EOF) {
            if (index > baseSize -2) {
                
                baseSize += baseSize;
                buffer = realloc(buffer, sizeof(char) * baseSize);
                if (buffer == NULL) {
                    return NULL;
                }
                printf("\nReallocated space: baseSize = %d, index = %d", 
                       baseSize, index);
                
            }
            buffer[index] = c;
            index++;
        }
        //adds '\0' to end of the string (needed for string functions)
        buffer[index] = '\0';
        
        //checks length of string
        int len = strlen(buffer);
        printf("\nLength of final string (without 0) = %d", len);
        
        //Dynamic allocation of correct sized memory
        char * stringReturn = malloc(sizeof(char) * (len + 1) );
        if (stringReturn == NULL) {
            return NULL;
        }
        
        //copies buffer to return string
        strncpy(stringReturn, buffer, len+1);
        //adds '\0' to end of return string
        stringReturn[len] = '\0';
        
        //frees buffer
        free(buffer);
        buffer = NULL;
        
        return stringReturn;
    }

  5. #35
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You don't need to use strncpy, you can use strcpy. strcpy copies a string*, not an entire variable's allocated size. So if I have:
    Code:
    char long_but_empty[6000000]="hello world";
    char copy[16];
    I can safely strcpy long_but_empty into copy with strcpy, because literally what strcpy copies is

    hello world[\0]

    which is only 12 characters. So if you have malloc'd stringReturn to be the right length, there won't be a problem.

    * a string ends with '\0', which you did add one to buffer.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings and functions
    By simo_mon in forum C Programming
    Replies: 8
    Last Post: 09-16-2008, 05:18 AM
  2. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  5. Replies: 2
    Last Post: 04-13-2003, 08:40 PM