Solved

Redim Preserve In C++

Posted on 2004-08-02
19
1,338 Views
Last Modified: 2008-01-09
int i=0;
int *Arr_UserId;
...
while(!rs->ADOEOF)
{
      Arr_UserId = new int[i]; //MUST PRESERVE
      Arr_UserId[i] = rs->Fields->GetItem("FK_UserId")->GetValue();
      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?

0
Comment
Question by:mammouth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
  • +1
19 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11696352
>>Arr_UserId = new int[i]; //MUST PRESERVE

is incorrect since 'i' is '0' at that moment. Allocate the right size and you won't have problems.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11696377
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.
0
 
LVL 1

Author Comment

by:mammouth
ID: 11696427
jkr: i can't allocate the right size because it come from database and i need to use adopenforwardonly.

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 1

Author Comment

by:mammouth
ID: 11696434
jaime_oliviares: How can i use those template?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11696482
please specify your compiler and Operating System first.
0
 
LVL 1

Author Comment

by:mammouth
ID: 11696493
Windows XP and c++7 under .net
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11696616
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);
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 11696634
Then, use a vector<int> instead, e.g.

int i=0;
vector<int> Arr_UserId;
...
while(!rs->ADOEOF)
{

    Arr_UserId.push_back( rs->Fields->GetItem("FK_UserId")->GetValue());
    i+=1;
    rs->MoveNext();
}
0
 
LVL 22

Expert Comment

by:grg99
ID: 11696658
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.

 
0
 
LVL 1

Author Comment

by:mammouth
ID: 11696667
upper bound may be up to 10,000
0
 
LVL 1

Author Comment

by:mammouth
ID: 11696706
jaime_olivares: i ger error:  error C2065: 'CUIntArray' : undeclared identifier
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11696792
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.
0
 
LVL 1

Author Comment

by:mammouth
ID: 11696826
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)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11696858
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...)
0
 
LVL 86

Expert Comment

by:jkr
ID: 11696876
Why not using a vector<int>? All you need is

#include <vector>
0
 
LVL 1

Author Comment

by:mammouth
ID: 11696917
jkr: How can i retrieve value in a vector?
may i use Arr_UserId[5] for example?

This seem to not work?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11696948
You can use array.GetAt(43) or array[43] using MFC arrays.
 
0
 
LVL 86

Expert Comment

by:jkr
ID: 11696982
>>may i use Arr_UserId[5] for example?

Sure,

int n =  Arr_UserId[5];

will just work fine.
0
 
LVL 1

Author Comment

by:mammouth
ID: 11697000
Sorry, my mistake, working great.

Thankx
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question