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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:16:56+00:00 2026-06-04T05:16:56+00:00

const char* test(bool i) { const char t[] = aa\n; const char* p =

  • 0
const char* test(bool i)
{
    const char t[] = "aa\n";
    const char* p = "bbb\n";
    if(i)
        return p;
    return t;
}
int main(array<System::String ^> ^args)
{
     printf(test(true));
     printf(test(false));
     return 0;
}

That returns something of sort:

 bbb
 %^&$^$%

It is clear that test(false) returns a pointer to a local variable. The question is that p is also local variable. Why the memory for “bbb\n” is not cleaned after the function returns. I thought const char[] is interpreted same way as const char* but it is not true as it seems.

  • 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-04T05:16:58+00:00Added an answer on June 4, 2026 at 5:16 am

    p is a local variable, which you return by value, but points to a string literal, which resides in read-only memory, not in the automatic memory allocated for the method.

    Returning t and the using it indeed results in undefined behavior.

    Also, don’t think of pointers and arrays to be equivalent.

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

Sidebar

Related Questions

ERROR CODE: CList<CString,const char*> test; ERROR INFO: ECLIPSE / UBUNTU 10 /home/latyas/workspace/CList/Debug/../test.cpp:7: undefined reference
int KMP( const char *original, int o_len, const char *substring, int s_len ){ if(
Consider this code: const char* someFun() { // ... some stuff return Some text!!
Having code: Date::Date(const char* day, const char* month, const char* year):is_leap__(false) { my_day_ =
bool ios::eof ( ) const; According to the library, The function returns true if
char *p=orkut vs const char *p=orkut whats the difference btwn these two... EDIT from
void CApplication::SendData( const char pBuffer[] ) { if( pBuffer == NULL ) { Log()->Write(
Why does: const char example; (uint64*)example have a value of 140734799798420 and *(uint64*)example have
In code: struct Rep { const char* my_data_; Rep* my_left_; Rep* my_right_; Rep(const char*);
-(IBAction)EnterButtonPressed:(id)sender { const char *sql = SELECT AccessCode FROM UserAccess; double result = ([Password.text

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.