dynamic array problem

I tried to use dynamic array. but it gives me following error
Project Project1.exe raised exception class EInvalidPointer with message 'Invalid pointer operation'
after that when i click continue it gives next error
Project Project1.exe raised exception class EStackOverflow with message 'Stack overflow'.
now next message is access voilation at xxxxxx
so where i am going wrong?
Heres my source code

int sizeofx;
sizeofx=StrToInt(ComboBox1->Text);
int* x=NULL; // Pointer to int, initialize to nothing.
x = new int[sizeofx]; // Size needed for array
int sizeofpsp;
sizeofpsp=npsp;
int* psp=NULL;
psp = new int[sizeofpsp];
int k=0; //Counter
int temp; //To store value of 2's complement
int l;
l=sizeofx;
int loc1=StrToInt(Edit4->Text);
int loc2=StrToInt(Edit5->Text);
/////////////////////////////////////////
for(k=1;k<=sizeofx;k++)
{
x[k]=1;
}

/////////////////////////////////////////
for(k=1;k<=sizeofpsp;k++)
{
psp[k]=x[sizeofx];
temp=x[loc1]^x[loc2];
do
{
x[l]=x[l-1];
l--;
}while(l>1);
x[1]=temp;
l=sizeofx;

}
///////////////////////////////////////////////
for(k=0;kCells[k][0]=psp[k+1];
}

delete [] x; // When done, free memory pointed to by x.
//x = NULL; // Clear a to prevent using invalid memory reference.
delete [] psp; // When done, free memory pointed to by psp.
//psp = NULL; // Clear a to prevent using invalid memory reference.





Comments

  • : I tried to use dynamic array. but it gives me following error
    : Project Project1.exe raised exception class EInvalidPointer with message 'Invalid pointer operation'
    You write/read out of the array's bounds.

    I'd suggest to use code tags next time [leftbr]code] and [leftbr]/code]

    Arrays go from index 0 to [b]and not including[/b] their size.
    So an array of size x has indices 0 to x.

    [code]
    : for(k=1;k<=sizeofx;k++)
    : for(k=1;k<=sizeofpsp;k++)
    [/code]

    Your for-loops also rad/write at index x (one beyond the max index).

    Why not use a std::vector, it can do range checking for you?

    You might want to check
    www.codepedia.com/1/CppArray
    www.codepedia.com/1/CppVector

    See ya,
    bilderbikkel

  • : : I tried to use dynamic array. but it gives me following error
    : : Project Project1.exe raised exception class EInvalidPointer with message 'Invalid pointer operation'
    : You write/read out of the array's bounds.
    :
    : I'd suggest to use code tags next time [leftbr]code] and [leftbr]/code]
    :
    : Arrays go from index 0 to [b]and not including[/b] their size.
    : So an array of size x has indices 0 to x.
    :
    : [code]
    : : for(k=1;k<=sizeofx;k++)
    : : for(k=1;k<=sizeofpsp;k++)
    : [/code]
    :
    : Your for-loops also rad/write at index x (one beyond the max index).
    :
    : Why not use a std::vector, it can do range checking for you?
    :
    : You might want to check
    : www.codepedia.com/1/CppArray
    : www.codepedia.com/1/CppVector
    :
    : See ya,
    : bilderbikkel
    It show error while closing program. function works correctly means it gives correct results. and when i change dynamic array to static array it doesnt shows any error.
    well i have never tried vector thing. will be interesting .

  • : It show error while closing program. function works correctly means it gives correct results. and when i change dynamic array to static array it doesnt shows any error.
    Yes, this error is not always detected.
    Both code snippets below are equally wrong:

    [code]
    const int size = 5;
    int array[size];
    array[size] = 0; //ERROR! Out of range!
    [/code]

    [code]
    const int size = 5;
    int * array = new int[size];
    array[size] = 0; //ERROR! Out of range!
    [/code]

    Access violations are viewed as 'time bombs'. If YOU don't get them NOW, does not mean that you or someone else will get it.

    : well i have never tried vector thing. will be interesting .
    It is recommended to do so by the Experts.

    Compare this:
    [code]
    const int size = 5;
    std::vector v(size);
    const int index = size;

    if (index < 0 || index >= v.size()) //Yes, bigger or equal then the size
    {
    std::cout << "OUT OF RANGE!!!" << std::endl;
    }

    v[index] = 0; //ERROR! Out of range!
    [/code]

    See ya,
    bilderbikkel

  • : Access violations are viewed as 'time bombs'. If YOU don't get them NOW, does not mean that you or someone else will get it.
    :
    : : well i have never tried vector thing. will be interesting .
    : It is recommended to do so by the Experts.
    :
    : Compare this:
    : [code]
    : const int size = 5;
    : std::vector v(size);
    : const int index = size;
    :
    : if (index < 0 || index >= v.size()) //Yes, bigger or equal then the size
    : {
    : std::cout << "OUT OF RANGE!!!" << std::endl;
    : }
    :
    : v[index] = 0; //ERROR! Out of range!
    : [/code]
    :
    : See ya,
    : bilderbikkel

    thanks 4 help :)
    its always good to see u here :)

  • I forgot to mention that the std::vector has an [b]at[/b] method that can do range checking for you:

    [code]
    const int size = 5;
    std::vector v(size);
    v.at(size) = 0; //Will give an error
    v.at( -1) = 0; //Will give an error
    [/code]

    See ya,


    bilderbikkel

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories