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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:17:36+00:00 2026-06-13T11:17:36+00:00

I am computing properties of boundary layer flow through a duct. I have a

  • 0

I am computing properties of boundary layer flow through a duct. I have a class CChannel which stores geometry of the duct, CFlow which holds global properties of the fluid and CNode which stores local parameters of the boundary layer.
When I execute the program in the current form the first element of the GridPoints vector (variable “alpha”) inside CChannel is assigned the same memory location as Uinf which is a private member of the CFlow class. When I change the latter class so that the fields it holds are no longer pointers but regular variables the problem disappears. I also tried reserving memory space for the GridPoints vector inside the class constructor but without any effect. When I was searching for the answer I found that this may have been caused by the in-built code optimiser but didn’t manage to learn anything else. (If this is so, how can I get around this without losing efficiency?)I am guessing the problem arises because of the differences between two different memory allocation modes (heap vs stack). I would still like to find out why is this happening exactly so I can store the global flow parameters as pointers and avoid this problem in the future.

Program.cpp

#include <iostream>

#include "Channel.h"    // stores the channel geometry
#include "Flow.h"   // stores the fluid properties and free stream data
#include "Node.h"       // holds the local BL flow properties, e.g. BL thickness, lambda, etc.

using namespace std;

int main(void)
{
    int NoNodes=21;
    CChannel MyChan(4, 1.2, .8);    // L, h1, h2
    MyChan.MeshUniform(NoNodes);

    CFlow Flow1(.5,1.529e-5,1.19);  // Uinf, niu, ro

    for (int i=0;i<NoNodes;i++)
    {
        MyChan.GridPoints->at(i).GetAlpha();
    }
    return(0);
}

Node.h

#pragma once
class CNode
{
public:
    double *alpha, *x, *lambda; // properties dependent on the Pollhausen velocity profile

    CNode(void);
    ~CNode(void);

    void GetAlpha(void);    // calculates alpha

};

Node.cpp

#include "Node.h"
#include <iostream>

CNode::CNode(void)
{
    alpha=new double;

    lambda=new double;
    *lambda=0;
}

CNode::~CNode(void)
{
    delete alpha, x, lambda;
}

void CNode::GetAlpha(void)
{
    *alpha=(.3-*lambda/120.);
}

Flow.h

#pragma once
class CFlow
{
private:
    double *Uinf, *niu, *ro;
public:
    CFlow(double, double, double);
    ~CFlow(void);
};

Flow.cpp

#include "Flow.h"

CFlow::CFlow(double u, double visc, double den)
{
    Uinf=new double;
    niu=new double;
    ro=new double;
    *Uinf=u;    // free stream velocity (assumes the inflow is parallel to the channel's CL) [m/s]
    *niu=visc;  // kinematic viscosity of the fluid [m^2/s]
    *ro=den;    // density of the fluid [kg/m^3]
}


CFlow::~CFlow(void)
{}

Channel.h

#pragma once
#include <vector>

#include "Node.h"

class CChannel
{
public:
    double *L, *h1, *h2;    // h1 & h2 defined from the CL => make use of the problem assumed to be symmetric
    std::vector<CNode> *GridPoints; // stores data for each individual grid point

    CChannel(double, double, double);
    ~CChannel(void);

    void MeshUniform(int);  // creates a uniform distribution of nodes along the length of the channel
};

Channel.cpp

#include "Channel.h"

CChannel::CChannel(double length,double height1,double height2)
{
    L=new double;   // allocate memory
    h1=new double;
    h2=new double;
    GridPoints = new std::vector<CNode>;

    *L=length;      // assign input values
    *h1=height1;
    *h2=height2;
}


CChannel::~CChannel(void)
{
    delete L, h1, h2, GridPoints; // delete all the members of the class
}


void CChannel::MeshUniform(int NoNodes)
{
    GridPoints->resize(NoNodes);    // resize the vector
    double dx=*L/(NoNodes-1);   // increment of length between each pair of nodes
    for (int i=0; i<NoNodes; i++)
        *GridPoints->at(i).x=0.+i*dx;   // assign the location to each node
}
  • 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-13T11:17:37+00:00Added an answer on June 13, 2026 at 11:17 am

    As already described you do not need all these pointers – change them to be raw variable.

    If you some day come to the situation where pointers are needed then remember of Rule of Three: What is The Rule of Three?.

    You broke this rule by not defining copy constructor and assignment operator in your classes, like in this particular class:

    class CNode
    {
    public:
        double *alpha, *lambda; // properties dependent on the Pollhausen velocity profile
    
        CNode(void);
        ~CNode(void);
    
        void GetAlpha(void);    // calculates alpha
    
    };
    

    You are using this CNode in std::vector<CNode> – so there you are suffering from wrong copying of this CNode object.

    So you need to add copy constructor and assignment operator – then your problem should disappear even if you will be still using pointers, like in this simple example class:

    class Example {
    public:
      Example() : p(new int()) {}
      ~Example() { delete p; }
      Example(const Example& e) p(new int(e.p?*e.p:0)) {}
      Example& operator = (Example e)
      {
          std::swap(e.p, p);
          return *this;
      }
    private:
      int* p;  
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which has some properties of type List<float> , List<int> etc.
I have two objects (instances of the same class) with a bunch of properties,
The graph is composed of many vertices each of which have some properties, and
Possible Duplicate: Comparing object properties in c# Let's say I have a POCO: public
I have a property EntityID in a class. Resharper (5.1) says Name 'EntityID' does
I have a makefile that depending on some properties sets vpath and generates a
I am trying to create a value template class, where additional properties can be
I have a modeless dialog, which is owned by the main window and snapped
I want to create several properties on the fly inside the class' constructor and
I have some components declared in MXML tags, with some properties and event handlers.

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.