sanghas26
sanghas26

Reputation: 151

Why is this method giving two different results?

So I have a class called HugeInt which stores a dynamic array of integers with each place corresponding to a decimal place (arr[0] = 2^0, arr[1] = 2^1...). So I also have this method reverse, which reverses the numbers. But in main it gives two different results, can anyone help? DynArray is the dynamic array class I have created. It just contains an array of int which resizes based on if we are adding to the array. (Cannot use vectors)

HugeInt HugeInt::reverse(){
    HugeInt hi;
    for (int i = 0; i < this->size; i++){
        hi.dyn.add(this->dyn[this->size - 1 - i]);
    }
    return hi;
}

My print functions:

void HugeInt::print(){
    dyn.print();
}

void DynArray::print(){
    for (int i = 0; i < nextIndex; i++){
        std::cout << arr[i];
    }
}

my operator=:

HugeInt& HugeInt::operator=(const HugeInt &b1)
{
    this->dyn = b1.dyn;
    this->size = b1.size;
    return *this;
}

When I run this:

int main(int argc, char *argv[])
{
    HugeInt hi4("123456");

    hi4.print();
    cout << endl;
    cout << endl;
    hi4.reverse().print();
    cout << endl;
    cout << endl;
    hugeInt = hi4.reverse();
    hugeInt.print();
}

I get these results:

123456

654321

3854321

Why is the last result different from the second result? I am not used to coding in c++ so I feel like it might be some kind of c++ thing I am overlooking?

UPDATE: ok so I am totally lost now. I changed my reverse() to :

HugeInt HugeInt::reverse()
{
    return *this;
}

and my main as:

int main(int argc, char *argv[])
{
    HugeInt hi4("123456");

    hi4.reverse().print();
    cout << endl;
    cout << endl;
    hugeInt = hi4.reverse();
    hugeInt.print();
}

and get the results

123456

3223456

32 on the ASCII table is 'Space' and 38 is & which is what it was saying before. I'm so lost!

Upvotes: 0

Views: 173

Answers (2)

Kupto
Kupto

Reputation: 2992

I tried to implement your problem. I had to improvise little bit on those class insides, but this code works flawlessly with MS VS 2010.

My program:

//file: header.h
//author: kupto
#pragma once
#include <iostream>
#include <stdio.h>
#include <vector>

class DynArray
{
    std::vector<char> arr;

public:
    void print();
    void add(char c) {arr.push_back(c);};
    char get(int pos) {return (arr[pos]);};
    int size() {return arr.size();};
};

void DynArray::print()
{
    for (int i = 0; i < this->size(); i++)
        std::cout << arr[i];
    std::cout << std::endl;
}

class HugeInt
{
    DynArray dyn;

public:
    void print() {dyn.print();}
    int size() {return (dyn.size());}

    HugeInt HugeInt::reverse();

    HugeInt(char* str);
    HugeInt() {};

};

HugeInt::HugeInt(char* str)
{
    char c;
    int i = 0;
    c = str[i];

    while (c)
    {
        dyn.add(c);
        c = str[++i];
    }
}

HugeInt HugeInt::reverse()
{
    HugeInt hi;
    for (int i = 0; i < this->size(); i++){
        hi.dyn.add(this->dyn.get(this->size() - 1 - i));
    }
    return hi;
}

and the source file:

//file: source.cpp
//author: Kupto
#include "header.h"

using namespace std;

int main()
{
    HugeInt hi4("123456");

    hi4.print();
    hi4.reverse().print();
    HugeInt hugeInt = hi4.reverse();
    hugeInt.print();
    hi4 = hugeInt;
    hi4.print();
    hi4 = hi4.reverse();
    hi4.print();
    return 0x00;
}

The output is as expected:

123456
654321
654321
654321
123456

Note, that I used DynArray::get(int) method instead your operator[int]. Good luck with your code.

Upvotes: 0

Drew Dormann
Drew Dormann

Reputation: 63745

This line in HugeInt HugeInt::reverse() is bad.

this->~HugeInt();

That means that the first time you call hi4.reverse(), you destroy hi4.

Every time it's used afterwards is undefined behavior.

Remove that line. I can't figure out what you were hoping to do there, but "nothing" is probably a better thing to do there.

Upvotes: 3

Related Questions