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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:54:51+00:00 2026-06-11T06:54:51+00:00

When I compile and run following C program on my Linux x86_64 machine, compiled

  • 0

When I compile and run following C program on my Linux x86_64 machine, compiled by GCC :

#include <stdio.h>

int main(void)
{
    char *p1 = "hello";               // Pointers to strings
    char *p2 = "hello";               // Pointers to strings
    if (p1 == p2) {                   // They are equal
    printf("equal %p %p\n", p1, p2);  // equal 0x40064c 0x40064c
                                      // This is always the output on my machine
    }
    else {
    printf("NotEqual %p %p\n", p1, p2);
    }
}

I always get the output as:

equal 0x40064c 0x40064c

I understand that strings are stored in a constant table but address are too low when compared to dynamically allocated memory.

Compare with following program:

#include <stdio.h>

int main(void)
{
    char p1[] = "hello";                // char arrar
    char p2[] = "hello";                // char array
    if (p1 == p2) {
    printf("equal %p %p\n", p1, p2);
    }
    else {                              // Never equal
    printf("NotEqual %p %p\n", p1, p2); // NotEqual 0x7fff4b25f720 0x7fff4b25f710
                                        // Different pointers every time
                                        // Pointer values too large
    }
}

The two pointers are not equal, because these are two arrays which can be independently manipulated.

I want to know how GCC generates the code for these two programs and how are they mapped to memory during execution. Since this would be already documented do so many times any links to documentation are welcome as well.

  • 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-11T06:54:53+00:00Added an answer on June 11, 2026 at 6:54 am

    In both cases the compiler emits the actual bytes of the string "hello" just once, in the .rodata section of the program (rodata stands for read only data).

    They are actually mapped directly from the executable file into memory, somewhat similar to the code section. That’s why they are far apart from the dynamically allocated ones.

    Then:

    char *p = "hello";
    

    Simply initializes p to the address of this (read-only) data.
    And obviously:

    char *q = "hello";
    

    Gets the very same address. This is called string pooling and is an optional popular optimization of the compiler.

    But when you write:

    char p[] = "hello";
    

    It will probably generate something like this:

    char p[6];
    memcpy(p, "hello", 6);
    

    Being the "hello" actually the address of the read-only pooled string.

    The call to memcpy is for illustration purposes only. It may very well to the copy inline, instead than with a function call.

    If later you do:

    char q[] = "hello";
    

    It will define another array and another memcpy(). So same data, but different addresses.

    But where these array variables will reside? Well, that depends.

    • If they are local, non static, variables: in the stack.
    • If they are global variables: then they will be in the .data section of the executable, and they will be saved there with the correct characters already in there, so no memcpy is needed in run time. Which is nice, because that memcpy would have to be executed before main.
    • If they are local static variables: exactly the same than with global variables. They both together are called variables of static duration or something like that.

    About the documentation links, sorry, I don’t know of any.

    But who needs documentation if you can do the experiments yourself? For that the best tool around is objdump, it can disassemble the program, dump the data sections and much more!

    I hope this answer your questions…

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

Sidebar

Related Questions

Is the following program a valid C program? #include <stdio.h> int main() { fwrite(x,
I have a c program #include <stdio.h> int main () { printf(Hello); } On
if I compile (under G++) and run the following code it prints Foo::Foo(int). However
Am trying to run the following python program import re regex=re.compile(http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]) f=open('out.txt') for a
I have the following program: ~/test> cat test.cc int main() { int i =
I have following code snippet that i use to compile class at the run
When I compile the following code, I only see the error during Run-time which
How do I compile and run a program in Java on my mac? I'm
When I compile and run a simple Win32 GUI program in MinGW+MSys with command
I just built a cross compiler using crosstools mips-unknown-linux-gnu-gcc and I compiled a hello

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.