Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8192585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:12:49+00:00 2026-06-07T04:12:49+00:00

I was reading a book on C today, and it mentioned that the following

  • 0

I was reading a book on C today, and it mentioned that the following was true; I was so curious as to why that I made this program to verify; and then ultimately post it here so someone smarter than me can teach me why these two cases are different at runtime.

The specifics of the question related to the difference at runtime between how a (char *) is handled based on whether it is pointing to a string created as a literal vs. created with malloc and manual population.

why is the memory allocated by the memory more protected like this? Also, does the answer explain the meaning of “bus error”?

Here is a program I wrote which asks the user if they would like to crash or not, to illustrate that the program compiles fine; and to highlight that in my head the code in both options is conceptually identical; but that’s why I’m here, to understand why they are not.

// demonstrate the difference between initializing a (char *) 
// with a literal, vs malloc
// and the mutability of the contents thereafter
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
    char cause_crash;
    char *myString;

    printf("Cause crash? "); 
    scanf("%c", &cause_crash);

    if(cause_crash == 'y') {
        myString = "ab";
        printf("%s\n", myString); // ab
        *myString = 'x'; // CRASH!
        printf("%s\n", myString);   
    } else {
        myString = malloc(3 * sizeof(char));
        myString[0] = 'a';
        myString[1] = 'b';
        myString[2] = '\0';
        printf("%s\n", myString); // ab
        *myString = 'x';
        printf("%s\n", myString); // xb     
    }
    return 0;
}

edit: conclusions

There are several good answers below, but I want to summarize what I have come to understand succinctly here.

The basic answer seems to be this:

When a compiler sees a “string literal” being assigned to a (char *) variable, the pointer will point to memory which is static (perhaps actually part of the binary, but usually enforced as read only by a lower-level system than your runtime. In other words, the memory is probably not dynamically allocated at that part of the program, but instead the pointer is simply set to point to an area of static memory which houses the contents of your literal.

There are a few things I want to call out about this resolution:

1. Optimization may be a possible motive: With my compiler, two different (char *) variables initialized with the same string literal actually point to the same address:

char *myString = "hello";
char *mySecond = "hello"; // the pointers are identical! This is a cool optimization.

2 Interstingly, if the variable is actually an array of chars (instead of a (char *)), this (#1) is not true. this was interesting to me because I was under the impression that (post-compilation) arrays where identical to pointers-to-chars.

char myArString[] = "hello";
char myArSecond[] = "hello"; // the pointers are NOT the same

3 to summarize what several answers hinted at: char *myString = "Hello, World!" does not allocate new memory, it just sets myString to point to memory which already existed; perhaps in the binary, perhaps in a special read-only block of memory… etc.

4 I found through testing that char myString[] = "Hello, World!" does allocate new memory; I think… what I know is that the string is mutable when created this way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:12:50+00:00Added an answer on June 7, 2026 at 4:12 am

    When you set a variable to a string literal, you are setting it to a value stored in the read only data section of the assembly program. These data items are constant, and attempts to use them differently will most likely crash.

    When you use malloc to get the memory, you are getting a pointer to read/write heap memory that you can do anything to.

    This is caused by a couple of reasons. For one thing, the actual type of "Hello, world" is char[13], or constant pointer to 13 characters. You can not assign a value to a constant character. But when you do something like what you do, which is casting away the constness. That means that the compiler wont prevent you from changing the memory, but the C standard calls is undefined behavior. Undefined behavior can be anything, but it is usually a crash.

    If you want to assign a literal value to char* memory, do this:

    char* data = malloc (42);
    memcpy(data, "Hi!", 4);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I mentioned in one of my earlier questions that I'm reading book C++ Coding
So I was reading this book where it says that if I create a
Reading a book I have found the following statement: (Object) Behaviors answer either of
I am reading some book and I have encountered a piece of code that
I am reading a book in which author used a code like this public
I have started reading the book Essential Linux Device Drivers. I am following the
I am reading a book that encourages the reader for a few reasons, to
I am reading a book which mentions this If the compiler knows every use
I'm reading a book (Rails 3 in Action) that's building a project management system
Today, while I was randomly reading the JavaScript patterns O'Reilly book, I found one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.