0

Are there any advantage of using normal arrays over array of pointers(and vice-versa)?

When should I use array of pointers and when should I avoid it?

In college, everyone seems to be crazy about pointers. I know std::vector is an easy way out, but it is prohibited to use in college. We're asked to solve stuff without STL for now.

I got an answer from SO (link), but the answer went way over my head.

For example: Is using int array[] better or is int* parray[] better?

Community
  • 1
  • 1
Parthib Biswas
  • 471
  • 1
  • 7
  • 17
  • 6
    What is a pointer array? – Brian Bi Jul 11 '14 at 22:11
  • 1
    BTW, if they won't let you use the C++ standard library in a C++ course, they are basically making you write C. – Brian Bi Jul 11 '14 at 22:12
  • An array of pointers sounds like a multidimensional array to me... Do you mean: `int * pointer = new int(5)` vs `int array[5]`? – Max Jul 11 '14 at 22:16
  • @JosephMansfield We are strictly asked to use only pointers. – Parthib Biswas Jul 11 '14 at 22:17
  • If the question is whether use arrays of concrete objects or arrays of pointers to this objects. I'll go with the first. Always try to have your memory compact. – 101010 Jul 11 '14 at 22:18
  • @Max Yup. Multi-dimentional array. – Parthib Biswas Jul 11 '14 at 22:18
  • @Parthib Biswas Well, it entirely depends on the context. Sometimes, it can make it easier to hold complex information (example: an array can contain an `x` and `y` index, to store a 2-dimensional map, or information "inside" information). In many cases, it's simply just to make things more logical. Personally, I've come accross cases where an array contains information about a specific object (sort of like classes), and I have several "objects" that I need to iterate through - in that case, multidimensional arrays are great. – Max Jul 11 '14 at 22:20
  • @ParthibBiswas: Did you mean pointer to an array versus normal array ? – Hoang Minh Jul 11 '14 at 22:25
  • @HoangMinh Whoa! Now I'm getting confused with my own question. What I meant is, is using `int array[]` advantageous over `int* parray[]`? – Parthib Biswas Jul 11 '14 at 22:29
  • It´s just different? What do you want to do is the question. – deviantfan Jul 11 '14 at 22:32
  • Those are different things... It's like asking if an enum is better than an object (not quite, but you get the point). If you won't use a multidimensional array, then why make one? Make it as simple as possible, as long as you maintain a logic model. If you don't need multidimensional arrays, then don't use them. – Max Jul 11 '14 at 22:33
  • @deviantfan I was told that if I use array of pointers, I could save memory. :\ – Parthib Biswas Jul 11 '14 at 22:34
  • @ParthibBiswas: ... That much generalized = nonsense. If you want to save some time durations (seconds), use int. If you want to save player numbers in the WM, use int. If you want to save memory addresses, use pointers. In C, pointers are the only way or a memorywise better way to do some programming things, but you can think about it as soon as you got such a situation (changing parameter values in a function is a perfect example, or keeping a 2-dimensional array with different line lenght without wasting memory, or...). Don´t use pointers if you have no reason to. Numbers = int, period. – deviantfan Jul 11 '14 at 22:40
  • In the original question, the example code compares an array of integers to an array of pointers to integers. My guess is that the comparison should have been an array of integers versus a pointer to an array of integers. The pointer could simply be | int * ptrtoint; | or it could be a pointer to an array of specific size, like the last dimension of a matrix: | int (*ptrtoarrayofint)[6]; | – rcgldr Jul 12 '14 at 00:53

1 Answers1

1

int array[] is an array of an int. What it means is it will hold a collection of multiple integer numbers. Imagine it as a place holder that holds a number of integers. When you use int array[] in C++, you must give it a fixed size before you use it:

int array[5]

and the size will be put inside the square bracket [], otherwise it won't compile and will give you error. The disadvantage of using this normal array is you have to know the size of the array first, otherwise the program won't run. What if your estimation size is different from actual use ? What if your estimation is much much larger than the real value ? It will cost you a lot of memory.

int *array[] is not valid in C++. If you want to do a pointer to an array without knwoing the size of the array at run time. Do this:

int *p;
int size;

cout << "How big is the size ?";
cin >> size;

p = new int[size];

That way, you don't need to know the value of size before run time, thus you won't waste memory.

Hoang Minh
  • 1,066
  • 2
  • 21
  • 40