Using Dynamic Memory in C++

i<3milfs<40

New Member
Joined
Apr 25, 2011
Messages
6
Reaction score
0
Location
Johannesburg, Sandton
Hi, I don't know if this is the right sub-forum to ask this, let me know.

Anyway, I need to know how to read in a list of unspecified length from a file, using c++.
The list will look like this (for instance):
1
2
4
7

But it can be any length.
This is what I have, but it doesn't work:

Code:
int main()
{

    int i = 0;
    int * p;


    p= new (nothrow) int[i];
    ifstream infile ("input.txt");
    while (infile >> p[i])
    {
        ifstream infile ("input.txt");
        infile >> p[i];
        cout << p[i];
        cout << "\n";
        i++;
    }

    return 0;
}

I may be doing it entirely wrong as I just read about dynamic memory and don't have a very good feel for it. Any help would be appreciated.
EDIT: I've come to understand that I can use vectors to do this, solutions using them will also be fine.
 
Last edited:
Wow, your code :p

Anyway if you don't know the length then you need to use a data structure. Either arraylist or linkedlist. Linkedlist is the easiest to code.

Code:
struct node {
 int data;
 node * next;
 node * prev;
}

int main()
{
   node * current = new node();
   current.next = 0;
   current.prev = 0;

   ifstream infile ("input.txt");
   while (infile >> temp)
    {
       current.data = temp;
       current.next = new node();
       current.next.prev = current;
       current = current.next;
       current.next = 0;
    }

 //cleanup

 node * temp;
 for(;;)
 {
    if (temp == 0) break; //done;
    temp = current.prev;
    delete current;
    current = temp;
 }
}

I'm not a C++ programmer by trade, I'm actually a Java programmer. So I don't have a C++ compiler installed on my computer to test this, I just wrote it up here but the theory is sound.

To access the linked list would be similar to the cleanup part. You just use:
node.next and node.prev to move through the links in the linked list.
to access that data for a particular link you use node.data;

Note: This example is actually bad because I use structs and no classes. Ideally the LinkedList should be written as it's own class.

If you want to use arrays instead of a linkedlist then you'll need to traverse the file twice or program an arraylist (preferable with a class).

Google is your friend ;)
 
Last edited:
Actually you could just do this:

Code:
int main()
{
   stack<int> stk; //can't remember how templates work in C++ best have look online

   ifstream infile ("input.txt");
   while (infile >> temp)
    {
       stk.push(temp);
    }

  int * list = new int[stk.size()];
 for(int count = 0; count < stk.size(); ++count)
 {
    list[count] = stk.top();
    stk.pop(); //pop seems to return void. So top must be used to access the element.
 }

 //cleanup
 delete [] list;

}

This one uses the standard library stack implementation. The list will be in reverse order...

If that is not desirable then you may wanna go back to using a linked list but the standard library actually includes a linkedlist also, called list.

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

Once again, I didn't test this code, so may or may not compile... Theory is sound tho.
 
Last edited:
Thanks for the replies gentlemen/women.
I've since settled on a vector which both works and does not look like a horrific pile of ****:

Code:
vector<int> values;
while (!infile.eof())
    {
        infile >> i;
        values.push_back(i);

        cout << i;
        cout << "\n";
    }

PS how does one bypass the word filter around here?
 
"using dynammic" memory and "allocating dynamic memory" are two different questions :D you sure you asked the right one OP ? xD
 
Top
Sign up to the MyBroadband newsletter
X