I am attempting to create a sorted array for a class assignment but I am receiving a weird access violation error in my debug code. I have double checked all my values and nothing is accessing anything out of range. My code is below.

Thanks.

#include <iostream>

using namespace std;
int size=0;

class SortedArray
{
public:
	void insert(int);
	void print();

private:
	int *sortedArray;
};

void SortedArray::insert(int inp)
{
	size++;
	int *temp=new int[size];
	for (int i=0;i<(size-1);i++)
		temp[i]=sortedArray[i];
	delete[]sortedArray;
	sortedArray=new int[size];
	for(int i=0;i<size;i++)
		sortedArray[i]=temp[i];
}
void SortedArray::print()
{
	for(int i=0; i<sizeof(sortedArray);i++)
	{
		cout << sortedArray[i] << " ";
	}
	cout << endl;
}

int main(){
    SortedArray x;
int inp;
do{
        cin >> inp;
        x.insert(inp);
        x.print();
    }while(inp>=0);
 
system ("Pause");
return 0;
}

When you apply sizeof() to a pointer, it does not return the size of the object pointed to; rather, it returns the size of the pointer itself. Thus, you will need to use size instead of sizeof(sortedArray) in the Print() method.

I would further recommend moving size into the class itself, rather than having it a global variable. That way, if you have more than one object of the SortedArray class, you wouldn't get a conflict in the sizes of the different arrays.

As a minor aside, you should be able to simplify the insert() method like so:

void SortedArray::insert(int inp)
{
	size++;
	int *temp=new int[size];
	for (int i=0;i<(size-1);i++)
		temp[i]=sortedArray[i];
	delete[]sortedArray;
	sortedArray = temp;   // since you are just copying the pointer, this should work
}

The way you had it before would also cause a memory leak, since you weren't deleting the temporary array; this approach neatly avoids that problem. However, I could add that you don't have any sort of destructor for the class, so the final value of sortedArray never gets reclaimed until the program closes.

Thanks for the suggestions Shoil-R-LEA.

I implemented the fixes, but I am still receiving the error message.

Here is my updated code. Also, I have included the full error message.

#include <iostream>

using namespace std;


class SortedArray
{
public:
	SortedArray();
	void insert(int);
	void print();
private:
	int *sortedArray;
	int size;
};

SortedArray::SortedArray()
{
	size=0;
}

void SortedArray::insert(int inp)
{
	size++;
	int *temp=new int[size];
	for (int i=0;i<(size-1);i++)
		temp[i]=sortedArray[i];
	delete[]sortedArray;
	sortedArray=temp;
}

void SortedArray::print()
{
	for(int i=0; i<size;i++)
	{
		cout << sortedArray[i] << " ";
	}
	cout << endl;
}

int main(){
    SortedArray x;
int inp;
do{
        cin >> inp;
        x.insert(inp);
        x.print();
    }while(inp>=0);
 
system ("Pause");
return 0;
}

Full Error Message:

"Unhandled exception at 0x504057aa (msvcr100d.dll) in Project 6.exe: 0xC0000005: Access violation reading location 0xccccccc0."

Also, during the debug when the error message causes the program to break,it opens a separate window in Visual Studio that is the debug code and points to the line "ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));"

I don't know if this is helpful, but I figured I would mention it.

You could improve the efficiency of the memory handling by reducing the number of times you have to copy the whole array. The best way to do this is to allocate the array, not to the exact size needed, but in multiples of some convenient value (16 is a good choice for this). You would need to add a second integer variable member to keep track of the total size of the array, as opposed to the used space.

This is just a refinement of the idea, however, and not actually necessary to make the class work.

Here's code that I think will fix several problems at once:

#include <iostream>

using namespace std;


const int ARRAY_DIVISION = 16;

class SortedArray
{
public:
    SortedArray();
    void insert(int);
    void print();
private:
    int *sortedArray;
    int size;
    int totalSize;
};

SortedArray::SortedArray()
{
    size = 0;
    totalSize = ARRAY_DIVISION;
    sortedArray = new int[totalSize];
}

void SortedArray::insert(int inp)
{
    int i;
    size++;

    if (size == totalSize)
    {
        totalSize += ARRAY_DIVISION;
        int *temp = new int[totalSize];
        if (sortedArray != NULL)
        {
            for (i=0; i<(size-1) && inp > sortedArray[i]; i++)
            {
                temp[i] = sortedArray[i];
            }
            if (i < (size - 1))
            {
                temp[i] = inp;

                for ( i++; i < size; i++)
                {
                    temp[i] = sortedArray[i - 1];
                }
            }
            else
            {
                temp[size - 1] = inp;
            }

            delete[] sortedArray;
        }
        sortedArray = temp;
    }
    else
    {
        for ( i = size - 1; i > 0 && inp < sortedArray[i - 1]; i--)
        {
            sortedArray[i] = sortedArray[i-1];
        }
        sortedArray[i] = inp;
    }
}

void SortedArray::print()
{
    for(int i=0; i<size; i++)
    {
        cout << sortedArray[i] << " ";
    }
    cout << endl;
}

int main()
{
    SortedArray x;
    int inp = 0;
    do
    {
        cin >> inp;
        cin.ignore();
        x.insert(inp);
        x.print();
    }
    while(inp>=0);

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.