Skip to content

Instantly share code, notes, and snippets.

@pileon
Created October 27, 2017 01:08
Show Gist options
  • Save pileon/40305e3fd8277e9d396bb442de63ea26 to your computer and use it in GitHub Desktop.
Save pileon/40305e3fd8277e9d396bb442de63ea26 to your computer and use it in GitHub Desktop.
Object* read(char* str)
{
Object* object = (Object*)malloc(sizeof(*object));
object->identity[0] = 0;
int capacity = (100 + 1) - (10);
// No allocation here object->name = (char*)malloc(capacity * sizeof(*object->name));
object->value = 0.0;
int length = strlen(str);
if (length > capacity)
{
// No reallocation here object->name = (char*)realloc(object->name, (capacity * 2) * sizeof(*object->name));
// Increase size (TODO: Should make sure that it's actually larger than length)
capacity *= 2;
}
// One single allocation here
object->name = (char*)malloc(capacity * sizeof(*object->name));
int arguments = sscanf(str, "%" STRING_SPACE "s %lf %[^\n]s",
object->identity,
&object->value,
object->name);
if (arguments == MATCHER) {
return object;
} else {
return NULL;
}
return object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment