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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:36:27+00:00 2026-06-14T09:36:27+00:00

I am in the beginning stages of writing a fairly large code. I have

  • 0

I am in the beginning stages of writing a fairly large code. I have defined one class as such:

class GPUMD {
    private:
        double xhi, xlo, yhi, ylo, zhi, zlo;
        int numAtoms;
        Atom *atoms;
    public:
        GPUMD();
        ~GPUMD();
};

The destructor is defined as below:

GPUMD::~GPUMD() {
    if(atoms != NULL)
        delete [] atoms;
}

Right now, the code does this:

int main(int argc, char *argv[]) {
    GPUMD gpumd;
    exit(0);
}

I get a glibc detected error: trying to free invalid pointer. Using valgrind, I see that this error traces to my destructor for GPUMD. For some reason, the atoms != NULL test is returning true even though I have not assigned anything to that pointer. Why is that?

EDIT: The constructor is defined as:

GPUMD::GPUMD() {}
  • 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-14T09:36:28+00:00Added an answer on June 14, 2026 at 9:36 am

    Because atoms has not been explicitly initialised to NULL or a valid pointer in the constructor. Change constructor to:

    GPUMD::GPUMD() : numAtoms(0), atoms(NULL) {}
    

    Note that the atoms != NULL check prior to the delete[] is superfluous as delete[], or delete, on a NULL pointer is no-op. The following is safe:

    GPUMD::~GPUMD() {
        delete [] atoms;
    }
    

    As there is a dynamically allocated member in GPUMD you need to prevent copying of instances of GPUMD or implement the assignment operator and copy constructor (see What is The Rule of Three?).

    As C++, consider using a vector<Atom> (or a vector of smart pointers) instead which will manage dynamic memory for you.

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

Sidebar

Related Questions

I'm in the beginning stages of designing an application using Prism and have a
I'm in the very beginning stages of understanding Django and have started work on
I am currently in the planning stages for a fairly comprehensive rewrite of one
I am just beginning an SQL class and am having issues with one of
I'm new developer in the beginning stages of writing a simple dictionary app for
Newbie question here. I'm in the beginning stages of laying out a site in
I am at a very beginning stage of Scala Learning (I have little understanding
Since beginning to use VB.NET some years ago I have become slowly familiar with
I'm beginning a large scale javascript application with Marionette. A Marionette application has a
I am in the beginning stages of creating an asp.net solution with 3 very

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.