# using c++ pointers for dynamic arrays



## Sphinx (Aug 5, 2003)

ok so i have this text file that holds a certain amount of integers (I don't know how many there will be, thus i must use dynamic arrays).

so i have;

int main(){

ifstream input;
input.open("inputfile.txt");

int r,z=0;

while(input >>r){
z++;
}

int * b = new int[z];

}

Thats what i have so far. I go through the file to see how many integers are in it, then I allocate a chunk of memory to an integer array of size z (by using a pointer).

Now this is my problem. I need to go back through the input file and put the actual integers into this array, but I cannot use loop counter variables -- EVERYTHING has to be done with pointers. I then need to find the min number, max number, and median of the array -- all in pointers. I am having trouble thinking about how to go about this.

Appreciate any help,
thanks.

}


----------



## cpscdave (Feb 25, 2004)

create a linked list. 
Bassically what a linked list is you create an object Node that has 2 or 3 pointers in it. 

First pointer points (or you can use an acutaly varible to store the information) to whatever you want to store in the list. You case would be integers. 

The 2nd pointer points to the next Node. The 3rd optional pointer points to the previous Node. having this has trade offs. It takes less operations to add/remove nodes in the middle of the list. However it requires more storage. 

then you only track explicitaly The Head or start of the list. 

Google linked lists for more information, examples and psudeo code on how to do inserts/removals, searches etc.


----------



## VTfletch (Jan 25, 2007)

I would suggest not going through the array like that initially. A better way would be to create an initial array for storing the integers with some generic size. Then as you loop through the input enter the values into the array and if the array runs out of space create a new array with a larger size and copy the contents of the old array into the new array and redirect the pointer. Then to find the min, median and max, sort the array once all the values are placed and you should be able to find those values in a snap. I hope all of this makes sense to you, if not just ask.


----------



## cpscdave (Feb 25, 2004)

The problem with that method VTfletch is that the complexity is O(N^N) plus then the sort.

If you want to have better access times load the data into a linked list first. Then allocate an array of needed size copy the values over either doing an insertion sort or copy then quick sort


----------



## cpscdave (Feb 25, 2004)

actually come to think about it if you needed the number sorted at the end instead of using a "linked list" use a b-tree. Then its being sorted as you build the tree.


----------



## VTfletch (Jan 25, 2007)

cpscdave I totally agree with the link list or b-tree method. I was going directly from the initial post which seemed like a school project, thus probably requires dynamic arrays. No offense to Sphinx if I'm making an incorrect assessment.


----------



## cpscdave (Feb 25, 2004)

Oh I'm sorry you're completly right. I suppose I should've read his entire post


----------



## Shadow2531 (Apr 30, 2001)

Sphinx said:


> EVERYTHING has to be done with pointers.


Really? You can't just array[pos] to fill the array from the file?

If you are going to read the file to get the size, reread it and then fill the array using pointers, something like the following might work:

nums.txt

```
8 0 22 17 66 255 33 22 85
43 5 9
6
1000 2058
99
```
nums.cc

```
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream in("nums.txt");
    // any problems opening the file?
    if (!in) {
        return 1;
    }
    size_t size = 0;
    for (int r; in >> r; ++size);
    int* arr = new int[size];
    // Did the allocation succeed?
    if (!arr) {
        return 1;
    }
    in.clear(); // clear file state so you can reset the position
    in.seekg(0); // set the position back to zero so you can read again
    
    // start off with a pointer to the beginning of the array
    int* iter = arr;
    /*
     since you ran through the file once and allocated the proper size,
     you can just increment the pointer as you go, to move to the next
     spot in the array. Assuming the file didn't magically change 
     content, you'll fill up the array just right without running
     past it.
     
     '*' dereferences the pointer so you can set r.
    */
    for (int r; in >> r; ++iter) {
        *iter = r;
    }
    //iter = arr;  reuse the iterator and set it back to the beginning of the array.
    // So you don't run over, stop when the pointer goes past the last element.
    for (iter = arr; iter != &arr[size]; ++iter) {
        cout << *iter << "\n";
    }
    delete [] arr; // free up memory of the array you new'd
}
```
That should give you an idea how to do iterators. From there you can do the sorting etc.

You should reimplement it in your own style. And, don't just assume that I got everything right. Go through it and check.


----------



## blaqDeaph (Nov 22, 2005)

Actually, the thing about pointers is that since it is basically a memory address (hence it's a number), you can do pointer arithmetic to figure out the position of everything.

Note, I wouldn't recommend you read any further unless using only pointers is a REQUIREMENT...

Important things to know:

Size of integer = 4 bytes

If the first address is 1000, then the first item will be at location 1000, the second item at 1004, 1008 etc.

to find the nth item of the array, assuming we are starting from 0:

starting_address+4*n

If you're working with something other than integers (doubles, floats), then you need to replace 4 with the coresponsing size of the data that you are handling..

Note that it is very easy to cause the program to crash, since you are writing & reading from memory locations...very dangerous and highly NOT recommended.


----------

