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

  • Home
  • SEARCH
  • 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 6986987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:52:43+00:00 2026-05-27T18:52:43+00:00

For class I’m creating a shell in C++. It’s been a while since I’ve

  • 0

For class I’m creating a shell in C++. It’s been a while since I’ve worked with C++ so I’m having a bit of trouble. One of the requirements for the project is that I must use the read() system call.

I’m required to keep a command history (similar to bash’s if you press the up arrow) that stores the most recent 20 commands. I feel the best way to do this is to use an array of pointers to previous statements. I’m running into a problem where no matter what I do, the string that contains the user input is always stored in the same location in memory. To clarify, this means, if a user inputs 5 statements and then views his/her history, they will see the most recent statement 5 times. My code looks a little something like this (I have to cut some things out because there is a lot of error handling in the middle):

char *history[20];
int historyCounter = 0;

while(true){
  char currLine[65];
  int charsRead = read(0,currLine,65);

  char tmp[charsRead];
  strcpy(tmp,currLine); //This is my attempt to ensure the char[] is stored int a 
                        //unique location every time, but this attempt failed.

  history[historycounter] = tmp;
  historycounter++;
}

Just to note, in my source, I do handle the case when historycounter > 19. Just not in this snippet.

If any more clarification is required I’d be happy to provide it. This is my first time posting on stack overflow so I apologize in advance if I’m making any sort of rookie mistakes. I also apologize if the solution is painfully obvious. I’ve been looking at this for a while and it is entirely possible that I’m just not thinking straight.

  • 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-27T18:52:44+00:00Added an answer on May 27, 2026 at 6:52 pm

    In this case, tmp is most likely to be in the same location, on stack. Its allocated every iteration, and deallocated when the iteration ends.

    The line

    history[historycounter] = tmp;
    

    Will lead to undefined behavior when you use it because you’ll be using an address of a local variable outside of its scope.

    If you want to ensure unique address (and solve the UB problem) – use new to allocate the memory, and don’t use delete until you’re done. Make sure to keep track of all the allocated pointers and delete them when done.

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

Sidebar

Related Questions

Class is a reference types while Struct is a value type. This means that
class token { private: char m_chIcon; //actual ascii char that shows up for this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class
class C { T a; public: C(T a): a(a) {;} }; Is it legal?

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.