Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#Modified code

Modified code

#Modified code

Modified code

Suggest a bigger initial size to save on mem usage (stolen from hoffmale's deleted answer)
Source Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325
void String_init(struct String* string)
{
 string->data = NULL;
 string->size = string->capacity = 0;
}
void String_allocate_space(struct String* string)
{
 size_t new_capacity = string->capacity ? 2 * string->capacity : 1;16;
 char *tmp = realloc(string->data, new_capacity);
 if (!tmp) {
 /* error handling - c is still valid */
 exit(1); /* TODO: improve error reporting */
 }
 string->data = tmp;
 string->capacity = new_capacity;
}

I've also incorporated a change above to start with a larger initial size (16) instead of 1. That lets us skip the first 4 reallocations for free.

void String_init(struct String* string)
{
 string->data = NULL;
 string->size = string->capacity = 0;
}
void String_allocate_space(struct String* string)
{
 size_t new_capacity = string->capacity ? 2 * string->capacity : 1;
 char *tmp = realloc(string->data, new_capacity);
 if (!tmp) {
 /* error handling - c is still valid */
 exit(1); /* TODO: improve error reporting */
 }
 string->data = tmp;
 string->capacity = new_capacity;
}
void String_init(struct String* string)
{
 string->data = NULL;
 string->size = string->capacity = 0;
}
void String_allocate_space(struct String* string)
{
 size_t new_capacity = string->capacity ? 2 * string->capacity : 16;
 char *tmp = realloc(string->data, new_capacity);
 if (!tmp) {
 /* error handling - c is still valid */
 exit(1); /* TODO: improve error reporting */
 }
 string->data = tmp;
 string->capacity = new_capacity;
}

I've also incorporated a change above to start with a larger initial size (16) instead of 1. That lets us skip the first 4 reallocations for free.

*c = realloc(*c, sizeof(**c) * (*capacity));
void String_init(struct String* string)
{
 string->data = NULL;
 sizestring->size = capacitystring->capacity = 0;
}
void String_allocate_space(struct String* string)
{
 size_t new_capacity = string->capacity ? 2 * string->capacity : 1;
 char *tmp = realloc(string->data, new_capacity);
 if (!tmp) {
 /* error handling - c is still valid */
 exit(1); /* TODO: improve error reporting */
 }
 string->data = tmp;
 string->capacity = new_capacity;
}

#Modified code #include <stdio.h> #include <stdlib.h> #include <ctype.h>

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct String {
 char* data;
 size_t size;
 size_t capacity;
};
void String_init(struct String* string)
{
 string->data = NULL;
 string->size = string->capacity = 0;
}
void String_allocate_space(struct String* string)
{
 size_t new_capacity = string->capacity ? 2 * string->capacity : 1;
 char *tmp = realloc(string->data, new_capacity);
 if (!tmp) {
 /* error handling - c is still valid */
 exit(1); /* TODO: improve error reporting */
 }
 string->data = tmp;
 string->capacity = new_capacity;
}
void add_character(struct String* string, char ch)
{
 if (string->size == string->capacity) { // if current letter exceeds capacity
 String_allocate_space(string);
 }
 string->data[string->size++] = ch; // append it
}
void String_read(struct String* string)
{
 string->size = 0;
 int ch;
 while ((ch = getc(stdin)) != EOF && !isspace(ch)) {
 add_character(string, (char)ch);
 }
}
void String_print(struct String *restrict string, FILE *restrict stream)
{
 if (fwrite(string->data, 1, string->size, stream) != string->size) {
 exit(1); /* TODO: improve error reporting */
 }
}
void String_free(struct String* string)
{
 free(string->data);
 string->data = NULL;
 string->size = string->capacity = 0;
}
int main()
{
 struct String string;
 String_init(&string);
 String_read(&string);
 String_print(&string, stdout);
 String_free(&string);
}
*c = realloc(*c, sizeof(**c) * (*capacity));
void String_init(struct String* string)
{
 string->data = NULL;
 size = capacity = 0;
}
void String_allocate_space(struct String* string)
{
 size_t new_capacity = string->capacity ? 2 * string->capacity : 1;
 char *tmp = realloc(string->data, new_capacity);
 if (!tmp) {
 /* error handling - c is still valid */
 exit(1); /* TODO: improve error reporting */
 }
 string->data = tmp;
 string->capacity = new_capacity;
}

#Modified code #include <stdio.h> #include <stdlib.h> #include <ctype.h>

struct String {
 char* data;
 size_t size;
 size_t capacity;
};
void String_init(struct String* string)
{
 string->data = NULL;
 string->size = string->capacity = 0;
}
void String_allocate_space(struct String* string)
{
 size_t new_capacity = string->capacity ? 2 * string->capacity : 1;
 char *tmp = realloc(string->data, new_capacity);
 if (!tmp) {
 /* error handling - c is still valid */
 exit(1); /* TODO: improve error reporting */
 }
 string->data = tmp;
 string->capacity = new_capacity;
}
void add_character(struct String* string, char ch)
{
 if (string->size == string->capacity) { // if current letter exceeds capacity
 String_allocate_space(string);
 }
 string->data[string->size++] = ch; // append it
}
void String_read(struct String* string)
{
 string->size = 0;
 int ch;
 while ((ch = getc(stdin)) != EOF && !isspace(ch)) {
 add_character(string, (char)ch);
 }
}
void String_print(struct String *restrict string, FILE *restrict stream)
{
 if (fwrite(string->data, 1, string->size, stream) != string->size) {
 exit(1); /* TODO: improve error reporting */
 }
}
void String_free(struct String* string)
{
 free(string->data);
 string->data = NULL;
 string->size = string->capacity = 0;
}
int main()
{
 struct String string;
 String_init(&string);
 String_read(&string);
 String_print(&string, stdout);
 String_free(&string);
}
*c = realloc(*c, sizeof(**c) * (*capacity));
void String_init(struct String* string)
{
 string->data = NULL;
 string->size = string->capacity = 0;
}
void String_allocate_space(struct String* string)
{
 size_t new_capacity = string->capacity ? 2 * string->capacity : 1;
 char *tmp = realloc(string->data, new_capacity);
 if (!tmp) {
 /* error handling - c is still valid */
 exit(1); /* TODO: improve error reporting */
 }
 string->data = tmp;
 string->capacity = new_capacity;
}

#Modified code

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct String {
 char* data;
 size_t size;
 size_t capacity;
};
void String_init(struct String* string)
{
 string->data = NULL;
 string->size = string->capacity = 0;
}
void String_allocate_space(struct String* string)
{
 size_t new_capacity = string->capacity ? 2 * string->capacity : 1;
 char *tmp = realloc(string->data, new_capacity);
 if (!tmp) {
 /* error handling - c is still valid */
 exit(1); /* TODO: improve error reporting */
 }
 string->data = tmp;
 string->capacity = new_capacity;
}
void add_character(struct String* string, char ch)
{
 if (string->size == string->capacity) { // if current letter exceeds capacity
 String_allocate_space(string);
 }
 string->data[string->size++] = ch; // append it
}
void String_read(struct String* string)
{
 string->size = 0;
 int ch;
 while ((ch = getc(stdin)) != EOF && !isspace(ch)) {
 add_character(string, (char)ch);
 }
}
void String_print(struct String *restrict string, FILE *restrict stream)
{
 if (fwrite(string->data, 1, string->size, stream) != string->size) {
 exit(1); /* TODO: improve error reporting */
 }
}
void String_free(struct String* string)
{
 free(string->data);
 string->data = NULL;
 string->size = string->capacity = 0;
}
int main()
{
 struct String string;
 String_init(&string);
 String_read(&string);
 String_print(&string, stdout);
 String_free(&string);
}
Add a lesson on invariants; various fixes
Source Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325
Loading
Finish the review
Source Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325
Loading
Add String_init
Source Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325
Loading
Source Link
Toby Speight
  • 87.9k
  • 14
  • 104
  • 325
Loading
lang-c

AltStyle によって変換されたページ (->オリジナル) /