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 3338958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:26:13+00:00 2026-05-18T00:26:13+00:00

Here are 2 type of code snippets which have the same outputs. char *p

  • 0

Here are 2 type of code snippets which have the same outputs.

char *p = "abc";
::printf("%s",p);

And

::printf("%s","abc");

Is there any difference as to where the “abc” string is stored in memory?

I once heard that in the second code, the “abc” string is placed by the compiler in read-only memory (the .text part?)

How to tell this difference from code if any?

Many thanks.

Update

My current understanding is:

when we write:

char *p="abc"

Though this seems to be only a declarative statement, but indeed the compiler will generate many imperative instructions for it. These instructions will allocate proper space within the stack frame of the containing method, it could be like this:

subl %esp, $4

then the address of “abc” string is moved to that allocated space, it could be like this:

movl $abc_string_address, -4(%ebp)

The “abc” string is stored in the executable file image. But where in the memory it (i mean the string) will be loaded totally depends on the implementation of the compiler/linker, if it is loaded into the read-only part of the process’s address space (i.e. the protection bit of the memory page is flagged as read-only), then the p is a read-only pointer, if it is loaded into the r/w part, the p is writable.

Correct me if I am wrong. Now I am looking into the assembly code generated by the gcc to have a confirmation for my understanding. I’ll update this thread again shortly.

  • 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-05-18T00:26:14+00:00Added an answer on May 18, 2026 at 12:26 am

    Is there any difference as to where the “abc” string is stored in memory?

    Nope, and that is true for both. String literals are stored in the read-only segment. However, if you declare your variable as a char[] it will be copied onto the stack, i.e., not read only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are the following code snippets equivalent? class a {} class b:a {} b foo=new
Here's a newbie question on the WPF TabControl, TabItem and TabPanel. There is a
Assume I have a static generic class. Its generic type parameters are not available
I created a little proxy node script, which looksup the request.url and either passes
I am having a couple problems with the code from the tutorial: ListView Tutorial
I wave a method in C++ that receives a parameter of the LPCOLESTR type.
I am using jQuery Form Plugin http://jquery.malsup.com/form/ to submit my form. Here is the
I'm making a call to the database. The result must be used for a
I am trying to save some images from Flash to Php by sending a
I get back the responseXml as a javascript object XMLdocument. How do i parse

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.