Allocating memory for array of pointers of int dynamically - c++

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;

Related

how can i get the size of an array so i can declare said array from a function

const int size = arraySize();
int list[size];
I get the following error:
expression must have a constant value
I know this may not be the best way to do it but it is for an assignment.
You should know that in C++ there are no dynamically allocated arrays.
Also, const variables are not necessarily known at compile time!
You can do one of the following:
Declare the function arraySize as constexpr, assuming you can calculate its value at compile time, or create a constant (again, with constexpr) which represents the array size.
Use dynamically allocated objects, such as std::vector (which is an "array" that can expand), or pointers. However, note that when you are using pointers, you must allocate its memory by using new, and deallocate using delete, which is error prone. Thus, I suggest using std::vector.
Using the first one, we get:
constexpr std::size_t get_array_size()
{
return 5;
}
int main()
{
constexpr std::size_t size = get_array_size();
int list[size];
}
which compiles perfectly.
Another thing which is nice to know is that there is std::array which adds some more functionality to the plain constant-size array.
Try dynamic allocation of this array.
int size = arraySize();
int* list;
list = new int [size];
It should work.
In addition, there is a condensed explanation of how dynamic arrays works: DynamicMemory
PS. Remember to free memory that you dynamically allocated, when it won't be needed:
delete [] list;

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;

Syntax of arrays of pointers in 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?

Does setting an array (int*) to NULL free the memory it occupies?

Say I have an array declared as:
int* array;
and I fill the array with however many integers.
If I later say
array = NULL;
does this free the memory that the numbers in array occupied, or does it just make the array unusable while the memory still lingers?
int* array; is not an array, it's a pointer. You have no array at this point. You can't start doing array[0] = 5; or anything because it doesn't even point at a valid int object, let alone an array of ints.
An array is declared by doing:
int array[5];
This array does have 5 objects ready for you to start assigning to. However, this array must not be deleted. The array was not dynamically allocated.
However, if you were to have done int* array = new int[5];, you would by dynamically allocating an array of 5 ints and getting a pointer to the first element in that array. In this case, you must delete[] array;. If you don't and only set the pointer to NULL or nullptr, you will only leak memory.
If you allocated the memory using new [] (more likely, since the name of the variable is array) or new, this will result in a memory leak.
Setting the pointer to NULL will not release the memory, only reassign the pointer. In other words, you will lose any chance to refer to the previously allocated memory.
Use delete[] to release memory allocated with new[], and delete to release memory allocated with new.
However, consider not using raw pointers and manual memory management at all, they are most often not needed and error-prone. The C++ Standard Library comes with collections and smart pointers that perform memory management under the hood and keep you safe from this kind of mistakes.
When you use the code
int *array;
array = NULL;
you have initialized the pointer. When you assign data to *array
int *array;
array[1] = 3;
you get an undefined behavior, but in most cases an access violation. When you allocate memory before writing and assign NULL to the pointer
int *array;
array = new int[3];
array[0] = 2;
array[2] = 4;
array = NULL;
you get a memory leak. Everything should be fine, when you delete the data:
int *array;
array = new int[3];
array[0] = 2;
array[2] = 4;
delete[] array;
array = NULL;
Setting the array to NULL does not free the memory.
Weather it is usable will depend on the surrounding code and how array is initialized. But you have not provided that information so anything else would be speculation.
Note it is unusual to use pointers in modern C++ code.
You should be looking to use a more appropriate structure. What that is will depend on what your usage us. Based on the name alone array. You should probably be using std::vector<int> (or std::array<int> C++11) or even a plain old C array int array[5].
it will leak the memory and it will only be freed when the process exits.
There is every chance that a segmenation fault may occur.
If you have:
int *array = new int[10];
you need to use
delete [] array;
to free the memory.
Basically follow the rule: Whenever allocating with new XXX deallocate with delete XXXor for array allocation delete [] XXX.
If you just use array = NULL, you only set the pointer to NULL and you will have a memory leak.

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