So I made all the functions for a project where I was supposed to take input from the user through a menu and make an array and do various things with it. Two things I couldnt figure out were how to Lock and unlock the array, and then I also couldnt figure out how to delete the array without getting an error. Here is the code:

int addamount();
void addvalues(int arr[],int n);
void showvalues(int arr[], int n);
int sumvalues(int arr[], int n);
void avervalues(int arr[], int n);
void showmax(int arr[], int n);
void showmin(int arr[],int n);
int menu(int size,int choice,bool hidemenu);

int main()
{
    int size=0;
    int choice=0;
    int *parr= new int[size];
    bool hidemenu=true;
    for(;;)
    {
        choice=menu(size,choice,hidemenu);
        if(choice==1)
        {
            size=addamount();
        }
        else if(choice==2 && size != 0)
        {
            addvalues(parr,size);
            hidemenu=false;
        }
        else if(choice==3 && hidemenu==false)
            sumvalues(parr, size);
        else if(choice==4 && hidemenu==false)
            avervalues(parr, size);
        else if(choice==5 && hidemenu==false)
            showmax(parr, size);
        else if(choice==6 && hidemenu==false)
            showmin(parr, size);
        else if(choice==7 && hidemenu==false)
            showvalues(parr, size);
        else if(choice==8 && hidemenu==false)//SUPPOSED TO LOCK
            const int (*parr);
        else if(choice==9 && hidemenu==false)//SHOULD DELETE arr
            delete[]parr;
    }
    return 0;
}

The error I get after hitting delete says the application wrote to memory after the end of the heap buffer. The const doesnt lock the array but doesnt give me an error. Any ideas of why this isnt working and what else I could do would be great.

>int size=0;
>int *parr= new int;
For starters, size isn't a compile-time constant, which means you're relying on a compiler extension and your code is thus non-portable. Next, an array of zero size is unlikely to be what you want. The error you're getting generally means that you've written outside the bounds of the array (or in the case of a zero size, written to the array at all). You didn't show the definitions of the functions that are being called, so I can't say for sure what's happening.

>The const doesnt lock the array but doesnt give me an error.
All you're doing is redeclaring a second variable called parr in the nested scope (it hides the existing variable). When you leave that scope, the new variable is destroyed and you have the original variable, which is not const. If you want to "lock" the array, you need to reference it using a sentry object or a const pointer instead of through the original. There's no way to add constness permanently.

I'm somewhat confused about what you mean by "lock" and "unlock" the array. Perhaps you could describe the result you're looking for and then I can help you come up with a suitable way of achieving it. You seem to want something different from const.

>int size=0;
>int *parr= new int;
For starters, size isn't a compile-time constant, which means you're relying on a compiler extension and your code is thus non-portable

I think you just had a brain fart :) There is nothing wrong with calling new with a non-const integer. Maybe you were thinking this: int parr[size];

Yep, that's what I get for not being anal about terminology. I was typing "array" to mean "simulated array" and got caught up in my own web of lies. ;) Thanks for the correction.

Ok, I see what you guys are talking about, so how would I insert the amount of my dynamic array inside the if statement and have it so that my addamount function somehow can actually put the amount into the dynamic array so the delete function can work.

int main()
{
	int size;
	int choice;
	int *parr= new int[];
	bool hidemenu=true;
	for(;;)
	{
		choice=menu(hidemenu);
		if(choice==1)
		{
			size=addamount();
		}
		else if(choice==2)
		{
			addvalues(parr,size);
			hidemenu=false;
		}
		else if(choice==3 && hidemenu==false)
			sumvalues(parr, size);
		else if(choice==4 && hidemenu==false)
			avervalues(parr, size);
		else if(choice==5 && hidemenu==false)
			showmax(parr, size);
		else if(choice==6 && hidemenu==false)
			showmin(parr, size);
		else if(choice==7 && hidemenu==false)
			showvalues(parr, size);
		else if(choice==8 && hidemenu==false)
			break;
		else if(choice==9 && hidemenu==false)
			delete[]parr;
	}
	return 0;
}

>int *parr= new int[];
*sigh*

>so how would I insert the amount of my dynamic array inside the if statement
One option is to nitialize it to null and add an edge case to the options that require it to be non-null:

int *p = 0;

for ( ; ; ) {
  choice = menu();

  if ( choice == 1 ) {
    size = addamount();
    p = new int[size];
  }
  else if ( choice == 2 ) {
    if ( p != 0 ) {
      ...
    }
  }
  ...
}

delete[] p;
This question has already been answered. Start a new discussion instead.