Skip to main content

Command Palette

Search for a command to run...

Where do your strings live?

Updated
2 min read
J

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class.

At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out.

In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.

In C, strings are represented as arrays of characters. There are various ways to declare and define strings, and the allocation of memory for strings depends on how they are declared and defined.

  1. String literals:

    • String literals are constant arrays of characters and are declared using double quotes. They are allocated in the read-only section of memory.

    • Example:

        const char *stringLiteral = "Hello, World!";
      
    • Explanation: The string literal "Hello, World!" is stored in the read-only section of memory. stringLiteral is a pointer to the first character of this string.

  2. Character arrays:

    • Strings can be declared as character arrays, which are mutable and can be modified.

    • Example:

        char mutableString[] = "Mutable String";
      
    • Explanation: The character array mutableString is allocated on the stack, and it is initialized with the content "Mutable String."

  3. Dynamic memory allocation using malloc and strcpy:

    • Strings can be dynamically allocated on the heap using malloc and then filled using functions like strcpy.

    • Example:

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
      
        int main() {
            char *dynamicString = (char *)malloc(20);
            strcpy(dynamicString, "Dynamic String");
            // ...
            free(dynamicString);  // Don't forget to free the allocated memory
            return 0;
        }
      
    • Explanation: The memory for dynamicString is allocated on the heap using malloc. The content is then copied using strcpy. It is crucial to free the allocated memory using free to avoid memory leaks.

Regarding the use of const:

  • When const is used in the declaration of a pointer, it indicates that the memory content pointed to by the pointer is constant and should not be modified.

  • Using const with strings can help prevent unintended modifications and is a good practice.

  • Example:

      const char *constString = "Constant String";
    

    In this case, attempting to modify the content through constString would result in a compilation error.

When passing strings as function parameters:

  • Using const in function parameters indicates that the function will not modify the content of the string.

  • Example:

      void printString(const char *str) {
          printf("%s\n", str);
      }
    

    This prevents unintentional modifications within the function and improves code safety.

In summary, the choice of string representation depends on the requirements of your program. Use string literals for constant strings, character arrays for mutable strings on the stack, and dynamic memory allocation for strings on the heap when the size is unknown at compile time. Use const to ensure immutability and improve code safety.

More from this blog

Jyotiprakash's Blog

251 posts

I'm Jyotiprakash, a software dev and professor at KIIT, with expertise in system programming.