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; }
Looks fine, except that you might want to check that malloc didn't return NULL in allocating 'stringReturn'.
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; }
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:
I can safely strcpy long_but_empty into copy with strcpy, because literally what strcpy copies isCode:char long_but_empty[6000000]="hello world"; char copy[16];
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