pointers for a beginner

Hello, I have a simple code where I am trying to store multiple arrays in an object for global retrieval. A simplified version of my cpp code is the following:

-------------------------------------------------------------

#include <iostream>

class cpoint1D
{
public:
void setx(double xr) {itsx = xr;}
void setw(double wr) {itsw = wr;}
double x()const {return itsx;}
double w()const {return itsw;}
private:
double itsx;
double itsw;
};

class ctrlPolygon1D
{
public:
int p;
cpoint1D *Pw;
double *U;

};

//----------------------------------------------------------

int main()
{
int ncp=5;
cpoint1D *Pw = new cpoint1D[ncp];
double *U = new double[nknots]{0.0};
Pw[0].setx(0);
Pw[1].setx(1);
Pw[2].setx(2);
Pw[3].setx(3);
Pw[4].setx(4);

for (int i=0; i<ncp; i++) printf("x: %10.5f \n",Pw[i].x());

ctrlPolygon1D *poly;
poly -> Pw = Pw;

return 0;
}

------------------------------------------------------

As is, this code gives me a segfault. My point is to access both arrays (U and Pw) from the cntrlPolygon1D class. But, when I try to define ->, I get a seg-fault.

Any help is greatly appreciated.
-Corey
You have not initialized poly before you dereference it.

Also, as a matter of style, it appears as though cpoint1D is strictly a value class and contains no real logic. If that is the case, personally I would eschew the setters and getters and just make the data public. After all, the data isn't really hidden.
So this is just a snippet of a much larger multi-file code i am building. The issue is that I needed trans-file variables. I was using extern, but after reading that it is better to encapsulate, I am trying this instead. Knowing this, would you still recommend eschewing the setters and getters?

Also, forgive my noobness ... but in this case, how would one initialize poly?
Thanks!!!
- Don't be afraid to use public data members. For something like a Point struct, there's nothing wrong with having x, and w publically accessible. One of the false assumptions about OOP is that all members need to be protected/private -- having set/get member functions for something like that is a little silly.

- Logically, it would make sense for your polygon to generate and maintain its own points, rather than you supplying the polygon with points to use. After all the points are part of the polygon, and therefore it makes more logical sense to make the points part of the polygon class. This will make your class easier to use and will reduce the occurance of bugs.

- this is your segfault problem:

1
2
ctrlPolygon1D *poly;  // <--- this creates a POINTER, not an object. the pointer points to nothing
poly -> Pw = Pw;  // <--- trying to derefence a pointer that points to nothing = segfault 


A solution is to not use a pointer, but to just make an object directly:
1
2
ctrlPolygon1D poly;
poly.Pw = Pw;


Or dynamically allocate the polygon:
1
2
3
ctrlPolygon1D* poly;
poly = new ctrlPolygon1D;  // make it actually point to something
poly->Pw = Pw;  // no more segfault 


- You have memory leaks! Whenever you call new, there must be a corresponding call to delete. And for new[], you must use delete[]. You're never delete[]'ing your 'Pw' and 'U' arrays.


EDIT -- blah. I'm too slow
Last edited on
[regarding memory leaks] Yeh, I just wrote the code hyperspeed to show the problem - I'm usually on top of the deletes!

Thank you so much for your time. I understood everything you mentioned. You have been wonderful.
-Corey
Topic archived. No new replies allowed.