1

My code is supposed to print the Union and Intersection of two sets of integers. Why do I get this warning?

screenshot

Is it because I use dynamic arrays and it's size could be anything in runtime?

How can I fix it? My code works fine but this warning really bugs me.

P.S: I know it would be a lot easier to use std::vector but my teacher required to use arrays.

#include <iostream>

using namespace std;

void UnionFunc(int[],int,int[],int,int[],int&);
void IntersectionFunc(int[], int, int[], int, int[], int&);

int main() {
    int* A;
    int SizeA;
    int* B;
    int SizeB;
    int* Union;
    int UnionSize=0;
    int* Intersection;
    int IntersectionSize=0;

    cout << "Enter the Size of First Set : "; cin >> SizeA;
    A = new int[SizeA];
    cout << "Enter the Size of Second Set : "; cin >> SizeB;
    B = new int[SizeB];
    Intersection = new int[SizeA >= SizeB ? SizeB : SizeA];
    Union = new int[SizeA + SizeB];

    for (int i = 0; i < SizeA; i++) {
        cout << "Set A[" << i + 1 << "] = ";
        cin >> A[i];
    }
    for (int i = 0; i < SizeB; i++) {
        cout << "Set B[" << i + 1 << "] = ";
        cin >> B[i];
    }
    
    UnionFunc(A,SizeA,B,SizeB,Union,UnionSize);
    IntersectionFunc(A, SizeA, B, SizeB, Intersection, IntersectionSize);

    cout <<endl<< "Union Set : ";
    for (int i = 0; i < UnionSize; i++) {
        cout << Union[i] << ",";
    }

    cout <<endl <<"Intersection Set : ";
    for (int i = 0; i < IntersectionSize; i++) {
        cout << Intersection[i] << ",";
    }


    system("pause>n");
    return 0;
}

void UnionFunc(int A[],int SizeA, int B[],int SizeB, int Union[],int &UnionSize) {
    
    //Adding First Array to Union Array
    for (int i = 0; i < SizeA;i++) {
        Union[i] = A[i];
        UnionSize++;
    }

    //Checking if second array's elemnts already exist in union arry, if not adding them
    bool exist;

    for (int i = 0; i < SizeB; i++) {
        exist = false;
        for (int j = 0; j < UnionSize; j++) {
            if (B[i] == Union[j] ) {
                exist = true;
            }
        }
        if (exist == false) {
            Union[UnionSize] = B[i];
            UnionSize++;
        }
    }

}

void IntersectionFunc(int A[], int SizeA, int B[], int SizeB, int Intersection[], int& IntersectionSize) {
    for (int i = 0; i < SizeA; i++) {
        for (int j = 0; j < SizeB; j++) {
            if (A[i] == B[j]) {
                Intersection[IntersectionSize] = A[i];
                IntersectionSize++;
            }
        }
    }

}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 2
    Possible duplicate of [Warning C6385 in Visual Studio](https://stackoverflow.com/questions/59649678/warning-c6385-in-visual-studio) – sebastian Aug 23 '20 at 15:45
  • 1
    @sebastian I agree that the linked question is very similar (even a duplicate, maybe), but the answer given there is simply to "turn off" the warning. In this case, there is a better way, which is not really possible/appropriate in the case of the 'dupe'. – Adrian Mole Aug 23 '20 at 16:02
  • @AdrianMole, MS acknowledged it's a bug, and said they'd fix it. Granted, that was 8 months ago :P – ChrisMM Aug 23 '20 at 16:04
  • @ChrisMM But in this case, it may not *always* be a false positive. – Adrian Mole Aug 23 '20 at 16:05

1 Answers1

1

Is it because I use dynamic arrays and it's size could be anything in runtime?

Yes! The compiler doesn't know (and, as your code is written, can't know) that both SizeA and SizeB will be 'valid' numbers - so the size of the three int arrays you create could be less than is required for the Intersection[i] 'read' to be valid.

A 'quick and dirty' fix for this is to provide a visible guarantee to the compiler that the arrays you create will be at least a certain size, like this:

A = new int[max(1,SizeA)]; // Compiler can now 'see' a minimum size

And similarly for the other allocations you make with the new[] operator.

(I have tested this with VS2019, adding the max(1,SizeA) and max(1,SizeB) 'fixes' to just the allocations of A and B and the warning is removed.)

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83