mammouth
asked on
Redim Preserve In C++
int i=0;
int *Arr_UserId;
...
while(!rs->ADOEOF)
{
Arr_UserId = new int[i]; //MUST PRESERVE
Arr_UserId[i] = rs->Fields->GetItem("FK_Us erId")->Ge tValue();
i+=1;
rs->MoveNext();
}
Arr_UserId = new int[i]; do not preserve data. For example Arr_UserId[0]=1234 When i loop again Arr_UserId[0]=will contain nothing and Arr_UserId[1] will contain data.
How can i preserve data?
int *Arr_UserId;
...
while(!rs->ADOEOF)
{
Arr_UserId = new int[i]; //MUST PRESERVE
Arr_UserId[i] = rs->Fields->GetItem("FK_Us
i+=1;
rs->MoveNext();
}
Arr_UserId = new int[i]; do not preserve data. For example Arr_UserId[0]=1234 When i loop again Arr_UserId[0]=will contain nothing and Arr_UserId[1] will contain data.
How can i preserve data?
I think will be better to use a template array from C++ (or MFC if working in Visual C++). Them have a good memory management and easy array grow without loss.
ASKER
jkr: i can't allocate the right size because it come from database and i need to use adopenforwardonly.
ASKER
jaime_oliviares: How can i use those template?
please specify your compiler and Operating System first.
ASKER
Windows XP and c++7 under .net
Ok, that will be easier. To store an array of integer, just use CUintArray or CDWordArray classes
CUIntArray array;
To add an element:
array.Add(val); <-- Add i value to the end of array, array grows automatically
To retrieve an element:
val = array.GetAt(43): <--- Retrieve the value of 43th element
To change an element:
val = array.SetAt(43, v): <--- Change the value of 43th element
To obtain array size:
size = array.GetSize();
To manually resize array:
array.SetSize(newSize);
CUIntArray array;
To add an element:
array.Add(val); <-- Add i value to the end of array, array grows automatically
To retrieve an element:
val = array.GetAt(43): <--- Retrieve the value of 43th element
To change an element:
val = array.SetAt(43, v): <--- Change the value of 43th element
To obtain array size:
size = array.GetSize();
To manually resize array:
array.SetSize(newSize);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How big can the array possibly get?
If it's upper bound is less than say 100 or so, a fixed size array is probably simpler/faster than any fancy variable size array folderol.
If it's upper bound is less than say 100 or so, a fixed size array is probably simpler/faster than any fancy variable size array folderol.
ASKER
upper bound may be up to 10,000
ASKER
jaime_olivares: i ger error: error C2065: 'CUIntArray' : undeclared identifier
OK, this is the problem:
CUIntArray belongs to Visual C++ MFC library, so I guess you are trying to compile a console application. The best way to support MFC is creating a new Project -> Win32 Console Application, and choose "An application that supports MFC" in the dialog. After that you will be able to use CUIntArray.
If you don't want to use MFC at all, you can use standard C++'s vector<> template as detailed by jkr, but I think it is a bit more complicated for a begginer. That's why I suggested you CUIntArray.
CUIntArray belongs to Visual C++ MFC library, so I guess you are trying to compile a console application. The best way to support MFC is creating a new Project -> Win32 Console Application, and choose "An application that supports MFC" in the dialog. After that you will be able to use CUIntArray.
If you don't want to use MFC at all, you can use standard C++'s vector<> template as detailed by jkr, but I think it is a bit more complicated for a begginer. That's why I suggested you CUIntArray.
ASKER
jaime_olivares: i added #include <afxcoll.h> at top of program to be able to use CUIntArray.
Now it's ok but when i do array.Add(1);, i get error...
ClientCom error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in libcpd.lib(newop.obj)
Now it's ok but when i do array.Add(1);, i get error...
ClientCom error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in libcpd.lib(newop.obj)
Your #include allows you to compile, because CUIntArray is declared at afxcoll.h, but your project is not liking MFC library correctly. So, maybe you can follow my recommendation. Please specify your type of project (console, windows, etc...)
Why not using a vector<int>? All you need is
#include <vector>
#include <vector>
ASKER
jkr: How can i retrieve value in a vector?
may i use Arr_UserId[5] for example?
This seem to not work?
may i use Arr_UserId[5] for example?
This seem to not work?
You can use array.GetAt(43) or array[43] using MFC arrays.
>>may i use Arr_UserId[5] for example?
Sure,
int n = Arr_UserId[5];
will just work fine.
Sure,
int n = Arr_UserId[5];
will just work fine.
ASKER
Sorry, my mistake, working great.
Thankx
Thankx
is incorrect since 'i' is '0' at that moment. Allocate the right size and you won't have problems.