0

I want the constructor to create a dynamic array and assign arguments passed to the constructor to the members of the array.

A simplified version of what I have in the header:

//in classes.h
#ifndef classes_h
#define classes_h
#include <iostream>
class Base{
        int a,*var;
public:
    Base();
    Base(int);
    ~Base();
    int func(int);
};
#endif

And in the *.cpp:

//In classes.cpp
#incldue "classes.h"
Base::Base(int a){
    var=new int[2];
    var[0]=a;
    var[1]=func(a);
}
Base::~Base(){
        delete var;
}
int Base::func(int b){
    return b++;
}
int main(){
Base obj(1);
return 0;}

I need to be able to pass that array to a function which will modify it in some way, but I'm actually having more trouble defining the array...

I don't get any errors from the compiler or linker, but by debugging I arrived at the conclusion the problem is around the creation of the array.

I went over the basics of pointers, dynamic arrays and classes 3 (or maybe more) times, but to no avail. I'm hoping what I'm trying to do is actually possible. If not, what's the closest thing?

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
wizzard
  • 41
  • 1
  • 8

3 Answers3

1

You're running into undefined behavior because of

var=new int[2];

being freed with

delete var;

instead of delete[] var;.

You should use a std::vector<int> instead. That way, you needn't worry about a destructor/copy constructor/assignment operator.

Also, note that

int Base::func(int b){
    return b++;
}

is basically a no-op, as the previous value of b is returned. Did you mean return b+1?

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • is there really no other way to do it without ::vector? – wizzard Nov 01 '12 at 06:51
  • 2
    @wizzard sure there is, it's called "the hard way". – Luchian Grigore Nov 01 '12 at 06:51
  • There is a multitude of reasons as to why `std::vector` makes dynamics arrays far less painful than hand-coding. Unless you have a solid reason for avoiding it, **don't**. – moshbear Nov 01 '12 at 06:55
  • My reason is to understand why it's not working the way it is right now. I decided to 'refresh' my memories on c++, but it turned out they were all long gone... (the language has become prettier last 4-5 years). Decided to make a simple 3D array, turned out it's not simple at all, looked around, found boost, turns up I have a problem defining the arguments I pass to boost, came here to ask after being unable to find anything related to my problem. Turns out My problem is even more basic than I thought :D – wizzard Nov 01 '12 at 07:00
  • @wizzard 1. you don't tell us exactly where the problem is. 2. Did you notice that `func` does nothing? – Luchian Grigore Nov 01 '12 at 07:02
  • @LuchianGrigore 2 was supposed to be just some function a `def f():pass` in Python if you will. Anyway, It must be what Als suggested, gonna look up the Copy constructor and Copy assignment operator edit my script a bit and report back with the good news or any further problems I encounter :) Thanks for the timely responses :) – wizzard Nov 01 '12 at 07:09
1

You have two problems:

  1. You are not following the Rule of Three.
  2. You have an Undefined Behavior.

Details:

delete var;

should be:

delete []var;

new requires delete and new[] requires delete [], a mismtach results in Undefined Behavior.

Suggestion:

You should be using a std::vector or std::array instead of an dynamic c-style array. It will save you all the efforts and they are less error prone.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

If you don't like vectors in this example you could use a std::pair but a vector is the way to go. I haven't used an array in months.

James
  • 1,764
  • 5
  • 31
  • 49