int* a=new int[100000000];
Are you sure that the os allocate 100000000 integer for you?
if not it will take about 0.000 s
and I test that it takes 0.5s if the os(winxp,vc 6.0) allocate so many space.
Printable View
int* a=new int[100000000];
Are you sure that the os allocate 100000000 integer for you?
if not it will take about 0.000 s
and I test that it takes 0.5s if the os(winxp,vc 6.0) allocate so many space.
Your code will refuse to compile if you did this:Quote:
Originally Posted by aewarnick
That's why you need the other overload.Code:#include <iostream>
//...
void foo( const aArr<int>& a )
{
std::cout << a[0]; // error -- operator [] is non-const
}
Regards,
Paul McKenzie
If if returns without throwing bad_alloc then yes, I am sure.Quote:
Originally Posted by sunshinesky
In release mode?Quote:
if not it will take about 0.000 s
and I test that it takes 0.5s if the os(winxp,vc 6.0) allocate so many space.
Paul, you are absolutely right about this:
void foo( const aArr<int>& a )
{
std::cout << a[0]; // error -- operator [] is non-const
}
And now I understand why. Thanks! I learned something today!
Someone mentioned that the Ins function was very slow and they are right. I was under the impression that memcpy copied data from begin to end but I did some tests and it LOOKS like it copies from end to begin.
Am I correct about this?
Should it matter? It may copy all data or part of data. Reverce order is most natural algorithm to move part of array in-place...
It does matter. Think about it in terms of how inserting data works. I need to move memory to the right to accomodate for inserted data:
stringy v("abcd");
v.Insert(0, "C");
I'm inserting one character which means all the data must move right one.
From begin to end bcd would all become a's because they would be over-written.
From end to begin, there is an empty spot at the end and therefore, not data is over-written.
*Do you imagine how complex your cardiovascular system is?* :lol:
There is a problem. It does not work correctly unless sizeof(T) is 1 or 2. It would seem that memcpy is not copying backward if T is an int (size 4) because the data is over-written just like I said above. Why is this?
PHP Code:void Ins(uint where, const T& d)
{
if(!Count || where >= Count)
{
Add(d);
return;
}
if(Count >= capacity)
Resize(Count < 10 ? 20 : Count*1.5f);
memcpy(&_Items[where+1], &_Items[where], (Count-where)*sizeof(T));
memcpy(&_Items[where], &d, sizeof(T));
/*aBuffer buf(sTemp);
//T* temp= (T*)malloc(sTemp);
memcpy(buf.Ptr(), &_Items[where], sTemp);
memcpy(&_Items[where], &d, sT);
memcpy(&_Items[where+1], buf.Ptr(), sTemp);
//free(temp);*/
++Count;
}
aArr<short> arr(4);
arr.Add('a'); arr.Add('b'); arr.Add('c'); arr.Add('d');
arr.Ins(0, 'C');
MB(arr.ToStr());
//Output: 67, 97, 98, 99, 100
//When short is int: 67, 97, 97, 97, 97
I found the solution: memmove.
I still don't understand why char and short worked fine though. Can anyone explain that?
Make sure expected behaviour isn't only what you experience using function, but what's declared in standard.