Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / Using C-string gives Warning: “Address of stack memory associated with local variable returned”

Using C-string gives Warning: “Address of stack memory associated with local variable returned”

August 19, 2021 by James Palmer

Variable char* matches[1]; is declared on stack, and it will be automatically released when current block goes out of the scope.
This means when you return matches, memory reserved for matches will be freed, and your pointer will point to something that you don’t want to.
You can solve this in many ways, and some of them are:

Declare matches[1] as static: static char* matches[1]; – this
will allocate space for matches in the static space and not on the stack (this may bite you if you
use it unappropriately, as all instances of my_completion function
will share the same matches variable).
Allocate space in the caller function and pass it to my_completion
function: my_completion(matches):
char* matches[1];
matches = my_completion(matches);

// …

char** ReadLineImpl::my_completion (char** matches) {
matches[0] = “add”;

return matches;
}

Allocate space in the called function on heap (using malloc, calloc, and friends) and pass the ownership to the caller function, which will have to deallocate this space when not needed anymore (using free).

When you return the matches array, what you are returning is the address of the first element. This is stored on the stack inside my_completion. Once you return from my_completion that memory is reclaimed and will (most likely) eventually be reused for something else, overwriting the values stored in matches – and yes, that may well be why your application doesn’t work – if it isn’t right now, it probably will be once you have fixed some other problems, or changed it a bit, or something else, because this is not one of those little warnings that you can safely ignore.
You can fix this in a few different ways. The most obvious is to simply use std::vector [or better yet std::vector] instead:
std::vector ReadLineImpl::my_completion ()
{
std::vector strings;
strings.push_back(“add”);
return strings;
}

Edit: So, if the library requires a char ** as per the readline interface,then use this:
char** ReadLineImpl::my_completion ()
{
char **matches = static_castmalloc(1 * sizeof(char *));
matches[1] = “add”;
return matches;
}

Problem solved!

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in