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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:44:20+00:00 2026-06-04T23:44:20+00:00

Apart from global and static data what else are allocated on Data segment ?

  • 0

Apart from global and static data what else are allocated on Data segment ?

I remember reading somewhere that constant strings are also allocated on Data segment and same memory is used when a reference to same string constant is made

ex:

char* returnPointer()
{
   char *p = "hello world"

   //some code

   return p;
}

void foo()
{
   char *s = "hello world"
  //some code
}

In the above code,

  1. Does the memory for constant “hello world” allocated on Data segement or stack (just like any other local variable) ?

  2. If allocated on Data segment, does both p and s point to same location ?

  • 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-04T23:44:21+00:00Added an answer on June 4, 2026 at 11:44 pm

    Does the memory for constant “hello world” allocated on Data segement or stack (just like any other local variable) ?

    This is compiler-specific, but typically string literals live on the initialized data segment (often a special read-only portion of the initialized data segment). I don’t know of any architecture where string literals could plausibly live on the stack.

    If allocated on Data segment, does both p and s point to same location ?

    That’s up to the compiler.

    When I compile your code using gcc 4.4.6, I see the following:

            .section        .rodata
    .LC0:
            .string "hello world"
            .text
    .globl returnPointer
            .type   returnPointer, @function
    returnPointer:
    .LFB0:
            .cfi_startproc
            ...
            movq    $.LC0, -8(%rbp)
            ...
            ret
            .cfi_endproc
    .LFE0:
            .size   returnPointer, .-returnPointer
    .globl foo
            .type   foo, @function
    foo:
    .LFB1:
            .cfi_startproc
            ...
            movq    $.LC0, -8(%rbp)
            ...
            ret
            .cfi_endproc
    .LFE1:
            .size   foo, .-foo
            .ident  "GCC: (GNU) 4.4.6 20110731 (Red Hat 4.4.6-3)"
    

    Here, the string literal is stored in a read-only data (.rodata) section. The compiler is smart enough to realize that exactly the same literal is being used twice in the compilation unit, and it puts only one copy of it in .rodata.

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

Sidebar

Related Questions

Apart from the global.css i'm including in my header.php , i would also like
Apart from lxml , is anyone aware of Python packages that depend on libxml2
Apart from the fact that HashSet does not allow duplicate values, what is the
Apart from not cluttering the global namespace and gaining the ability to encapsulate code
We have an application that has multiple identical databases apart from customer customizations. we
Apart from Dll concept that provides ability of loading/unloading methods or functions at run-time,
Apart from Sql server profiler ,is there any third party user friendly tool available
Apart from main thread, I've ThinkerThread object _thinker whose finished() signal is connected to
Apart from jQuery, which other JavaScript libraries support Visual Studio2005/2008 intellisense integration?
Apart from using image on the button, how can I write X Y on

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.