It would be better starting a new thread for this. It is also good netiquette.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I dunno. It's always good to resurect 2 year old threads!
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
>>Hello sorry if i hijack this thread for my own purpose
Please don't do it again. I split your thread this time for you for free :)
Line 5 is incorrect. Arrays are numbered from 0 to, but not including, the value of N. You should use the < operator, not <= operator.
To solve your problem I think just pass the second parameter by reference and let expandera_lista() increment it. That way the calling function will always have an updated copy of the size of the array.
Your function is also doing too much copying. You only have to copy once.
void expandera_lista( int *&list , int& n)
{
int *templist = new int[n+1];
for(int i=0; i<n;i++)
{
templist[i] = list[i];
}
delete[] list;
list = templist;
++n;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>My problem is how to get the size of my array when its a dynamic array
You allocated the array, you should know the size. In other words, there's no portable way to find the size of allocated memory given nothing but a pointer to it. You need to remember the size yourself and pass it around along with the pointer.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
use a vector instead of a simple c-style array and you won't have that problem. c++ vector class has a method that returns the current size of the array.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343