Created
February 25, 2016 07:56
-
-
Save insipx/49e0c8848ad6cbcc1981 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char * readUUI() | |
{ | |
//allocate 64bytes for usage | |
size_t size = 64; | |
char *str = (char*)malloc(size); | |
if(str){ | |
}else{ | |
die("Mem allocate unsuccessful"); | |
} | |
char c; | |
int i = 0; | |
do | |
{ | |
c = fgetc(stdin); | |
str[i] = c; | |
i++; | |
//reallocate memory by +1 every time, we use very little memory this way | |
//this isn't the most mem-efficient method but it should use less cycles | |
if(i >= size){ | |
size++; | |
str = (char*) realloc(str, size); | |
} | |
if(c == '\n' || c == EOF){ | |
str[i] = '\0'; | |
break; | |
} | |
}while(1); | |
printf("%s%s", " This is the Readline String: ", str); | |
printf("%s%d%s", " and this is the size ", sizeof(str)/sizeof(char), "\n"); | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment