pointer question newbie

We have a project and this is the instruction:

Your task is to create a program that manipulates ‘array contents using pointers’. Your program must have at least the following functions:

My question is if thats the instruction, how should I approach the problem with pointers? should I create it as an int array? or create it as vector?

You can do it both ways. In case of arrays, you can use pointers. In case of vectors, you can use iterators. But, it seems that they are talking about arrays. So, try to use them instead of a vector. Also, using array and doing memory management yourself will teach you good things while in case of vector it is done automatically. Try to use dynamic array allocation, if possible, to build a strong base over pointers and check for memory leaks too. :slight_smile:

This is indeed one of my favourite questions or query I should say. Both the data structures are good at their places, but sometimes one acts like cherry over the other :P.

  • Using arrays can sometime produce more succinct code. See the following construct:

     int i,ele;
     int arr[]= {1,2,3,4};   // one line initilisation
  • arrays have fixed size, which can be specified both at compiler time and run-time, provide contigous, indexable sequence of elements. However, we can’t resize them, here we prefer using vectors.

  • They take storage space depending on the scope in which they are declared. If declared in stack portion(locally) of large size, will give SIGSEGV. But vectors grow and shrink dynamically. They automatically manage their allocation and memory, which is freed on destruction. This is not true with arrays, which are required to be explicitly destroyed if dynamically allocated.

  • Arrays can’t be returned from a function but vectrs can be. Even they can be copied/assigned (deep copy).

  • Arrays automatically decay to pointers in many situations, specially when passed to the function. This is very common source of error, which we still commit(even knowing this). Such doesn’t happen with vectors, but we can get a pointer using reference.

  • When an array is dynamically allocated, we need a separate constructor to construct the array(either use malloc with initlisation or use calloc). Vectors are good at this, they don’t need defalut constructor.

  • It’s much harder to make exception-safe code with raw arrays allocated via new or malloc (in part because it’s really easy to forget that you need to worry about it). In practice, std::vector is implemented as a flat array. As such, it’s always possible to pull out the raw array and use C-style access patterns. I typically start with the vector subscript operator syntax. For some compilers, when producing a debug version, vectors provide automatic boundary checking. This is slow (often a 10x slowdown for tight loops), but helpful in finding certain types of bugs.

  • Consider a situation where the entry you want to store in an array is not an integer or float or char i.e. no primitive datatype. You need to store a struct node, or a pair or two entries or anything. This can be easily done using vector , but creates a problem in case of arrays.