Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Memory Management for Strings | String Representation in C
C Strings

bookMemory Management for Strings

Stryg for at vise menuen

When working with strings in C, understanding how memory is allocated is essential to avoid bugs and ensure your programs run reliably. There are two main approaches to allocating memory for strings: static allocation and dynamic allocation. Static allocation uses a fixed-size array, while dynamic allocation uses functions like malloc to request memory at runtime. Regardless of the method, you must always account for the null-terminator ('\0') at the end of every C string. The null-terminator signals the end of the string, and omitting space for it can lead to unpredictable behavior.

main.c

main.c

copy
1234567891011121314151617181920212223
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { // Static allocation char greeting[6]; // Enough for "Hello" + '\0' strcpy(greeting, "Hello"); printf("Static allocation: %s\n", greeting); // Dynamic allocation char *dynamic_greeting = (char *)malloc(6 * sizeof(char)); // 5 for "Hello" + 1 for '\0' if (dynamic_greeting == NULL) { printf("Memory allocation failed!\n"); return 1; } strcpy(dynamic_greeting, "Hello"); printf("Dynamic allocation: %s\n", dynamic_greeting); free(dynamic_greeting); return 0; }

In the code above, you see both static and dynamic allocation for a string that will store the word "Hello". For static allocation, the array size is set to 6 to hold 5 characters plus the null-terminator. For dynamic allocation, malloc requests 6 bytes—again, 5 for the visible characters and 1 for the null-terminator. If you forget to allocate space for the null-terminator, the string will not be properly terminated, which can cause functions like strcpy and printf to read past the end of your string, potentially causing crashes or data corruption.

main.c

main.c

copy
123456789101112
#include <stdio.h> #include <string.h> int main() { // Incorrect: no space for null-terminator char bad_string[5]; strcpy(bad_string, "Hello"); // "Hello" needs 6 bytes, only 5 allocated // This may cause undefined behavior printf("bad_string: %s\n", bad_string); return 0; }

This example demonstrates what happens if you do not allocate enough space for the null-terminator. Here, bad_string is only 5 bytes long, just enough for the characters in "Hello" but not for the \0. Writing "Hello" into this array overwrites memory beyond what was allocated, leading to undefined behavior. This can result in subtle bugs that are hard to trace. To manage memory safely when dealing with strings, always ensure you allocate one extra byte for the null-terminator, check the size of your arrays carefully, and use memory allocation functions responsibly. When using malloc, always multiply the length of your string (plus one for the null-terminator) by sizeof(char), and remember to free dynamically allocated memory when it is no longer needed.

question mark

When allocating memory for a string in C, what must you always remember to include?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 5

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Sektion 1. Kapitel 5
some-alt