Search Results

Search found 2 results on 1 pages for 'kiokko89'.

Page 1/1 | 1 

  • c++ struct size

    - by kiokko89
    struct CExample { int a; } int main(int argc, char* argv[]) { CExample ce; CExample ce2; cout << "Size:" << sizeof(ce)<< " Address: "<< &ce<< endl; cout << "Size:" << sizeof(ce2)<< " Address: "<< &ce2 << endl; CExample ceArr[2]; cout << "Size:" << sizeof(ceArr[0])<< " Address: "<<&ceArr[0]<<endl; cout << "Size:" << sizeof(ceArr[1])<< " Address: "<<&ceArr[1]<<endl; return 0; } Excuse me I'm just a beginner but i'd like to know why with this code, there is a difference of 12 bytes between the addresses of the first two objects(ce and ce2) (i thought about data allignment), but there is only a difference of 4 bytes between the two objects in the array. Sorry for my bad English...

    Read the article

  • Understanding C++ dynamic allocation

    - by kiokko89
    Consider the following code: class CString { private: char* buff; size_t len; public: CString(const char* p):len(0), buff(nullptr) { cout << "Constructor called!"<<endl; if (p!=nullptr) { len= strlen(p); if (len>0) { buff= new char[len+1]; strcpy_s(buff, len+1, p); } } } CString (const CString& s) { cout << "Copy constructor called!"<<endl; len= s.len; buff= new char[len+1]; strcpy_s(buff, len+1, s.buff); } CString& operator = (const CString& rhs) { cout << "Assignment operator called!"<<endl; if (this != &rhs) { len= rhs.len; delete[] buff; buff= new char[len+1]; strcpy_s(buff, len+1, rhs.buff); } return *this; } CString operator + (const CString& rhs) const { cout << "Addition operator called!"<<endl; size_t lenght= len+rhs.len+1; char* tmp = new char[lenght]; strcpy_s(tmp, lenght, buff); strcat_s(tmp, lenght, rhs.buff); return CString(tmp); } ~CString() { cout << "Destructor called!"<<endl; delete[] buff; } }; int main() { CString s1("Hello"); CString s2("World"); CString s3 = s1+s2; } My problem is that I don't know how to delete the memory allocated in the addition operator function(char* tmp = new char[length]). I couldn't do this in the constructor(I tried delete[] p) because it is also called from the main function with arrays of chars as parameters which are not allocated on the heap...How can I get around this? (Sorry for my bad English...)

    Read the article

1