Functions and return const char*

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 <strong>test(false)</strong> 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.


p is a local variable, which you return by value, but points to a <strong>string literal</strong>, 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.


Although p is a local variable, what it points to is not local - it is a compile-time string constant; it is legal to return that constant's address from a function.

t is different, because the compile-time string constant is copied into an automatic storage area, causing an undefined behavior on dereferencing the returned pointer.


  • Library not loaded- Alamofire
  • promise cancellation still firing fulfill function
  • Safe regexs to clean serialized DOM?
  • No System image available when adding AVD in Android studio
  • Why cout is producing no output on Code Blocks?
  • Where these are stored?
  • Can't remove headers after they are sent
  • How to print columns containing value
  • Undefined references when compiling gSOAP client
  • Spark job failing in YARN mode
  • How can I sort a a table with VBA with given text condition?
  • How to Cache Real-time Data?
  • Is it possible to access block's scope in method?
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Spring Data JPA custom method causing PropertyReferenceException
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Fill an image in a square container while keeping aspect ratio
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Arrays break string types in Julia
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Does armcc optimizes non-volatile variables with -O0?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?