Most Likely a Simple Mistake

Is there something that I'm missing here?
1
2
3
4
5
6
7
8
	     try{
		  int* DynamicVertexTemp = new int[DynamicVertex.size()];
		}
        catch(std::bad_alloc&){
			Exceptions.ReportException(1);
		}

      std::copy (DynamicVertex.begin(), DynamicVertex.end(),DynamicVertexTemp[]);

All the includes are correct;
DynamicVertex (if you didn't notice) is a vector which is properly filled with numbers;
What is Underlined is the errors.

'DynamicVertexTemp' : undeclared identifier

Is this because it's in a try statement?

error C2059: syntax error : ']'

It says Expected an expression, I can solve it by putting a number there, however I want to pass the entire array into the copy function.
Change

1
2
3
	     try{
		  int* DynamicVertexTemp = new int[DynamicVertex.size()];
		}

to
1
2
3
4
	      int* DynamicVertexTemp;
	     try{
		  DynamicVertexTemp = new int[DynamicVertex.size()];
		}



Here

std::copy (DynamicVertex.begin(), DynamicVertex.end(),DynamicVertexTemp[]);

instead of DynamicVertexTemp[] shall be DynamicVertexTemp
1
2
3
4
5
6
7
try{
   int* DynamicVertexTemp = new int[DynamicVertex.size()];
   std::copy (DynamicVertex.begin(), DynamicVertex.end(),DynamicVertexTemp);
}
catch(std::bad_alloc&){
   Exceptions.ReportException(1);
}
I'll assume that you don't want the copy to occur, if the allocation failed.

This way you can separate normal flow and error handling.
Last edited on
@ne555

The problem is that the copying in the code you suggested has no sense and leads only to memory leak.:)
Last edited on
I don't see the leak, ┬┐care to explain?

Edit: put a //... in line 4
Last edited on
The int* goes out of scope after having just allocated the memory, no way to get the pointer back.

You need a combo of both:
1
2
3
4
5
6
7
8
9
10
11
12
int* DynamicVertexTemp = NULL;
try{
   DynamicVertexTemp = new int[DynamicVertex.size()];
   std::copy (DynamicVertex.begin(), DynamicVertex.end(),DynamicVertexTemp);
}
catch(std::bad_alloc&){
   Exceptions.ReportException(1);
   
  // Maybe This helps the function proceed as normal
  DynamicVertexTemp = DynamicVertex;

}
Last edited on
It's only a snippet!

Every thing was handled afterwards; Thanks for the speedy replies.
I've always wondered if there is a way to try to allocate the memory just after the end of the existing array, thus eliminating the need for the copy.
Topic archived. No new replies allowed.