Contact::Contact(const char* name2, const long long * phone2, int num2) {
bool safe = name2== nullptr && phone2 == nullptr && num2 == 0;
if (safe) {
*this = Contact();
}
else {
strcpy(name, name2);
name[19] = '\0';
bool valid = phone2 != nullptr && num2 > 0;
if (valid) {
int count = 0;
for (int i = 0; i < num2; i++) {
valid = phone2[i] > 10000000000LL && phone2[i] < 999999999999LL; // PROBABLY A GOOD IDEA TO MAKE A FUNCTION TO CHECK VALIDNESS
if (valid) count++;
}
num = count;
phone = new long long[num];
for (int i = 0, j = 0; i < num2; i++) {
if (phone2[i] > 10000000000LL && phone2[i] < 999999999999LL) {
phone[j] = phone2[i];
j++;
}
}
}
else {
num = 0;
phone = nullptr;
}
}
}
This is my 3 arguments constructor. I keep getting the error message that stack around the variable is corrupted. But when I get rid of the name2==nullptr
in line3, it works without the error (though the output is not exactly as what i want). What I am doing wrong there?