Improper usage of new with inheritance? - c++

I am trying to figure out a crash, and have boiled it down the following sample:
class Base
{
public:
virtual unsigned int GetNum () = 0;
};
class Derived : public Base
{
int data; // <--- commmenting this out removes the crash
public:
Derived() {}
virtual unsigned int GetNum() { return 1; }
};
void func() {
Base** ppBases;
unsigned int xLen = 3;
unsigned int yLen = 4;
ppBases = new Base*[xLen];
for(unsigned int x = 0; x < xLen; ++x) {
ppBases[x] = new Derived[yLen];
for(unsigned int y = 0; y < yLen; ++y) {
Base* curBase = dynamic_cast<Base*>(&(ppBases[x][y]));
Derived* curDerived = dynamic_cast<Derived*>(&(ppBases[x][y])); // <--- crashes with a null dereference
unsigned int result = curBase->GetNum();
result = result;
}
}
}
I guessed that Derived isn't actually being allocated with the proper size. Changing ppBases to be a tripple pointer (like so) makes everything work:
void func() {
Base*** ppBases;
unsigned int xLen = 3;
unsigned int yLen = 4;
ppBases = new Base**[xLen];
for(unsigned int x = 0; x < xLen; ++x) {
ppBases[x] = new Base*[yLen];
for(unsigned int y = 0; y < yLen; ++y) {
ppBases[x][y] = new Derived();
Base* curBase = dynamic_cast<Base*>(ppBases[x][y]);
Derived* curDerived = dynamic_cast<Derived*>(ppBases[x][y]);
unsigned int result = curBase->GetNum();
result = result;
}
}
}
I don't understand why though.
Thanks

The problem is with this line:
ppBases[x] = new Derived[yLen];
You're assigning an array of Derived objects to a Base * pointer. So when you write ppBases[x][y] the compiler works with the type of the pointer, which is Base *, and index appropriately. So ppBases[x][1] does not refer to the second Derived object in the allocated array.

Related

Initialize C++ struct that contains a fixed size array

Suppose I have a POD C struct as so:
struct Example {
int x;
int y[10];
int yLen;
}
With the following code, the program doesn't compile:
Example test() {
int y[10];
int yLen = 0;
auto len = this->getSomethingLength();
for (int i = 0; i < len; i++) {
y[yLen++] = this->getSomething(i);
}
return Example{ 0, y, yLen };
}
However, doing return {0, {}, 0}; does seem to compile. Problem is, I can't know the size of y until doing some sort of logic ahead of time. Initializing int y[10]{} in test doesn't seem to make a difference. I know this seems like a pretty simple question, but I can't seem to find anything that works.
Declare the structure as a whole instead of its parts and then initialize it:
Example test() {
Example result;
auto len = this->getSomethingLength();
for (result.yLen = 0; result.yLen < len; result.yLen++) {
result.y[result.yLen] = this->getSomething(result.yLen);
}
return result;
}
Declaring y as an int* and allocating memory with new, when the size is known, would be an even better solution.
Declare constructor in Example:
struct Example {
int x;
int y[10];
int yLen;
Example(int xNew, int *yNew, int yLenNew)
{
x = xNew;
yLen = yLenNew;
for (int i = 0; i < yLenNew; i++)
{
y[i] = yNew[i];
}
}
};
And use it like this:
Example test() {
int y[10];
int yLen = 0;
auto len = this->getSomethingLength();
for (int i = 0; i < len; i++) {
y[yLen++] = this->getSomething(i);
}
return Example( 0, y, yLen );
}

How to initialize dynamic array inside a class?

I wish to initialize a multidimensional, dynamic array inside a class. But, I am getting an error.
I have seen several examples on the net. They seem to be difficult. I am new to coding. I would like a simple solution if possible.
class myp
{
int ntc = 5;
public:
double** y = new double*[ntc];
for(int i = 0; i < ntc; ++i)
y[i] = new int[3];
};
int main()
{
int x;
myp mp;
mp.y[1][1] = 3;
cout<<mp.y[1][1]<<endl;;
return 0;
}
test.cpp:12:2: error: expected unqualified-id before ‘for’
for(int i = 0; i < ntc; i++)
^~~
test.cpp:12:17: error: ‘i’ does not name a type
for(int i = 0; i < ntc; i++)
^
test.cpp:12:26: error: ‘i’ does not name a type
for(int i = 0; i < ntc; i++)
You need to do class initialisation in the constructor function, and cleanup in the destructor.
class myp
{
int m_numColumns;
int m_numRows;
double** y;
public:
// overload array operators
double* operator [] (size_t row) { return y[row]; }
const double* operator [] (size_t row) const { return y[row]; }
// return dimensions of array
int numColumns() const { return m_numColumns; }
int numRows() const { return m_numRows; }
// constructor
myp(int nc, int nr) : m_numColumns(nc), m_numRows(nr)
{
y = new double*[m_numRows];
for(int i = 0; i < m_numColumns; ++i)
y[i] = new int[m_numColumns];
}
// destructor
~myp()
{
for(int i = 0; i < m_numColumns; ++i)
delete [] y[i];
delete [] y;
}
// be careful of the copy ctor. I'm deleting it in this case!
myp(const myp&) = delete;
// edit: as per user4581301's suggestion
myp() = delete;
myp(myp&&) = delete; // remove move ctor
myp& operator = (const myp&) = delete; // remove assignment
myp& operator = (myp&&) = delete; // remove move assignment
};
int main()
{
myp mp(5, 3);
mp[1][1] = 3;
cout << mp[1][1]<<endl;
return 0;
}
Just For Run.
class myp
{
int ntc = 5;
public:
double **y;
void initArray()
{
y = new double*[ntc];
for(int i = 0; i < ntc; ++i)
y[i] = new double[3]; // i change this line [new int] to [new double]tv
}
};
int main()
{
int x;
myp mp;
mp.initArray();
mp.y[1][1] = 3;
cout<<mp.y[1][1]<<endl;;
return 0;
}
using constructor & destructor
class myp
{
int ntc = 5;
public:
double **y;
myp() // run at created
{
y = new double*[ntc];
for(int i = 0; i < ntc; ++i)
y[i] = new double[3];
}
~myp() // run at the end of life cycle
{
/* free memory here */
}
};
int main()
{
int x;
myp mp; // myp() called
mp.y[1][1] = 3;
cout<<mp.y[1][1]<<endl;
return 0;
}
using constructor with parameter, for dynamic size
class myp
{
// int ntc = 5; // using at created
public:
double **y;
myp(int ntc, int size) // run at created
// if you want to use only myp mp;
// myp(int ntc = 5, int size = 3) {} will be helpful
{
y = new double*[ntc];
for(int i = 0; i < ntc; ++i)
y[i] = new double[size];
}
~myp() // run at the end of life cycle
{
/* free memory here */
}
};
int main()
{
int x;
myp mp(5, 3); // myp(int, int) called
mp.y[1][1] = 3;
cout<<mp.y[1][1]<<endl;
return 0;
}

Segmentation fault at std::thread

typedef struct _ppm_struct* ppm_struct;
typedef unsigned char ppm_subpixel;
typedef ppm_subpixel (*ppm_pixel_func)(ppm_subpixel);
struct ppm_pixel {
ppm_subpixel red;
ppm_subpixel green;
ppm_subpixel blue;
};
struct _ppm_struct {
unsigned int width;
unsigned int height;
unsigned int max_color;
ppm_pixel *firstpixel;
};
So I have this function call:
void pppm_color_pixel(ppm_struct p, ppm_struct out, ppm_pixel_func func, int threads) {
//ppm_pixel_func func2 = &inverse_pixel;
unsigned int w = p->width;
unsigned int h = p->height;
ppm_pixel *iterator = p->firstpixel;
std::thread th[threads];
long long total_pixels = w*h;
unsigned int count_for_thread = total_pixels/threads;
for(int j = 0; j<threads; ++j) {
th[j] = std::thread(pppm_color_chunk_pixel,func, *iterator,count_for_thread);
iterator = &iterator[count_for_thread];
}
for(int j = 0; j<threads; ++j) {
th[j].join();
}
The next two function: funct is inverse subpixel
ppm_subpixel inverse_subpixel(ppm_subpixel subpixel) {
subpixel = 255- subpixel;
return subpixel;
}
void pppm_color_chunk_pixel(ppm_pixel_func func, ppm_pixel start_pixel, unsigned int count) {
ppm_pixel *iterator = &start_pixel;
for(unsigned int i = 0; i< count; ++i) {
iterator[i].red = func(iterator[i].red);
iterator[i].green = func(iterator[i].green);
iterator[i].blue = func(iterator[i].blue);
}
}
The main:
pppm_color_pixel(p,out, &inverse_subpixel,pppm_get_max_cores());
The problem is that when I run this it shows me a segmentation fault. This happen on this line:
iterator[i].green = func(iterator[i].green);
What I really don't understand is that i = 2 or more when this happens. It doesn't crash from the first time. Even if I try to call only a single thread the result is the same.
pppm_color_chunk_pixel accepts a copy of ppm_pixel. Since it is copied by value, passing *iterator to it will cause only one element to be copied. Iterating over anything after &start_pixel will fail.
Since std::thread doesn't pass objects by reference, you also need a reference-wrapper for this.
void pppm_color_chunk_pixel(ppm_pixel_func func, ppm_pixel& start_pixel, unsigned int count) {
ppm_pixel* iterator = &start_pixel;
for (unsigned int i = 0; i < count; ++i) {
iterator[i].red = func(iterator[i].red);
iterator[i].green = func(iterator[i].green);
iterator[i].blue = func(iterator[i].blue);
}
Calling it:
for (int j = 0; j < threads; ++j) {
th[j] = std::thread(pppm_color_chunk_pixel, func, std::ref(*iterator), count_for_thread);
iterator = &iterator[count_for_thread];
}
I have not tested it, but I guess this is your problem.

How to avoid returning pointers in a class

Assume I have a class A that has say 3 methods. So the first methods assigns some values to the first array and the rest of the methods in order modify what is computed by the previous method. Since I wanted to avoid designing the methods that return an array (pointer to local variable) I picked 3 data member and store the intermediate result in each of them. Please note that this simple code is used for illustration.
class A
{
public: // for now how the class members should be accessed isn't important
int * a, *b, *c;
A(int size)
{
a = new int [size];
b = new int [size];
c = new int [size];
}
void func_a()
{
int j = 1;
for int(i = 0; i < size; i++)
a[i] = j++; // assign different values
}
void func_b()
{
int k = 6;
for (int i = 0; i < size; i++)
b[i] = a[i] * (k++);
}
void func_c()
{
int p = 6;
for int (i = 0; i < size; i++)
c[i] = b[i] * (p++);
}
};
Clearly, if I have more methods I have to have more data members.
** I'd like to know how I can re-design the class (having methods that return some values and) at the same time, the class does not have the any of two issues (returning pointers and have many data member to store the intermediate values)
There are two possibilities. If you want each function to return a new array of values, you can write the following:
std::vector<int> func_a(std::vector<int> vec){
int j = 1;
for (auto& e : vec) {
e = j++;
}
return vec;
}
std::vector<int> func_b(std::vector<int> vec){
int j = 6;
for (auto& e : vec) {
e *= j++;
}
return vec;
}
std::vector<int> func_c(std::vector<int> vec){
//same as func_b
}
int main() {
std::vector<int> vec(10);
auto a=func_a(vec);
auto b=func_b(a);
auto c=func_c(b);
//or in one line
auto r = func_c(func_b(func_a(std::vector<int>(10))));
}
Or you can apply each function to the same vector:
void apply_func_a(std::vector<int>& vec){
int j = 1;
for (auto& e : vec) {
e = j++;
}
}
void apply_func_b(std::vector<int>& vec){
int j = 6;
for (auto& e : vec) {
e *= j++;
}
}
void apply_func_c(std::vector<int>& vec){
// same as apply_func_b
}
int main() {
std::vector<int> vec(10);
apply_func_a(vec);
apply_func_b(vec);
apply_func_c(vec);
}
I'm not a big fan of the third version (passing the input parameter as the output):
std::vector<int>& func_a(std::vector<int>& vec)
Most importantly, try to avoid C-style arrays and use std::vector or std::array, and don't use new, but std::make_unique and std::make_shared
I'm assuming you want to be able to modify a single array with no class-level attributes and without returning any pointers. Your above code can be modified to be a single function, but I've kept it as 3 to more closely match your code.
void func_a(int[] arr, int size){
for(int i = 0; i < size; i++)
arr[i] = i+1;
}
void func_b(int[] arr, int size){
int k = 6;
for(int i = 0; i < size; i++)
arr[i] *= (k+i);
}
//this function is exactly like func_b so it is really unnecessary
void func_c(int[] arr, int size){
int p = 6;
for(int i = 0; i < size; i++)
arr[i] *= (p+i);
}
But if you just want a single function:
void func(int[] arr, int size){
int j = 6;
for(int i = 0; i < size; i++)
arr[i] = (i+1) * (j+i) * (j+i);
}
This solution in other answers is better, if you are going to allocate memory then do it like this (and test it!) also if you are not using the default constructor and copy constructor then hide them, this will prevent calling them by accident
class A{
private:
A(const &A){}
A() {}//either define these or hide them as private
public:
int * a, *b, *c;
int size;
A(int sz) {
size = sz;
a = new int[size];
b = new int[size];
c = new int[size];
}
~A()
{
delete[]a;
delete[]b;
delete[]c;
}
//...
};

C++ private member of object is modified when calling constructor again

I've created a class in C++ to deal with arrays of 0s and 1s. In private attributes I have a size, a type and an array of integer for the size specified.
My problem is that the values in the array is being modified every time I call the constructor again. In more detailled, I create a first object with the constructor, then a second one and doing this modifies the first one!
I've tried to play around with pointers, the new operator, const pointers to const object, nothing works! Idependently of the size of array I chose, it's always the third, then the sixth, then the ninth, etc value of the array that is modified to the value of the size.
any suggestion appreciated.
some extracts from my code:
class SArray
{
private:
int SArray_Size;
int DType;
int Table[];
public:
//complete constructor
SArray::SArray(const int& tsize, const int& ttype)
{
SArray_Size = tsize;
DType = ttype;
if (ttype == 0) //random array with integer values between 0 and 1
{
for (int i = 0; i < getSize(); i++)
{
Table[i] = rand() % 2;
}
}
if (ttype == 1) //default array with only 1s
{
for (int i = 0; i < getSize(); i++)
{
Table[i] = 1;
}
}
}
};
int main()
{
const int NbRes = 15;
//reset the random number generator
srand(time(0));
const SArray test3(NbRes,1);
(test3).print();
const SArray test1(NbRes,1);
(test1).print();
(test3).print();
return 0;
}
The culprit is int Table[] - you have not specified how large your table is.
You should really replace it with std::vector<int> Table; and initialize it with tsize.
For instance:
#include <vector>
class SArray
{
private:
int DType;
std::vector<int> Table;
public:
const size_t getSize() const { return Table.size(); }
public:
SArray::SArray(const int tsize, const int ttype) :
DType(ttype), Table(tsize)
{
int i, n = getSize();
switch( ttype )
{
case 0:
for (i = 0; i < n; ++i)
Table[i] = rand() % 2;
break;
case 1:
for (i = 0; i < n; ++i)
Table[i] = 1;
break;
}
}
};
You must allocate memory for "Table". For example:
SArray::SArray(const int& tsize, const int& ttype)
{
SArray_Size = tsize;
DType = ttype;
Table= new int[tsize];
...
Don't forget to free it in destructor.