如何使用数组成员将内存分配给类的实例?

时间:2017-04-13 10:51:56

标签: c++

根据我的理解,在通常条件下,数组在编译时被分配内存,但是当数组是成员变量时会发生什么,并且在编译期间几乎没有什么可以分配内存。当创建该类的实例时,是否会动态分配动态分配?

class Arr{
   public:
   int arr[10];
};

Arr doSomething(Arr &arg){
   return arg; //copy of arg created; 'copy=new int[10]' at this point?
}

int main(){
   Arr temp; 
   doSomething(temp);

   //if returned object's array was dynamically initialized
   //will it result the array in temp being released twice?
}

UPD。在我的情况下,当我尝试减去B> A的A-B时情况会上升。在这些情况下,我可以访问数组来读取它,但修改它的值会导致垃圾。而且,main中的最终输出很好。这是完整的代码:

#include <iostream>

using namespace std;

const int MAX_ARRAY_SIZE=256;

char* getOperatorIndex(char* equationString);
bool isOperator(char characterDec);
int* toIntArray(char* firstInA, char* lastInA, int* firstInB);

class Number{
    int* firstInNumber;
    int* lastInNumber;
    int number[MAX_ARRAY_SIZE];

    public:
    Number();
    ~Number();
    bool operator<(Number& b);
    int& operator[](int index);
    Number operator-(Number& b);
    void print();
    int length(){return lastInNumber-firstInNumber;}
    int*& lastPtr(){return lastInNumber;}
    int*& firstPtr(){return firstInNumber;}
};

Number::Number(){
    firstInNumber=number;
    lastInNumber=number;
}

Number::~Number(){
    cout<<"number destroyed"<<endl;
}

int& Number::operator[](int index){
    return number[index];
}

bool Number::operator<(Number& b){
    if(length()>b.length())return false;
    if(length()<b.length())return true;

    for(int a=0;a<length();++a){
        if(number[a]>b[a])return false;
        if(number[a]<b[a])return true;
    }
    return false;

}

void Number::print(){
    for(int a=0; a<=length(); ++a){
        cout<<number[a];
    }
    cout<<endl;
}

Number Number::operator-(Number& b){

    Number result;

    if(*this < b)
    {
        result=b-*this;
        cout<<*result.lastPtr()<<endl;
        result.print();
        *result.lastPtr()*=-1; // GARBAGE HERE
        cout<<*result.lastPtr()<<endl;
        return result;
    } 

    result[0]=0;

    for(int q=0; q<=length(); ++q)
    {

        if(b.length()-q >= 0)
        {
            result[q]+=(*this)[length()-q]-b[b.length()-q];

            if(result[q] < 0)
            {
                result[q]+=10;
                result[q+1]=-1;

            } 
            else
            {
                result[q+1]=0;
            }

        }
        else
        {
            result[q]+=(*this)[length()-q];

        }

    ++result.lastPtr(); 
    }

    do{
        --result.lastPtr();
    }while(!*result.lastPtr());

    return result;
}

int main(){
    char equationArray[MAX_ARRAY_SIZE*2+1];  // operandA(<=256) operator(1) operandB(<=256)
    Number a,b;

    cin>>equationArray;
    char* operatorPtr=getOperatorIndex(equationArray);

    a.lastPtr()=toIntArray(equationArray, operatorPtr, a.firstPtr());
    b.lastPtr()=toIntArray(operatorPtr+1, operatorPtr+strlen(operatorPtr), b.firstPtr());

    a.print();
    b.print();

    Number c;
    switch(*operatorPtr){
        case '-':
            c=a-b; 
            break;
    }
    c.print();

}


char* getOperatorIndex(char* equationString){
    while(!isOperator(*++equationString));   
    return equationString;
}

bool isOperator(char characterDec){
    if(characterDec>='*' & characterDec<='/')return true;
    return false;
}


int* toIntArray(char* firstInA, char* lastInA, int* firstInB){
    while(lastInA-firstInA>0){
        *firstInB=*firstInA-'0';
        ++firstInB;
        ++firstInA;
    }
    return --firstInB;  
}

1 个答案:

答案 0 :(得分:3)

班级的所有数据成员都具有与班级相同的存储时长。对象创建。因此:

int main(){
   Arr temp; 
   doSomething(temp);
}

temp.arr类型的数据成员int[10];也有自动存储持续时间 ..(或者不恰当地称为&#34; 堆栈对象&#34)

Arr doSomething(Arr &arg){
   return arg; //copy of arg created; 'copy=new int[10]' at this point?
}

不,这里没有堆分配。而是由隐式复制构造函数构成的成员副本。见How are C++ array members handled in copy control functions?