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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:37:22+00:00 2026-05-15T01:37:22+00:00

Could someone please explain the difference in how the 2 snippets of code are

  • 0

Could someone please explain the difference in how the 2 snippets of code are handled below? They definitely compile to different assembly code, but I’m trying to understand how the code might act differently. I understand that string literals are thrown into read only memory and are effectively static, but how does that differ from the explicit static below?

struct Obj1
{
    void Foo()
    {
        const char* str( "hello" );
    }
};

and

struct Obj2
{
    void Foo()
    {
        static const char* str( "hello" );
    }
};
  • 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-15T01:37:23+00:00Added an answer on May 15, 2026 at 1:37 am

    With your static version there will be only one variable which will be stored somewhere and whenever the function is executed the exact same variable will be used. Even for recursive calls.

    The non-static version will be stored on the stack for every function call, and destroyed after each.

    Now your example is a bit complicated in regards to what the compiler actually does so let’s look at a simpler case first:

    void foo() {
        static long i = 4;
        --i;
        printf("%l\n", i);
    }
    

    And then a main something like this:

    int main() {
        foo();
        foo();
        return 0;
    }
    

    will print

    3
    2
    

    whereas with

    void foo() {
        long i = 4;
        --i;
        printf("%l\n", i);
    }
    

    it will print

    3
    3
    

    Now with your example you have a const, so the value can’t be changed so the compiler might play some tricks, while it often has no effect on the code generated, but helps the compiler to detect mistakes. And then you have a pointer, and mind that the static has effects on the pointer itself, not on the value it points to. So the string “hello” from your example will most likely be placed in the .data segment of your binary, and just once and live as long as the program lives,independent from the static thing .

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

Sidebar

Related Questions

Could someone please explain the difference between the following two lines of code: 1.
Wondering if someone could please explain the difference between these two queries and advise
I'm a little lost in general, but if someone could please briefly explain why
Could someone please explain the Objective-C difference between myString and anotherString in the following
Could someone please explain the difference? From what I understand, I could expose my
Could someone please explain the significant difference in speed between a firefox updatepanel async
Please could someone explain me what's the difference between public and published class members
Could someone please explain to me the difference between the combo box that's available
What's the difference between Activity.runOnUiThread and View.post , could someone, please, explain?
Could someone please explain what's the difference between inheriting from ISerializable interface and declaring

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.