1

I'm trying to implement a new datatype called HugeInteger in C++ with classes. I've encountered a problem that when I try to create new HugeInteger, my program terminates itself when the first number is printed. When I comment the statement the statement user enter a HugeInteger but the statement that displays the number is still there, the first number isn't written(the default value of the number is 0) and second number is filled by user. My try is as following:

HugeInteger.h

// HugeInteger class definition


#ifndef HUGEINT_H
#define HUGEINT_H

class HugeInteger
{
private:
    int arr[40] = {0};
    size_t len;
    
    
public:
    
    void Input();
    void Output();
    
};

#endif

HugeInteger.cpp

#include "HugeInteger.h"
#include <iostream>
#include <sstream> // to fill arr


void HugeInteger::Input()
{
    std::string line;
    int i;
    std::getline(std::cin, line);
    
    for (size_t i = 0; i < line.length(); i++)
    {
        arr[i] = line[i] - '0';
    }
    len = line.length();
}

void HugeInteger::Output()
{
    int i = 0;
    while (i < len)
        std::cout << arr[i++];
}

main.cpp

#include <iostream>
#include "HugeInteger.h"

int main()
{
    HugeInteger hui = HugeInteger();
    //hui.Input(); // when this is commented, Output method does not work.
    hui.Output();
    HugeInteger hui2 = HugeInteger();
    hui2.Input();
    hui2.Output();
    return 0;
}

Note: I've just perfomed another debug that I create another method called getLen so that I can access the len attribute. However, I put the code before and after Output method in the main.cpp, but I couldn't see the value of len neither these poisions. After that, I make the arr attribute as public, and tried to see the contents of it, and I couldn't see that either. I think my code doesn't work and I can somehow only see what I entered, after that the program is finished. Note: I think I'm not facing a buffer overflow problem, because I always debugged my program with 3 4 digit numbers, so my array has contain 3-4 numbers and the rest is 0. Moreover, I'll probably put a code to handle this issue.

kursat
  • 11
  • 2
  • Does this answer your question? [Segmentation fault on large array sizes](https://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) – Erel Feb 25 '22 at 09:47
  • 4
    @Erel I wouldn't consider `int arr[40]` as that big that one or two instances of `HugeInteger` may exhaust the stack - not with the usual default settings of the major compilers on the major platforms... – Scheff's Cat Feb 25 '22 at 09:53
  • 1
    Notice that you would habe buffer overflow when input is longer than 40; – Jarod42 Feb 25 '22 at 09:57
  • @Scheff's Cat You're right indeed. I did not read the question carefully: I misread `40` and thought it `400`. – Erel Feb 25 '22 at 13:03
  • @Jarod42 Yeah thx for the warning. I'll probably handle it like using overflow case, for instance when user enters 41-digit number, I'll round it to 0 or may be -2^40. – kursat Feb 25 '22 at 20:58

1 Answers1

3

The call to Input() sets the value of the len member. Without the call, the member is uninitialized, but still used by Output().

Just initialize it to 0, similar to what you do with the array.

BoP
  • 2,310
  • 1
  • 15
  • 24