Take the 2-minute tour ×

Continuing from the linked question Dynamic template arrays, there two more things that I would like to be explained:

  1. I receive a warning by the compiler that the copy commands are unsafe: “Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct” How serious is this problem and what can be done to get over it?

  2. Most important: I cannot get correctly the length of the array with the common manner. The final size of array is calculated to 1, although it contains 3 elements. Is there a flaw in the code or something works differently due to dynamic arrays?

Here is my code:

template<typename T>
void VectorHolder<T>::setArrays(T firstarray[],T secondarray[] ,int N1, int N2)

    array1 = new T[N1];
    array2 = new T[N2];

    std::copy(firstarray, firstarray + N1, array1);
    std::copy(secondarray, secondarray + N2, array2);

    int arraysize = sizeof(array1)/sizeof(T);

    cout<<"\narraysize: "<<arraysize<<endl;
    cout<<"\nFirst array contains: \n";

    for(int i=0; i<arraysize;i++)

I get at the output only one element, but the array contains three. If I change the for loop as


I get the correct result. Why?

share|improve this question
Post the warning messages as well. –  Nawaz Dec 3 '11 at 13:58
Why do you need to calculate array size when N1 and N2 are passed in? –  dasblinkenlight Dec 3 '11 at 13:58
Anyway, in the other topic, I suggested you to use std::vector<T> instead of T[], but you didn't listen to me. So explain, is there any problem in using std::vector? Because I don't see you would get better solution than that. –  Nawaz Dec 3 '11 at 14:00
Why do you use raw pointers instead of stl containers?? You could use std::vector in this case. –  AlexTheo Dec 3 '11 at 14:00
Pseudo-rule: In C++, don't use naked arrays and don't use new (unless in controlled conditions), and delete never.. –  Kerrek SB Dec 3 '11 at 14:10

4 Answers 4

Question 1:

Don't mind much about that warning. It warns you about a risk of an overflow. To get rid of it, and if you're using Visual Studio, add this to your file :

#pragma warning( disable : 4996 )

I assume 4996 is the code of this warning.

Question 2:

The fact that you have :

array1 = new T[N1];

means that array1 is a pointer on T:

T * array1;

Let's suppose N2 is much bigger than N1, although that you will find that sizeof(array1) is equal to sizeof(array2).

I guess this due to the fact that sizeof(array1) returns the memory allocated for the pointer itself and not to the value to which the pointer points to, but not sure of this~.

A simple test :

char * tab1;
tab1 = new char[3];

char * tab2;
tab2 = new char[100];

if( sizeof(tab1) == sizeof(tab2) )

will return : "equals"

To resolve this problem try this :

std::vector<T> array1;

and consider getting the size by :

share|improve this answer

Did you consider using std::vector and its size method?

share|improve this answer
I suggested him to use that in the other topic as well. I explained the usage as well. –  Nawaz Dec 3 '11 at 14:03
Ok guys, of course I prefer vectors and I know that are more easily conceivable. I just tried smth with dynamic arrays to gain some knowledge about them. It is an experimental attempt, not a real project. –  arjacsoh Dec 3 '11 at 14:15

array1 is not an array, it's a pointer. Therefore, its size is that of a pointer, and is constant.

Consider using a standard container rather than a raw C-style array.

share|improve this answer

if you really want to use dynamic arrays, then do it properly: never use [], but always only pointers. Most annoyingly, you MUST pass the size of the array separately and take care to delete your array. Both these issues are tricky in complicated programs and using containers that take care of them is much preferred.

share|improve this answer

Your Answer


