dynamic allocate

I have a class and I am reading list of data from file and i made a static allocated memory and stored everything into a pointer array. The problem I am having is that i need it to be dynamically allocated and i don't know how to do that. If someone could help out I will greatly appreciate it. This is the part of the code where i made static allocated memory.

1
2
3
4
5
6
7
8
9
10
11
12
13
void main(int argc, char *argv[])
{
   const int MAX=200; 
    ifstream infile; 

bank *a; 
a=new bank[MAX];

while(!infile.is_open()){
  {
     //code to open 
}
Last edited on
I only see one array in this, and it's already dynamically allocated:

obj=new Account[MAX];
The C++ Standard Template Library has containers just for this use. For a plain Jane array container std::vector would do just fine.

http://www.cplusplus.com/reference/stl/vector/

Thanks for replying back but i set MAX to 200, so therefore i set the memory 200. What i need to do is when it read from the file it should allocated memory depending on how much i read from the file.
Instead of setting MAX like this const int MAX=200; read in a value and set MAX to equal that value. You could rename MAX if you like.
so should it be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
void main(int argc, char *argv[])
{
   int count=0; 
    ifstream infile; 

Account *obj; 

while(!infile.is_open()){
  {
     //code to open 
}

}


Last edited on
Not quite. You need to make the array, and then you can start using it. In your code, you start using the array:
obj[count].assign(last, first, num,bal);

before you make it.
obj=new Account[MAX];

That just doesn't make sense.

While we're here, void main(int argc, char *argv[]) is absolutely wrong in C++ and if your compiler doesn't complain, throw it away and get a better one for free. main returns an int, like this: int main(int argc, char *argv[])

I misunderstood; I thought you were reading in a value that told you how many there would be. What you'll have to do is allocate space for, say, ten, and then if that's not enough, allocate space for ten more, and so on.

Here's some pseudocode:

Allocate an array of ten.
Start reading - if reach ten, create new array of twenty, copy the first ten across, delete original array, and carry on with new array.
If reach twenty, create new array of forty, copy the first twenty across, delete original array, and carry on with new array.
If reach forty, create new array of eighty, copy the first forty across, delete original array, and carry on with new array.
...
and so on.


This would be much, much easier with a proper grown-up C++ container class such as vector, but if you're forced to do it this way, you're stuck with it. If you can use proper containers, just use a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
     int count=0; 
    ifstream infile; 

vector<obj> Account;

while(!infile.is_open()){
  {
     //code to open 
}

while(!infile.eof())
     {
        infile>>last>>first>>num>>bal; 
        obj temp;
        Account.push_back(temp);
        Account[count].assign(last, first, num,bal);     
        count++;
     }

}


Last edited on
so using vector<obj>Account; dynamically allocates the memory.

also say i want to call up a print member class and print what in the vector would i call the member like this and print it.

1
2
3
4
for(int i=0; i<count; i++)
                 {
                    obj[i].print(); 
                 }


or would it be
1
2
3
4
for(int i=0; i<count; i++)
                 {
                    Account[i].print(); 
                 }
Last edited on
Yup. In a vector, the [ ] operator works just as in an array.

Account[i].print(); takes object number i - 1 (just as in an array, the first element is numbered zero) and calls that object's print member function.

so using vector<obj>Account; dynamically allocates the memory.

The vector will take care of it's own memory. You can ask it at any time how big it is, and when you try to add a new object to the end and it needs to expand, it will silently expand so you don't need to worry about it.

Note that if the vector is, say, 10 elements long, and you try to put something in element 11 like this

Account[11] = someObject;

you'll have trouble, just as you would with an array, so if you just want to add to the back, use push_back.

If you've never used vector before, it's well worth seeing all the fabulous things you can do with it:
http://www.cplusplus.com/reference/stl/vector/
Last edited on
when declared
vector<obj>Account;
I keep getting a compiler error obj undeclared.
The bit inside the < > should be the type, and the word after that is what you want to call it. I didn't pay much attention to what your type was actually called, so I probably got them the wrong way round.

If you were making a vector of ints called bananas, for example, it would be

vector<int> bananas;
Last edited on
vector<Account> obj;
how would you free a memory vector<Account>obj;

i know that for array it would be delete[]obj;
It'll be tidied away when it goes out of scope, like everything else on the stack. If you insist on shrinking it before then, you've got resize. http://www.cplusplus.com/reference/stl/vector/resize/

The whole point of using vector, through, is so that you get dynamic memory handling without having to allocate and delete it yourself.
A vector frees it's own memory.

resize don't necessary free any memory
Last edited on
thanks for the help i got it to work.
@Peter87

resize don't decessary free any memory .

what does that mean .. please can you elobrate on this .
@bluecoder

Using resize to decrease the size of the vector will call the destructors for the elements to be removed. The destructors might free memory but the vector will not reallocate it's internal buffer. The capacity stays the same, so for simple types like int and double resize will not free any memory.
Topic archived. No new replies allowed.