vector or pointer array??which one is the best

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
HI
I am really trobled with memory management problem.Like this program


#include <iostream>
#include<ctime>
#include <cstdlib>
#include<fstream>


using namespace std;


int main()
{      
	int **alpop;
	int i,rand_index,p,pop;
	int *chrom,temp,job;

	cout<<"enter population size";
	cin>>pop;
	cout<<"enter no of jobs";
	cin>>job;//enter chromosom size
	chrom=new int [job];//dynamic memory allocation

	alpop=new int* [job];
	for(int lp=0;lp<job;lp++)
		alpop[lp]=new int[pop];

	ofstream outfile("C:\\fuad2.OUT");
	outfile<<"population size:"<<pop<<endl;
	outfile<<"no of jobs:"<<job<<endl;
	srand( unsigned(time(0)));

	for(i=0;i<job;i++)
	{
		chrom[i]=i+1;
	}

	for(i=0;i<job;i++){
		cout<<chrom[i]<<"\t";
		outfile<<chrom[i]<<"\t";
	}
	outfile<<endl;

	cout<<endl;

	bool same;
	for(p=0;p<pop;p++)
	{
		for(i=0;i<job;i++)
		{

			do
			{	
				same=false;
				rand_index=(rand()%(job-1))+1;//swapping operation
				temp=chrom[rand_index];
				chrom[rand_index]=chrom[i];
				chrom[i]=temp;
				alpop[p][i]=chrom[i];			
				for(int check=0;check<i;check++){
					if(alpop[p][i]==alpop[p][check]){
						same=true;
						break;

					}
				}

			}while(same);
		}

	}


	for(p=0;p<pop;p++)
	{
		for(i=0;i<job;i++)
		{
			cout<<alpop[p][i]<<"\t";
			outfile<<alpop[p][i]<<"\t";
		}
		cout<<endl;
		outfile<<endl;
	}



	
	// De-Allocate memory to prevent memory leak
	for (int lp = 0; lp < pop; lp++)
		delete [] alpop[lp];
	delete[] chrom; 

	
	outfile.close();



	return 0;

}



[code]







Which one is more flexible?
>pointer and arrays or
>vector
[/code]
Last edited on
Depends on the why you want to use them. If you want to push much new items to the array its difficult. If you push a new item to the array you need to reallocate it and find a new bigger place for it in the memory.

If you don't want to push many items during runtime then maybe you can use an array.

If you want to push many items constantly then you might want to use a linked list.

(not sure how vectors work :/)
Vectors are useful when you don't know the exact amount of items you need.

The "core" of a vector is the push function: it adds an element at the end and increases the vector's size by 1. The inner workings are something like this:

The vector has an array of size 'N' and a variable that keeps its actual size 'n'. When push is called, it checks whether n <= N. If yes, add item to the n'th spot and increase n by one. If not, make a new array of size N*2, copy all N elements to the new array, double N, delete the old array.

Obviously, push is very efficient if n < N. All it does is write a new object to an array slot, and increase a counter by one. However, push is very expensive if n = N, because it needs to allocate new memory and copy N items. Since N doubles every time n = N, the likeliness of this appearing decreases when size increases, which means the "amortized complexity" (average of "infinite" push calls) is constant.

Having all these pointers only makes you code less readable and has a greater chance to make mistakes. In my opinion it's better to use std::vector, and never use pointers for this purpose. You can use arrays if you know the size at compile time though.

For multidimensional arrays I usually use a simple std::vector and index it something like [y * width + x], or puts it inside a wrapper class.
Last edited on
You can use arrays if you know the size at compile time though.

Or a somewhat tight upperbound for the size. Personally, I prefer a bit of memory redundancy over switching to vectors for static content. Vectors are great, but if you don't need them, don't use them.

Then again, my work is hardly ever memory-critical. In the real world, memory redunancy is probably not a good idea.
Topic archived. No new replies allowed.