Array memory Allocation doesn't work
I saw the following code and found that it doesn't compile.
Is the code in the OP correct?
Thank you
class A {
};
class B : public A {
int num;
};
int main() {
/* Original Post
error: expected initializer before ‘*’ token
A vec*[5] = new B*[5];
A vec*[5] = new B*[5]; // <<< I don't understand this line
for(int i = 0; i < 5; i++)
{
vec[i] = new B();
}
*/
// My modified version
A* vec[5];
for(int i = 0; i < 5; i++)
{
vec[i] = new B();
}
return 0;
}
Related
I am learning about classes in C++ and I created a simple one that just creates an interval from int a to int b, using a dynamic int array. Here's the code:
Interval::Interval() {
a_ = 0;
b_ = 0;
interval = new int[2];
for (int i = 0; i <= 1; ++i) {
interval[i] = 0;
}
}
Interval::Interval(int a, int b) {
if (a > b) Interval(); // doesn't seem to work?
else if (a == b) {
a_ = a;
b_ = b;
interval = new int[2];
for (int i = 0; i <= 1; ++i) {
interval[i] = a;
}
} else {
a_ = a;
b_ = b;
int size = b - a + 1;
interval = new int[size];
for (int i = 0; i < size; ++i) {
interval[i] = a++;
}
}
}
Interval::~Interval() {
delete[] interval;
cout << "Destructed\n";
}
However, on this part here:
if (a > b) Interval();
It doesn't seem to create it. Where am I wrong?
Edit: As answered by molbdnilo, I was simply creating a separate object inside of the scope of the constructor without assigning or doing anything with it.
Noting that your default constructor is equivalent to Interval(0,0), you can reuse the non-default constructor instead, by forwarding to it:
Interval::Interval() : Interval(0,0) {}
Interval::Interval(int a, int b)
: a_(a <= b ? a : 0),
b_(a <= b ? b : 0)
{
if (a_ == b_) {
interval = new int[2] {a_, a_};
} else {
int size = b_ - a_ + 1;
interval = new int[size];
for (int i = 0; i < size; ++i) {
interval[i] = a_ + i;
}
}
}
This line
if (a > b) Interval();
when the condition is met, it default constructs a temporary Interval which is destroyed at the end of the line.
Your code looks rather compilcated for what it does. The array should rather be a std::vector such that you need not mess around with manual memory allocations. Anyhow, to get what you want "I want default constructed object in that case." the easy fix is the following:
if (a > b) {
a = 0;
b = 0;
}
if (a == b) {
a_ = a;
b_ = b;
interval = new int[2];
for (int i = 0; i <= 1; ++i) {
interval[i] = a;
}
} else { ...
I have to develop a program which checks if some undirected graph is bipartite in C++ as a project exam. I have created all necessary functions but I faced issue I do not clearly understand.
I have some custom struct:
struct slistEl
{
slistEl* next;
int v;
};
Then I have all logic which checks if the graph previously taken from the .txt file is bipartite:
bool isBipartite(int n, slistEl** A)
{
queue Q;
int* C;
int v, u, i;
slistEl* p;
C = new int[n];
for (i = 0; i < n; i++) C[i] = 0;
for (i = 0; i < n; i++)
if (!C[i])
{
C[i] = 1;
Q.push(i);
while (!Q.empty())
{
v = Q.front();
Q.pop();
for (p = A[v]; p; p = p->next)
{
u = p->v;
if (C[u] == C[v])
{
delete[] C;
return false;
}
if (!C[u])
{
C[u] = -C[v];
Q.push(u);
}
}
}
}
delete[] C;
return true;
}
void continueWork(vector<int[2]> verticles) {
slistEl* p, * r, ** A;
A = new slistEl * [getVerticlesCount(verticles)];
for (int i = 0; i < getVerticlesCount(verticles); i++) A[i] = NULL;
for (int i = 0; i < verticles.size(); i++)
{
slistEl v1;
v1.v = verticles[i][0];
slistEl v2;
v2.v = verticles[i][1];
p = new slistEl;
p->v = v2.v;
p->next = A[v1.v];
A[v1.v] = p;
p = new slistEl;
p->v = v1.v;
p->next = A[v2.v];
A[v2.v] = p;
}
}
Function getVerticalCounts has no impact for this issue- it counts verticals from the input file.
Then, I receive following issue which I tried to resolve like here in this issue but It did not help me in any way: Error in converting argument from 'const_Ty' to const custom struct
Errors I receive:
'==': no conversion from 'const _Ty' to 'int [2]'
an array cannot be initialized with a parenthesized initializer
Could someone explain to me specifically what this problem is and potentially how to fix it? I would be so grateful.
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;
}
I have a class that looks like this:
class testy {
int *arr1, *arr2, *resu;
int n;
testy() {
n = 100000000;
arr1 = new int[n];
arr2 = new int[n];
resu = new int[n];
for (int i = 0; i < n; i++) {
arr1[i] = i;
arr2[i] = -i;
}
}
void worker(int Idx) {
resu[Idx] = arr1[Idx] + arr2[Idx];
}
void doTest() {
parallel_for(0, n, worker);
}
};
however I cant compile it since "testy::worker function call missing argument list"
the test worked fine while the worker wasn't a member of the class so I'm guessing I need to specify class instance here or something? How would I do that?
so my question is, why is it not allowed, to cast B*** to A*** in this case?
When i manually cast arr to (A**) this code works fine.
static const int N = 2;
class A {
public:
virtual ~A() {
}
virtual void bla() {
cout << 'A' << endl;
}
};
class B: public A {
public:
~B() {
}
void bla() {
cout << 'B' << endl;
}
};
void callBla(A** arr) {
for (int i = 0; i < N; i++) {
arr[i]->bla();
}
}
int main() {
B** arr = new B*[N];
for (int i = 0; i < N; i++) {
arr[i] = new B[N];
}
callBla(arr);
return 0;
}
I couldn't find anything about this case yet, thanks for your answers in advance!
It's not typesafe; you could do
void callBla(A** arr) {
for (int i = 0; i < N; i++) {
arr[i] = new A;
}
}
and then your array of B* wouldn't be an array of B* any more.
or even
void callBla(A** arr) {
for (int i = 0; i < N; i++) {
*arr[i] = A();
}
}
and then none of your Bs would be Bs any more.