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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:12:26+00:00 2026-06-03T11:12:26+00:00

This is a C++ programming problem. I need to generate a list and return

  • 0

This is a C++ programming problem.

I need to generate a list and return a pointer so that other functions can use the list. The code works but has memory leak because I use “new” to allocate each new node for the list.

After using the list I have to release the memory.

My code is as follows:

#include <iostream>
#include <stack>
#include <memory>
using namespace std;

class linkListClass
{

private:
      int data;
      auto_ptr<linkListClass> nextData;
public:
     auto_ptr<linkListClass> buildLinkList(const int size);
     int getData(){return data;};
     int printListBackward(auto_ptr<linkListClass> listroot);
};

inline auto_ptr<linkListClass> linkListClass::buildLinkList(const int size)
{
            linkListClass *trueRoot;
            linkListClass *listRoot = new linkListClass ;
            linkListClass *trueRoot;
            linkListClass *listRoot = new linkListClass ;
            // data is random int
            for (int i = 0; i < size ; ++i)
            {
                    if (i < size -1)
                    {
                            if (i == 0 )
                            {
                                    listRoot->data = rand()%10 ;
                                    listRoot->nextData = auto_ptr<linkListClass>(0) ;
                                    trueRoot = listRoot ; // transfer ownership
                            }
                            else{
                                    listRoot->nextData = auto_ptr<linkListClass> (new linkListClass );  // segmentation fault 
                                    listRoot->data = rand()%10 ;
                                    listRoot = listRoot->nextData ;
                            }


                    }
                    else
                            listRoot->nextData = auto_ptr<linkListClass>(0) ;

            }

    cout << "the built list has " << size << " data \n\n" ;
    return trueRoot;
 }
 inline int linkListClass::printListBackward(auto_ptr<linkListClass> listroot)
 {
    int counter =0 ;
    stack<int> outputStack;
    cout << "print the list forward \n\n" ;
    //if (listroot != NULL)
    cout << "print the list forward \n\n" ;
    //if (listroot != NULL)
    if (listroot.get() != 0)
    {
            do
            {
                    try{
                            cout << listroot->getData() << " \t " ;
                            outputStack.push(listroot->data);
                            listroot = listroot->nextData;
                            ++counter;
                            cout << "in printListBackward counter is " << counter << endl;
                            //if (listroot == 0 ) break;
                    }
                    catch(exception& e)
                    {
                            cout << "an error is " << e.what() << endl;
                            return 1;
                    }

            //}while(listroot != 0);
            }while(listroot.get() != 0);
            cout << "in printListBackward outof do while \n\n " << endl ;
    }
    else
    {
            cout << "the input list is null \n\n" << endl;
            return 1;
    }
    cout << endl ;
    cout << "there are" << counter << " data in the list \n\n " << endl ;
    cout << "print the list backward \n\n" ;

    if (outputStack.empty() == 1)
    {
            cout << "the ouytput queu is empty \n\n " << endl ;

            cout << "the ouytput queu is empty \n\n " << endl ;
            return 1;
    }
    else
    {
            do
            {
                    cout << outputStack.top() << " \t" ;
                    outputStack.pop();
            }while(outputStack.empty() == 0);
    }
    cout << endl;
    cout << "there are" << counter << " data in the list \n\n " << endl ;
    return 0 ;
  }

  int main()
  {
    const int listSize = 5;
    linkListClass linkListObj;
    auto_ptr<linkListClass> myRoot  ; //= linkListClass::buildLinkList(listSize);
    myRoot = linkListObj.buildLinkList(listSize);
    linkListObj.printListBackward(myRoot);


    return 0;
   }

  // EOF

The code has segmentation fault because the auto_ptr transfer pointee’s ownership so that
after

listRoot = listRoot->nextData 

the linked list is broken and the listRoot->nextData is NULL.

I have tried tr1::shared_ptr and waek_ptr

    tr1::weak_ptr<linkListClass> wp1 = listRoot->nextData;
    listRoot = wp1.lock() ;
    listRoot = listRoot->nextData ;

but I got compile error:

listPtSharedptr.cpp:63: error: conversion from linkListClass* to
non-scalar type std::tr1::weak_ptr requested
listPtSharedptr.cpp:65: error: no match for operator= in listRoot =
listRoot.std::tr1::shared_ptr<_Tp>::operator-> with _Tp =
linkListClass->linkListClass::nextData
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/boost_shared_ptr.h:486: note: candidates are: std::tr1::shared_ptr&
std::tr1::shared_ptr::operator=(const
std::tr1::shared_ptr&)

Any help will be appreciated .

thanks !

  • 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-06-03T11:12:28+00:00Added an answer on June 3, 2026 at 11:12 am

    Here’s a modification to your function “buildLinkList” which should work now.
    With one difference, though. You were creating a FIFO list. This version creates a LIFO list. I guess FIFO is difficult to create with just auto_ptrs. You may try, afterwards, using this working example to use shared_ptrs and convert it to a FIFO list.

    inline auto_ptr<linkListClass> linkListClass::buildLinkList(const int size)
    {
                auto_ptr<linkListClass> trueRoot(0);
                auto_ptr<linkListClass> listRoot(0);
                // data is random int
                for (int i = 0; i < size ; ++i)
                {
                         listRoot = auto_ptr<linkListClass> (new linkListClass );
                         listRoot->data = random()%10 ;
                         listRoot->nextData = trueRoot;
                         trueRoot = listRoot;
                }
        cout << "the built list has " << size << " data \n\n" ;
        return trueRoot;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the problem described in Programming pearls . I can not understand binary
I am faced with the following programming problem. I need to generate n (a,
This is more of a business-oriented programming question that I can't seem to figure
I have this programming problem, I'm using C#. I want to check if varA
I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I
This problem has happened to me twice.Both times during programming with Borland C++.when i
This is not really a programming question, more of an algorithmic one. The problem:
I am having this really annoying problem on a php-site I am programming and
First of all: This question is not directly programming related. However, the problem only
this is not a question about a specific programming problem, it's about examining different

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.