Syntax of arrays of pointers in c++ - c++

I am new to c++ programming and trying to understand this syntax
int **arr;
arr = new int *[5];
I am confused about this part new int *[5]; Does it mean pointer of type int to 5 subpointers?
Any help will be appreciated.

This will hopefully become clear when you read it piece by piece:
new int * [5]
^^^ ^^^^^ ^^^
give me pointers to int and five of those.
Then you safe the address of the first of the new pointers in arr and thus have an dynamically allocated array of five pointers.
Note that "in the real world" (i.e. if no teacher/prof forbids you to) you would use an std::vector instead of new[].

You are declaring an array of pointers to integers.
The array capacity is 5 (pointers).
The variable is allocated in dynamic memory because of new.

When you have problem with a non trivial type use typedef (at least in your head):
typedef int* int_ptr;
int_ptr *arr;
arr = new int_ptr[5];
It is easier to understand now, is it not?

Related

"ABORT CALLED" in hacker rank [duplicate]

Possible Duplicate:
is this a variable or function
I mistakenly used something like:
int *arr = new int(100);
and it passes compile, but I knew this is wrong. It should be
int *arr = new int[100];
What does the compiler think it is when I wrote the wrong one?
The first line allocates a single int and initializes it to 100. Think of the int(100) as a constructor call.
Since this is a scalar allocation, trying to access arr[1] or to free the memory using delete[] would lead to undefined behaviour.
Wikipedia new(C++) quote:
int *p_scalar = new int(5); //allocates an integer, set to 5. (same syntax as constructors)
int *p_array = new int[5]; //allocates an array of 5 adjacent integers. (undefined values)
UPDATE
In the current Wikipedia article new and delete (C++) the example is removed.
Additionally here's the less intuitive but fully reliable C++ reference for new and new[].
It allocates one object of type int and initialized it to value 100.
A lot of people doesn't know that you can pass an initializer to new, there's a particular idiom that should be made more widely known so as to avoid using memset:
new int[100]();
This will allocate an array of int and zero-initialize its elements.
Also, you shouldn't be using an array version of new. Ever. There's std::vector for that purpose.
The first one creates a single new integer, initializes it to the value 100 and returns a pointer to it.
In C/C++ there is no difference between a pointer to an array and a pointer to a single value (a pointer to an array is in fact just a pointer to its first element). So this is a valid way to create an array with one element.

I have an array of pointers. How to correctly assign a value to the locations each element of the array points to?

I have this code declaring an array of pointers and assigning value of 5 to the location that the first pointer in the array points to:
int *p[10];
*p[0]=5;
However, this is showing an EXC_BAD_ACCESS error. I tried
int **p = new int *[10];
*p[0]=5;
But this is giving the same error. How do I assign a value to the location pointed by an element of my array of pointers? Thank you.
Before dereferencing the first pointer in the array, you need to initialize it to point to a valid memory location. For example:
int *p[10];
p[0] = new int;
*p[0] = 5;
Note that manual memory allocation at such level is almost certainly not what you want to use, except as a learning exercise, or as part of implementation of a larger structure. If you in fact want to an array of 10 integers, and access parts of it through a pointer, you can do it like this:
int array[10]; // allocate room for ten integers
array[0] = 5; // initialize the first one
int *first = &array[0]; // get a pointer to the first one
assert(*first == 5); // work with the pointer
If you do need an array of pointers, look into std::unique_ptr and std::shared_ptr to make sure they do not leak data in case of exceptions, early returns or forgotten delete.

Allocating memory for array of pointers of int dynamically

If I want to dynamically allocate memory for array of pointers of int, how can I achieve this requirement?
Suppose I declare an array of pointers of int like this:
int (* mat)[];
Is there a way I can allocate memory for K number of pointers dynamically and assign it to mat? If I do
mat = new int * [K];
It gives error : cannot convert 'int**' to 'int (*)[]' in assignment. I understand this memory allocation is implicitly got converted to int **. Is there any way to allocate memory for above scenario?
Even when I try to assignment of statically allocated array of pointers of int to array of pointers of int, like this:
int (*mat)[] = NULL;
int (* array_pointers)[26];
mat = array_pointers;
Compilation gives this error: cannot convert 'int (*)[26]' to 'int (*)[]' in assignment.
Can someone please explain to me why this is an error or why it should be an error?
In C, int (* mat)[]; is a pointer to array of int with unspecified size (not an array of pointers). In C++ it is an error, the dimension cannot be omitted in C++.
Your question says new int *[K] so I assume this is really a C++ question. The expression new T[n] evaluates to a T * already, there is no implicit conversion.
The code to allocate an array of null pointers using new is:
int **mat = new int *[10]();
Then mat points to the first one of those. Another option (less commonly used) is:
int * (*mat)[10] = new int *[1][10]();
where *mat designates the entire array of 10 pointers.
NB. This sort of code is not useful for anything except demonstration purposes perhaps, whatever you are trying to do has a better solution.
In C and C++, in some cases, arrays and pointers are interchabgable.
int*[] decays in to int**.
To dynamically allocate an array, use:
int* mat;
// ...
mat = new int[size];
// ...
delete[] mat;
Note that you need to delete all memory you take with new, or else you'll leak memory.
It's not always easy to do.
C++ has a thing called RAII to help.
To use RAII (meaning all resources taken in a scope will be released when the scope ends), put the new in a classes constructor, and delete in the destructor.
The STL has some comtainers ready for you to use.
Look in to std::vector.
If you want an array of pointers as opposed to a dynamic array, the syntax is more like
int** arr = new int[size];
arr[i] = &val;
// ...
delete[] arr;
Don't forget to initialize each pointer in the array!
If size is known at compile time, this can be rewritten as:
std::array<int*, size> arr;
arr[i] = &val;
No need to delete, and it checks overflows.
If size is unknown at compile time, you can do:
std::vector<int*> arr;
arr.resize(size);
arr[i] = &val;

What does int **p in mean?

I understand that we use this when we need to give a pointer to an array of pointers in the dynamic memory but what I don't understand is that how this works in the stack.
Does this make an array of pointers in the stack too that is pointing to the array of pointers in the heap or does it make a single pointer in the stack that is pointing to the array of pointers in the heap? if yes, then what is the difference between
int **p = new int*[100]
and
int *p = new int[100]
Thanks in advance. I have been trying to understand this for a long time now and have read a lot of documentation online but I still don't understand this.
int **p declares a pointer on the stack which points to pointer(s) on the heap. Each of that pointer(s) point to an integer or array of integers on the heap.
This:
int **p = new int*[100];
means that you declared a pointer on the stack and initialized it so that it points to an array of 100 pointers on heap. For now each of that 100 pointers point nowhere. By "nowhere" I mean that they point neither to a valid chunk of memory, nor they are nullptrs. They are not initialized, thus they contain some garbage value which was in the memory before the pointers were allocated. You should assign something sensible to them in a loop before usage. Note that p[0] - p[99] pointers are not guaranteed to point to adjacent regions of memory if you assign return values of new to them. For example, if you allocate memory for each of them as p[i] = new int[200];, p[0][201] will not reference p[1][2], but will lead to an undefined behavior.
And this:
int *p = new int[100];
is a pointer on the stack which points to an array of 100 integers on the heap.
Don't worry, pointers and arrays in C are always a source of confusion. In general, when you declare an array of, say type int, you create a pointer of type int that points to the first element in a contiguous block of memory that will store ints. For example, if I have a simple array of ints using int *p_to_intarr = new int[3], I get this:
+++++++ <--------- p_to_intarr
| int |
+++++++
| int |
+++++++
| int |
+++++++
In general, if I want an array of type T, I create a pointer to type T like T *ptr_to_Tarr = new T[3].
So what if I want an array of array of ints? Lets just replace the T in with the type of an "array of ints" and this will give us an array of "array of ints". Well we just say in our first example that the type of an array of ints in int *, and so an array of array of ints would be: int* *ptr_to_arrayofintarr = new int*[3]. Note we just replaced the T with int star. This is often written more neatly as int **ptr_to_arrayofintarr = new int*[3].
So int **p could be a pointer to a 2d array. It could also be a reference to a 1d array; depends on the specific case :)
The new expression evaluates to a pointer of some type, pointing to memory that has been allocated in the free store (essentially the heap) but not necessarily in the free store. (It can still be in the free store depending on the context; for instance, consider an initialization-list in a constructor for an object being allocated in the free store.)
The object(s) initialized by new is/are of course in the free store.
In the assignment statements you've shown, you can see the type of the pointer returned by new on the left side of the equals sign, and the type of the free store object(s) to the right of the new. Thus the only object that is locally evaluated (i.e. might be on the stack) is, in the first case, a pointer-to-pointer-to-int, and in the second case, a pointer-to-int. The objects in free space are an array of pointers-to-ints in the first case and a simple array of ints in the second.
Note that just because the array in the first assignment consists of pointers doesn't mean that the pointers themselves actually point to anything yet; new does not magically recursively allocate free space for objects to be targeted by any pointers in an array it creates. (This wouldn't make much sense anyway.)
The ** means that you have a pointer to pointers. In more practical terms, it means you have a two-dimensional array.
Since each element of the array is also a pointer, you need to initialize those pointers as well:
for (int i = 0; i < 100; ++i)
p[i] = new int[200];
This initializes a 100x200 array. You can access the bottom-right corner with p[99][199].
When it's time to delete the pointer you have to reverse the process:
for (int i = 0; i < 100; ++i)
delete [] p[i];
delete [] p;

Whats the difference? Pointer to an array vs regular array

I'm familiar with Java and trying to teach myself C/C++. I'm stealing some curriculum from a class that is hosting their materials here. I unfortunately can't ask the teacher since I'm not in the class. My concern is with the section under "dynamically declared arrays":
If you
want to be able to alter the size of
your array at run time, then declare
dynamic arrays. These are done with
pointers and the new operator. For the
basics on pointers, read the pointers
section.
Allocate memory using new, and then
you access the array in the same way
you would a static array. For example,
int* arrayPtr = new int[10]; for
(int i = 0; i < 10; i++) {
arrayPtr[i] = i; }
The memory picture is identical to the
static array, but you can change the
size if you need to. Don't forget you
must deallocate the memory before
allocating new memory (or you will
have a memory leak).
delete [] arrayPtr; // the []
is needed when deleting array pointers
arrayPtr = new int[50]; . . .
When you're completely done with the
array, you must delete its memory:
delete [] arrayPtr;
Dynamic multi-dimensional arrays are
done in a similar manner to Java. You
will have pointers to pointers. For an
example, see a
My understanding is that an array in C is simply a reference to the memory address of the first element in the array.
So, what is the difference between int *pointerArray = new int[10]; and int array[10]; if any?
I've done some tests that seem to indicate that they do the exact same thing. Is the website wrong or did I read that wrong?
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
// Initialize the pointer array
int *pointerArray = new int[10];
for (int i = 0; i < 10; i++){
pointerArray[i] = i;
}
// Initialize the regular array
int array[10];
for (int i = 0; i < 10; i++){
array[i]= i;
}
cout << *(pointerArray + 5) << endl;
cout << *(array + 5) << endl;
cout << pointerArray[5] << endl;
cout << array[5] << endl;
cout << pointerArray << endl;
cout << array << endl;
return 0;
}
Output:
5
5
5
5
0x8f94030
0xbfa6a37c
I've tried to "dynamically re-size" my pointer array as described on the site, but my new (bigger) pointer array ends up filled with 0's which is not very useful.
int array[10]; declares the array size statically, that means it is fixed - which is the only major difference. It also might be allocated to be inside the function's stack frame, i.e. on the program's stack. You do not need to worry about using delete [] on that kind of array, in fact, you might crash the program if you delete it.
When you use operator new, you allocate memory dynamically which could be slower and the memory usually comes from the heap rather than the program's stack (though not always). This is better in most cases, as you are more limited in the stack space than the heap space. However, you must watch out for memory leaks and delete[] your stuff when you don't need it anymore.
As to your array being filled with zeros, what your class material does not say is that you have to do this:
int *arr = new int[20]; // old array
//do magic here and decide that we need a bigger array
int *bigger = new int[50]; // allocate a bigger array
for (int i = 0; i < 20; i++) bigger[i] = arr[i]; // copy the elements from the old array into the new array
delete[] arr;
arr = bigger;
That code extends the array arr by 30 more elements. Note that you must copy the old data into the new array, or else it will not be there (in your case, everything becomes 0).
My understanding is that an array in C is simply a reference to the memory address of the first element in the array.
So, what is the difference between int *pointerArray = new int[10]; and int array[10]; if any?
What you mention is the reason for much confusion in any C/C++ beginner.
In C/C++, an array corresponds to a block of memory sufficiently large to hold all of its elements. This is associated to the [] syntax, like in your example:
int array[10];
One feature of C/C++ is that you can refer to an array by using a pointer to its type. For this reason, you are allowed to write:
int* array_pointer = array;
which is the same as:
int* array_pointer = &array[0];
and this allows to access array elements in the usual way: array_pointer[3],
but you cannot treat array as a pointer, like doing pointer arithmetics on it (i.e., array++ miserably fails).
That said, it is also true that you can manage arrays without using the [] syntax at all and just allocate arrays by using malloc and then using them with raw pointers. This makes the "beauty" of C/C++.
Resuming: a distinction must be made between the pointer and the memory that it points to (the actual array):
the [] syntax in declarations (i.e., int array[10];) refers to both aspects at once (it gives you, as to say, a pointer and an array);
when declaring a pointer variable (i.e., int* p;), you just get the pointer;
when evaluating an expression (i.e., int i = p[4];, or array[4];), the [] just means dereferencing a pointer.
Apart from this, the only difference between int *pointerArray = new int[10]; and int array[10]; is that former is allocated dynamically, the latter on the stack.
Dynamically allocated:
int * pointerArray = new int[10];
[BTW, this is a pointer to an array of 10 ints, NOT a pointer array]
Statically allocated (possibly on the stack):
int array[10];
Otherwise they are the same.
The problem with understanding C/C++ arrays when coming from Java is that C/C++ distinguishes between the array variable and the memory used to store the array contents. Both concepts are important and distinct. In Java, you really just have a reference to an object that is an array.
You also need to understand that C/C++ has two ways of allocating memory. Memory can be allocated on the help or the stack. Java doesn't have this distinction.
In C and C++, an array variable is a pointer to the first element of the array. An array variable can exist on the heap or the stack, and so can the memory that contains its contents. And they can be difference. Your examples are int arrays, so you can consider the array variable to be an int*.
There are two differences between int *pointerArray = new int[10]; and int array[10];:
The first difference is that the memory that contains the contents of the first array is allocated on the heap. The second array is more tricky. If array is a local variable in a function then its contents are allocated on the stack, but if it is a member variable of a class then its contents are allocated wherever the containing object is allocated (heap or stack).
The second difference is that, as you've realised, the first array is dynamic: its size can be determined at run-time. The second array is fixed: the compiler must be able to determine its size at compile time.
First, I'd look for some other place to learn C++. The page you cite is
very confusing, and has little to do with the way one actually programs
in C++. In C++, most of the time, you'd use std::vector for an array,
not the complex solutions proposed on the page you cite. In practice,
you never use operator new[] (an array new).
In fact, std::vector is in some ways more like ArrayList than simple
arrays in Java; unlike an array in Java, you can simply grow the vector
by inserting elements into it, preferrably at the end. And it supports
iterators, although C++ iterators are considerably different than Java
iterators. On the other hand, you can access it using the []
operator, like a normal array.
The arrays described on the page you cite are usually called C style
arrays. In C++, their use is mostly limited to objects with static
lifetime, although they do occasionally appear in classes. In any case, they are never allocated dynamically.
The main difference is that some operations that are allowed on pointers are not allowed on arrays.
On the one hand:
int ar[10];
is using memory allocated on the stack. You can think of it also locally available and while it is possible to pass a pointer / reference to otehr functions, the memory will be freed as soon as it goes out of scope (in your example at the end of the main method but that's usually not the case).
On the other hand:
int ar* = new int[10];
allocates the memory for the array on the heap. It is available until your whole program exits or it is deleted using
delete[] ar;
note, that for delete you need the "[]" if and only if the corresponding new has had them, too.
There is a difference but not in the area that you point to. *pointerArray will point to the beginning of a block of memory of size 10 bytes. So will array. The only difference will be where it is stored in memory. pointerArray is dynamically assigned memory (at run-time) and hence will go on the heap, while array[10] will be allocated at compile-time and will go to the stack.
It is true that you can get most of array functionality by using a pointer to its first element. But compiler knows that a static array is composed of several elements and the most notable difference is the result of the sizeof operator.
sizeof(pointerArray) = sizeof int*
sizeof(array) = 10 * sizeof int