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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:52:17+00:00 2026-05-21T08:52:17+00:00

I’ve been working on a school assignment that makes pretty heavy use of vectors,

  • 0

I’ve been working on a school assignment that makes pretty heavy use of vectors, sets, stacks and queues.

What is the difference between Foo and Bar, especially when passing Foo and Bar between functions? When would it be safe to call delete on Bar, if ever? I’m guess it’s never safe to call delete on Bar unless everything in that vector has been moved? If I return Foo, wont it (and its contents) be deleted when the function exits?

vector<Line *> Foo;

and:

vector<Line *> * Bar = new vector<Line *>();

Similary, lets say I have a function:

vector<Line *> BSTIndex::query(string expression)
{
    vector<Line *> result; //Holds the lines that match the expression
    string query = expression;
    queue<string> * output = expressionParser(query);
    doSomeStuffWithOutputHere();
    return result;
}

And my expressionParser is:

queue<string> * BSTIndex::expressionParser(string query)
{
    char * cQuery = new char[100];
    strcpy_s(cQuery, 100,query.c_str());
    //strcpy(cQuery, query.c_str());
    queue<string> * output = new queue<string>(); //Operators go in the queue
    stack<string> * s = new stack<string>();  //Operands go on the stack
    performSomeMagicOnQueueAndStackHere();
    return output;
}

The stack is actually only local to expressionParser so I KNOW I can remove the new keyword from that. The queue however, needs to go back to the query function where it’s used but then that’s it. Do I HAVE To create a pointer to the queue in this case (I want to say yes because it’s going to fall out of scope when expressionParser returns). If I need to create the pointer, then I should call a delete output in my query function to properly get rid of the queue?

My last concern is the vector being returned by query. Should that be left as I have it or should it be a pointer and what’s the difference between them? Once I consume that vector (display the information in it) I need it to be removed from memory, however, the pointers contained in the vector are still good and the items being pointed to by them should not be deleted. What’s the best approach in this case?

What happens to the contents of containers when you call delete on the container? If the container held pointers, and those pointers are deleted won’t that affect other areas of my program?

  • 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-21T08:52:17+00:00Added an answer on May 21, 2026 at 8:52 am

    SO many questions (direct and indirect) crammed into such a small space!

    What is the difference between Foo and Bar.

    vector<Line*>    Foo;
    vector<Line*>*   Bar = new vector<Line*>();
    

    Foo is an object of automatic (potentially static (but the distinction is not important here)) storage duration. This means it is created at the point of declaration (constructors called) and destroyed when it goes out of scope (destructors called).

    Bar on the other hand is a pointer. Pointer have no constructors or destructors and are used to point at other objects (or NULL). Here Bar is initialized to point at an object of dynamic storage duration. This is an object that has to be manually released (otherwise it will leak).

    especially when passing Foo and Bar between functions?

    When passing Foo (by value) to/from functions the copy constructor is used to make a copy of the original object. Note: The standard explicitly states that copying from a function (via return) can be elided (look up RVO/NRVO) and all modern compilers will remove the extra copy construction and build in place at the return site (but this is an optimization invisible to the user just think of it as a very efficient copy out of a function). The resut of this is that when passed into a function (by value) or returned from a function (by return) you are working on a new object (not the original). This is important because of the side effects of using the object will not affect the original. (see your question below about returning Foo).

    When passing Bar (by value) the pointer is copied. But this means that what is pointed at is the same. So modifying an object via the pointer is modifying the original value. This makes passing it as a parameter very cheap (because all you are passing is the address of the object). But it makes returning a value potentially dangerous, this is because you could return the address of an object that has gone out of scope inside the function.

    Using pointers in inherently dangerous and modern C++ programs rarely use RAW pointers directly. Pointers are usually wrapped up inside objects that manage the ownership and potentially lifespan of the object (see smart pointers and containers).

    When would it be safe to call delete on Bar, if ever?

    It is only safe to delete Bar if:

    • It is NULL
    • The object it points at has been dynamically allocated via new.

    I’m guess it’s never safe to call delete on Bar unless everything in that vector has been moved?

    It would safe to delete Bar even if it’s content had not been moved (though you could leak if the contained pointers were owned by Bar (this is not directly unsafe but can be inconvenient when you run out of space)). This is another reason pointers are rarely used directly, there are no ownership semantics associated with a pointer. This means we can not tell if Bar owns the pointers it holds (it is the responsibility of the owner to call delete on a pointer when it is no longer used).

    If I return Foo, wont it (and its contents) be deleted when the function exits?

    Yes. But because you return an object it will be copied out of the function. So what you use outside the function is a copy of Foo (Though optimizers may elide the copy because of RVO/NRVO. Not if the copy is elided the destructor is also elided).

    Similary, lets say I have a function: query and expressionParser:

    vector<Line *> BSTIndex::query(string expression)
    {
        vector<Line *> result; //Holds the lines that match the expression
        string query = expression;
        queue<string> * output = expressionParser(query);
        doSomeStuffWithOutputHere();
        return result;
    }
    
    queue<string> * BSTIndex::expressionParser(string query)
    {
        char*    cQuery = new char[100];
        strcpy_s(cQuery, 100,query.c_str());
        queue<string> * output = new queue<string>(); //Operators go in the queue
        stack<string> * s = new stack<string>();  //Operands go on the stack
        performSomeMagicOnQueueAndStackHere();
        return output;
    }
    

    The stack is actually only local to expressionParser so I KNOW I can remove the new keyword from that.

    Not only can you but you should. As currently you are leaking this object when it goes out of scope.

    The queue however, needs to go back to the query function where it’s used but then that’s it. Do I HAVE To create a pointer to the queue in this case (I want to say yes because it’s going to fall out of scope when expressionParser returns).

    No. You can pass the obejct back by value. It will be correctly copied out of the function. If you find that this is expensive (unlikely) then you could create a dynamic object with new and pass it back as a pointer (but you may want to look at smart pointers). Remember that pointers do not indicate that you are the owner so it is unclear who should delete a pointer (or even if it should be deleted). So (if passing a value is too expensive) use a std::auto_ptr<> and pass the pointer back inside it.

    If I need to create the pointer, then I should call a delete output in my query function to properly get rid of the queue?

    Yes. And No. If you create a dynamic object with new. Then somebody must call delete on it. It is bad C++ style to do this manually. Learn to use smart pointers so that it is done automatically and in an exception safe manor.

    My last concern is the vector being returned by query. Should that be left as I have it or should it be a pointer and what’s the difference between them?

    I would do it like this:

    vector<Line> BSTIndex::query(string const& expression)
    {
        vector<Line>    result;       // 1 Keep line objects not pointers.
        queue<string>   output  = expressionParser(expression);
    
        doSomeStuffWithOutputHere();
    
        return result;
    }
    
    queue<string> BSTIndex::expressionParser(string const& query)
    {
        char    cQuery[100];               // Don't dynamically allocate
                                           // unless somebody takes ownership.
                                           // It would probably be better to use string or vector
                                           // depending on what you want to do.
    
        strcpy_s(cQuery, 100, query.c_str()); // std::string can use the assignment operator.
    
        queue<string>   output;            // Just build the que and use it.
        stack<string>   s;                 // Unless there is a need. Just use a local one.
    
        performSomeMagicOnQueueAndStackHere();
    
        return output;
    }
    

    Once I consume that vector (display the information in it) I need it to be removed from memory, however, the pointers contained in the vector are still good and the items being pointed to by them should not be deleted. What’s the best approach in this case?

    Depends who owns the pointers.

    What happens to the contents of containers when you call delete on the container?

    If the content of the containers are pointers. Then nothing. The pointers disappear (what they point at is unchanged and unaffected). If the container contains objects (not pointers) then the destructor is called on the objects.

    If the container held pointers, and those pointers are deleted won’t that affect other areas of my program?

    It would. But you would have to call delete manually.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.