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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:04:30+00:00 2026-06-14T15:04:30+00:00

I have a arduino project and I created this struct: struct Project { boolean

  • 0

I have a arduino project and I created this struct:

struct Project {
  boolean         status;
  String          name;
  struct Project* nextProject;
};

In my application I parse some data and create Project objects. To have them in a list there is a pointer to the nextProject in each Project object expect the last. This is the code where I add new projects:

void RssParser::addProject(boolean tempProjectStatus, String tempData) {
  if (!startProject) {
    startProject = true;

    firstProject.status      = tempProjectStatus;
    firstProject.name        = tempData;
    firstProject.nextProject = NULL;

    ptrToLastProject = &firstProject;
  } else {

    ptrToLastProject->nextProject = new Project();

    ptrToLastProject->nextProject->status      = tempProjectStatus;
    ptrToLastProject->nextProject->name        = tempData;
    ptrToLastProject->nextProject->nextProject = NULL;

    ptrToLastProject = ptrToLastProject->nextProject;
  }
}

firstProject is an private instance variable and defined in the header file like this:

Project firstProject;

So if there actually no project was added, I use firstProject, to add a new one, if firstProject is set I use the nextProject pointer.

Also I have a reset() method that deletes the pointer to the projects:

void RssParser::reset() { 
  delete ptrToLastProject;
  delete firstProject.nextProject;

  startProject = false;
}

After each parsing run I call reset() the problem is that the memory used is not released. If I comment out the addProject method there are no issues with my memory. Someone can tell me what could cause the memory leak?

  • 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-14T15:04:32+00:00Added an answer on June 14, 2026 at 3:04 pm

    First of all, you do not need the variable startProject – simply initialize the firstProject pointer with NULL and then write your condition like this:

    if (firstProject)
    {
        // There is a project, so append the new one.
    }
    else
    {
        // There is no project, so we need to create a new list.
    }
    

    The value FALSE is defined as 0, just as NULL. If firstProject is NULL the expression would look like if (FALSE) and continue execution inside the else-block.

    So now your reset-Method needs to free the memory allocated for all projects, not only the last one and the second one, as your code does.

    delete ptrToLastProject; // Free last project
    delete firstProject.nextProject; // Free the project following to the first one.
    

    The problems in here are:

    • what if ptrToLastProject == firstProject.nextProject? The second delete-statement would free already released memory.
    • firstProject gets never released
    • the projects between the second and the last ones will never be released.

    The best way to free a singly linked list is something like this:

    Project* pProject = firstProject;
    Project* pProjectToDelete;
    
    while (pProject)  // As long as the pointer points to something (see the first comment)
    {
        pProjectToDelete = pProject;
        pProject = firstProject->nextProject;
        delete pProjectToDelte;
    }
    

    In this implementation you are “walking” through the list, releasing the predecessing element, as long as there is an element following. If the next element is NULL, the last element has been released and the loop breaks.

    Last but not least you need to reset the pointer to the first element (also called “anchor” in the terms of data structures):

    firstProject = NULL;
    

    this ensures that addProject does not try to append an project to NULL.

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

Sidebar

Related Questions

I have an Arduino that is processing a string by splitting it into an
I have been trying to get my Arduino/Eclipse environment setup. For some reason I
I have a makefile for compiling Arduino programs . I need to add some
I have quite a big Arduino project (in eclipse) going on with a lot
I am fetching data from the following database: I have arduino-box that send that
I have to connect the Arduino board I got with Java and get data
I have a robotics type project with an Arduino Uno , and to make
For my project I have an Android phone and an Arduino device that communicate
I have a Python script that writes data packets to an Arduino board through
I have an arduino program where I want to store data in a dynamic

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.