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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:13:50+00:00 2026-05-20T02:13:50+00:00

Lets say i have a class TwoWayList that holds Records , and GetRec actually

  • 0

Lets say i have a class TwoWayList that holds Records , and GetRec actually creates a new list on the heap, here is the method

void GetRec(TwoWayList<Record> &rec)
{
   TwoWayList<Record>* list= new TwoWayList<Record>();
   Record r;
   list->Insert(&r);
}

Now i have the follow two scenarios, the first one dies when i call delete, and the other one i just get a null reference to record, so when i call MoveToStart() i get a segfault, however if i just delete it works…

int main () {
    TwoWayList<Record> record;
    GetRec(record);
    record.MoveToStart();
    delete &record;//crash
   return 0;
}

int main () {
    TwoWayList<Record> *record;
    GetRec(*record);
    record->MoveToStart(); //segfault
    delete record;
   return 0;
}

So whats going on here? Im creating a TwoWayList in the heap in the method, therefore shouldnt i be able to delete (in fact wont it be a leak if i dont delete it?) Whats the correct way to get the TwoWayList from the method here in order for me to be able to delete it later?

Thanks

Daniel

  • 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-20T02:13:50+00:00Added an answer on May 20, 2026 at 2:13 am

    Your first main creates record on the stack — not the heap. So your attempt to delete the address of a stack variable crashes.

    Your second main never allocates record at all. So when you try to use the pointer, it segfaults.

    Also, your function allocates memory, but then forgets about it and leaks it. By that I mean that you create a new list, but never hold onto the pointer. Once the function exits, you no longer have a pointer to the list you created — probably not what you wanted to do.

    GetRec also ignores the input parameter — also probably not what you wanted.

    Guessing at what you’re attempting…

    void GetRec(TwoWayList<Record> &rec)
    {
        Record r;
        rec.Insert(r);
    }
    
    int main () {
        TwoWayList<Record> record;
        GetRec(record);
        record.MoveToStart();
        return 0;
    }
    

    This creates a TwoWayList (named record), passes a reference to record to the function GetRec. GetRec creates a Record and Inserts it into the TwoWayList. Back in main, it calls MoveToStart on record (which now has one Record inserted into it).

    This avoids any issues with new/delete by using the stack, at the cost copying Record when you insert it into the TwoWayList. It’s doubtful the performance cost of that copy will matter much to you. But if it does, just say so.

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

Sidebar

Related Questions

Lets say I have a class that contains a list of sorted numbers. The
Lets say I have a class that is supposed to generate some ID (for
Lets say that i have a class with some instance variables and i want
Lets say I have Class A, that calls static methods in either Class B
Lets say I have a class A that inherits from class B in C#.
So lets say I have a class called Post that contains an IList I
Lets say I have a class that is doing something like: public class Foo
Lets say I have a class A and I overload operator new in the
Here's what I'm thinking. So lets say I have a class called intro and
Lets say you have a class MyClass that has a constructor public Myclass(SomeObject o)

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.