Hi,

When I run my program it crashes and a message that an asseration failure happened appears. when I debug the program it points to following:

for ( i = 0; i < delta*d; ++i)
    delete [] o[i];
  delete [] o;

I create this array as follows:

int **o;
  // Allocate memory
  o = new int*[delta*d];
  for (int i = 0; i < delta*d; ++i)
    o[i] = new int[delta*d];

Is the way I am creating or deleting the dynamic array wrong?. If not then what else may cause the assertion failure?. Thanks,

It also sometimes give me another error regarding also the dynamic array creation:

Access violation writing location 0x00000008.

More information would be helpful. What are you trying to accomplish? Are you trying to create a single-dimension array of pointers to something or a multi-dimensional array of pointers to something else (?ints?, perhaps) ? I'm having a hard time telling from the context of your code...

what's the value of variables delta and d? Is either of them 0?

The assertion failure can also be caused by something else in your program, such as writing beyond the boundries of the array. For example: o[delta*d][0] = 0; will write to non-existant element of the array. And doing that may corrupt heap memory.

Well, I don't agree with AD about how writing beyond an array will corrupt the heap, it might throw an Access Violation or segmentation fault, but not corrupt the heap (it would be wonderful if it did, it would make heap-related bugs so much easier to find! I know by experience.. painful memories..).

I would suspect that you get to the deleting part before you get to the allocation part, somehow, or that you delete twice (the rest of the code could tell). I have a hunch that you get an "Access Violation writing a location 0x00000008" when you compile in debug mode, if your compiler is set to initialize the uninitialized memory to 0. And when not in this debug mode, the pointer "o" or the pointers it contains is/are pointing no-where and trying to free it/them corrupts the heap.

The heap gets corrupted (well corrupted in a way that you are lucky enough to get an assertion, most often you don't and it just makes your program go completely crazy) when you try to free memory outside of the chunk of RAM that your App's heap is managing.

Looking at your code you are almost 100% creating the array incorrectly.
And that may cause your problem.

By the looks of it you want an 2d array of dimension d and delta.
E.g. as if you had known d and delta at compile time you would have written int o[delta][d]; or the other way round.

int **o; 
// Allocate memory
o = new int*[delta*d];          // To much memory (?) 
for (int i = 0; i < delta*d; ++i)        // now over allocate again 
  o[i] = new int[delta*d];
// You have allocated (delta*d)*(delta*d) memory locations !!!!

Normally I would have expected to see:

o=new int*[delta];
for(int i=0;i<delta;i++)
  o[i]=new int[d];

Alternatively this is often done:

o=new int*[delta];
o[0]=new int[delta*d];
for(int i=1;i<delta;i++)
  o[i]=o[0]+delta*i;
// when o is ready for deletion:
delete [] o[0];
delete [] o;

Now your delete is correct for the allocation. However, you have two problems, did you really mean to allocate that much memory. It is very possible that you effectively have more memory that your system can cope with.

Second IF your got the array wrong, have you made a mistake elsewhere. You can check
if you put a std::cout<<(long int) o[0]<<std:endl; at the allocation of you array
and the deletion you can see if you have changed the memory location. e.g. have you deleted it twice.

Third: Common error, have you decided to zero the array AFTER you have deleted it?
That should not be done and causes a similar problem

@Fbody: I am trying to create a two dimensional dynamic array of integers.
@Ancient Dragon: No the values of d and delta are not zero both of them are integers>0 and I am not writing out side the boundries.
@mike: I am creating the arrays at the begining of the function and I am deleting them before I go out (before return). could you please give me more explaination on how the array can be deleted twice ?. If I am creating an array in a function then I go out without deleting it then in another function I create an array with the same name and delete it, can this cause problems?.
I changed my way in creating the array to the following:

int **lo1=0;
  // Allocate memory
  lo1 = new int*[d];
  for (int i = 0; i < d; ++i)
    lo1[i] = new int[2];

and it still give me assertion failure :(

@stuXYZ: I am actually trying to create an array of size (delta*d) x (delta*d).. I try to put cout<<(long int) lo[0]; after the array creation. then I put it again before the delete statements it gave me the same location. I am not using the array after the delete statements. How I can know if my system can't handle this memory?.

THANK YOU GUYS.

If I am creating an array in a function then I go out without deleting it then in another function I create an array with the same name and delete it, can this cause problems?

YES, very much so. If you allocate an array in one function (assuming the array pointer is local to the function, that is, declared inside the function) and don't delete it, it is lost memory (memory leak or dangling pointer). You will never retrieve that pointer again and will never be able to delete its memory. Even if you re-enter the same function again, the pointer will have a new value that doesn't point to the memory you allocated the last time around. So, any memory you allocate with "new" inside a function has to be deleted before you leave the function.

Declaring a pointer for the array with the same name in another function has the same problem, that pointer has absolutely no relationship with the one that points to your allocated memory. The scope or life-time of a variable is limited to inside the first "{ }" around the place where it was declared (data member of classes are one exception). Think about it, otherwise, that would mean that any variable you create would have to not have the same name as any other variable in any function in the entire set of C++ standard libraries, any other libraries you use, and all of your own (if that was the case not a soul on earth would be programming in C++, because it would be so much trouble to find suitable variable names that don't conflict with others).

If you are creating a new array in another function and deleting that one, then NO there is no _real_ problem with that, in the sense that it shouldn't throw an assert or access violation, but you still have a memory leak in the function that didn't delete its array before exiting.

could you please give me more explaination on how the array can be deleted twice ?

Well you can call delete twice in a row (e.g. "delete p; delete p;"), that's one trivial example. But typically, this occurs with global pointers or pointer-type data members in objects. For example, an object might hold a pointer to an array that is allocated upon construction of the object, then you call some "cleanup()" function that deletes the array, then later when you delete the object, the destructor also deletes the array again (if you forgot to either verify that it was NULL or forgot to set it to NULL in the cleanup() function). So of course, deleting an array twice is never intentional, it's a bug, a mistake by the programmer, but that's why I said it because you were getting an error with a message that is typical of that kind of situation.


If you want access to that array in multiple functions, you have a few choices by _my_ order of preference:
1. create a class that holds that array and make your functions as methods of the class.
2. pass the array pointer around from one function to another (that's old-school C-style).
3. make it a global variable (that's not old-school C-style.. just BAD style, period, but it will do the job).

hope this helped a bit, if you post a bigger chunk of code we might be able to help more.

I understand mike. Thanks. Below is a function of my code:

int Compute_Delay_Values(double **z,double **x,double **y,double **t,int d,int r)
{
 
	vector<int> selected;
	vector<double> delay;

	int **lo=0;
  // Allocate memory
  lo = new int*[d];
  for (int i = 0; i < d; ++i)
    lo[i] = new int[2];

	cout<<(long int)lo[0]<<endl;
  
  int *count1=new int [delta];
  
  int *count2=new int [delta];
 int *single=new int[d];

for(i=0;i<d;i++)
  for(int j=0;j<r+delta-1;j++)
	  y[i][j]=2;

  int output=0;
  int i1;

 

int max,indexi,indexj;
 int best,c_delay,id,trans;
  int *sel=new int[d];

for(i=0;i<d;i++)
 for(int j=0;j<4;j++)
	 t[i][j]=0;

 for(i=0;i<d;i++)
 { single[i]=0;
   count2[i]=0;
   sel[i]=0;
 }


 for(i=delta;i<delta*d;i=i+delta)
	 for(int j=0;j<delta;j++)
		 for(int s=0;s<delta;s++)
		 {
			 if(z[s+i][j]!=-1)
			 { single[i/delta]++;
			   lo[i/delta][0]=s+i;
			   lo[i/delta][1]=j;
			 }
		 }

int flag7=0;
int a=0;
int too=0;
int res;
int res1;
int del;

		 indexi=0;
		 indexj=0;

 for(i=1;i<d;i++)
 {		 for(int i1=0;i1<delta*d;i1=i1+delta)
  for(int j=0;j<delta*d;j=j+1)
   for(int s1=0;s1<delta;s1++)
  {
	  if(((i1+s1)==lo[i][0]) && (j==lo[i][1]))
		  del=s1;
	 
  }
	 if(a==0)
	 {
	 if(single[i]==1)
	 {
		 a++;
		


 res1= compress(x,y,0,i,lo[i][1],del,r,d);
 		 if(res1==-1)
		 { output=1;
		 
			 goto out;
		 }
		
  selected.push_back(0);
  selected.push_back(i);
 
 flag7=1;
 t[0][0]=-1;
 t[0][1]=lo[i][1];
 
 t[i][0]=-1;
 t[i][1]=del;
 
	 }
	 }

	 else if((a!=0) && single[i]==1)
	 {
		 
         extend(x,y,i,lo[i][0],r,d);
	 res=compress_2(y,i,d,r,del,too);
	 if(res==-1)
		 	 { output=1;
	 
			 goto out;
		 }
	 else
	 {
		 extend(x,y,i,del,r,d);
		 compress_3(y,i,d,r);
	 }

		
	 selected.push_back(i);
	 
 t[i][0]=-1;
 t[i][1]=del;

	 }


 }

 

 if(flag7 !=1)
 {
		 max=z[0][0];
		 
		 
	for(i=0;i<delta;i++)
	{

      for(int j=0;j<delta*d;j++)
	  {
         
		if(z[j][i]>max)
		{ max=z[j][i];
		  indexj=j;
		  indexi=i;
		}
           


	  }
	}// end for loop i..

	if(max==-1)
     	 { output=1;
	
			 goto out;
		 }

int h=indexj/delta;
  selected.push_back(0);
  selected.push_back(h);

  		 for( i1=0;i1<delta*d;i1=i1+delta)
  for(int j=0;j<delta*d;j=j+1)
   for(int s1=0;s1<delta;s1++)
  {
	  if(((indexi)==i1+s1) && (indexj==j))
		  del=s1;
	 
  }

  t[0][0]=-1;
  t[0][1]=indexi;

  t[h][0]=-1;
  t[h][1]=del;
  delay.push_back(indexi);
  delay.push_back(del);

  compress(x,y,0,h,indexi,del,r,d);
 }

 

  
  while (selected.size() !=d)
  { 
	  for( i=0;i<d;i++)
	  {  
		  //cout<<"i: "<<i<<endl;
		  if(t[i][0]==-1)
	     continue;

		  for(int j=0;j<delta;j++)
      {  
		   extend(x,y,i,j,r,d);

		    
		 count1[j]=compress_2(y,i,d,r,j,count2[j]);

		  }// end for loop j..
  

		  best = count1[0];
		  c_delay=0;
		  trans=count2[0];
		  int u=0;
		  for(int l=0;l<delta;l++)
		  { if(count1[l]>best)
			  { best=count1[l];
			    c_delay=l;
				trans=count2[l];
			  }
		   if(count1[l]==best)
		   {
			   if(count2[l]>trans)
				   best=count1[l];
			   c_delay=l;
			   trans=count2[l];

		   }
		   if(count1[l] != -1)
			   u++;
		  }

          t[i][0]=best;
		  t[i][1]=c_delay;
		  t[i][2]=trans;
		  t[i][3]=u;

	
		if(best==-1)
			 { output=1;
		
		  cout<<"IN"<<endl;
			 goto out;
		 }
	  }// end for loop i..
	
	  
 for( i1=0;i1<d;i1++)
 { 
   sel[i1]=0;
 }
id=	 select(t,d,sel);


if(id==-2)
{
for(i1=0;i1<d;i1++)
if(sel[i1]==1)
{ selected.push_back(i1);
  delay.push_back(t[i1][1]);

 extend(x,y,i1,t[i1][1],r,d);
int val=0;
val= compress_3(y,i1,d,r);
if(val==-1)
	 { output=1;

		  cout<<"IN"<<endl;
			 goto out;
		 }
}
}
else
{
	selected.push_back(id);
  delay.push_back(t[id][1]);

 extend(x,y,id,t[id][1],r,d);
 compress_3(y,id,d,r);
}

  }


out:
  
	cout<<(long int)lo[0]<<endl;
for ( i = 0; i < d; ++i)
    delete [] lo[i];
  delete [] lo;

	cout<<(long int)lo[0]<<endl;
  
  
  delete [] count1;
  delete [] count2;
  delete [] single;
  delete [] sel;


if(output==1)
return -1;
else
return 0;


}// end function Compute_Delay_Values..

I tried my code on smaller input it works but when I run it on larger inputs it cause insertion failure.
I also tried to convert almost all the arrays to static when I ran it ,it cause stack overflow!. Is this because I am using too much large size arrays ? Thanks

Well stack overflow! That's too much memory usage. You can set the stack size as a compiler option, but I have never needed to, so I don't know the exact compiler option.

Other than that, you might want to learn to ident the code a bit better, it usually helps you debug things.

You might want to have a second look at line 39, if d > delta, this is an access violation.

Also line 285 is an access violation too ("lo" has been deleted at this point, so don't use it again after line 283, even just trying to print its value will throw an access violation).

Oops!. Thanks for the notice about line 39 and 285 :). But it still causing assertion failure. Thanks alot mike.
I think I need to increase the size of the stack. Anyone knows how I can do so?

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.