So I have this code in here I've try to create a 2-D array with different sizes,
first I declare 3 pointers then I assign different arrays to them and hope it works and it did well, but there is the problem , in the second code the compiler gives an error ( a value of type int* cannot be assigned to an entity type int ) so it means they are no longer pointers I think , but why is that , what am I missing here ? what is the biggest difference in these two codes other than one of them is declared in stack and other is on the heap
int main()
{
int* arr[3];
arr[0] = new int[5];
arr[1] = new int[2];
arr[2] = new int[6];
delete[] arr[0];
delete[] arr[1];
delete[] arr[2];
}
//2nd Code
int main()
{
int* arr = new int[3];
arr[0] = new int[5];
arr[1] = new int[2];
arr[2] = new int[6];
}
and sorry for my baddd English
In the first program there is declared an array of pointers
int* arr[3];
In the second program there is allocated an array of integers
int* arr = new int[3];
So for example the expression
arr[0]
has type int.
If you want to allocate an array of pointers you should write
int ** arr = new int *[3];
^^ ^
In this case the expression
arr[0]
has type int * and you may write
arr[0] = new int[5];
Related
int **v = new int*[n];
I'm confused as to what this does? could someone please explain?
This allocates an array of n pointers to int. A pointer to the first element in this array of pointers is stored in v. It is a double pointer, such that accessing an element via v[i] returns a stored pointer from the array.
it's not correctly complete:
int **v = new int*[n];
we'd think this means to allocates dynamically array of integer array on memory, so the dimension must suit it
statically equivalent
const size_t n=5;
int *v[n] = {} ;
// or
int v[][n] ={ {1,2,3,4,5}, {6,7,8}, {9,8,7,6,5} }; //2 dimensional array
as max size of first dimension is inferred automatically to be 3 but
I guess IMHO, this is not yet given,
int **v = new int*[n];
so it'd be specified as
int **v = new int*[n * 3];
When I make an array of integer pointers, I tried this.
int *arr = new int*[10];
This did not work but the following worked.
int **arr = new int*[10];
Why do we need double pointer?? And when I deference it, I had to do the following.
cout<<arr[0];
Why we do not need * in front of arr??
thanks!
new int*[10] allocates an array of ten pointers, and it yields a pointer to the first element of that array. The element type is itself a pointer, that's why you end up having a pointer to a pointer (to int), which is int**. And obviously int** isn't convertible to int*, so you have to declare arr with the appropriate type.
You are not just "making an array of integer pointers": you are dynamically allocating them.
Just like when you dynamically allocate an array of integers you get a single pointer through which to access them:
int* ptr = new int[5];
when you dynamically allocate an array of pointers-to-integer you get a single pointer through which to access those, too; since your element type is int*, adding the extra * gives you int**:
int** ptr = new int*[5];
As for dereferencing, I'm not quite sure what you're asking but that's just how the [] operator works; it adds n to a pointer then dereferences it:
int* ptr = new int[5];
*(ptr+1) = 42; // identical
ptr[1] = 42; // to this
If you forget dynamic allocation and just make a nice array, it's all much simpler:
int* array[5];
std::cout << array[0];
This statement is an expression for an 1D array of int
int* arr = new int [10]; // pointer to array of 10 int
This statement is an expression for a 2D array of int
int** arr = new int* [10]; // pointer to 10 pointers to arrays of 10 int
To populate the 1D array, you need to do this...
for (int i = 0; i < 10; i++) {
arr[i] = val; // where val can be any integer
}
To populate the 2D array, you need to do this...
int** arr2 = new int*[10];
for (int i = 0; i < 10; i++) {
arr2[i] = new int[10];
for (int j = 0; j < 10; j++) {
arr2[i][j] = val; // where val can be any integer
}
}
The * symbol between the variable type and the variable name is syntax for pointer. It changes type from int to pointer of int.
For a pointer to an int, I can do -
int *p = new int;
*p = 10;
delete p; // Step 1: Memory Freed
p = 0; // Step 2: Pointer set to NULL
Now, if I have a pointer to an int array -
int *p = new int[10];
p[1] = 1;
p[5] = 5;
delete[] p; // Step 1: Memory freed corresponding to whole array
Now, how to achieve 'Step 2' for this case?
You don't have an array of int pointers. You just have an array of int. Since you only have one pointer, p, you can just do the same thing as you did previously:
p = 0; // or nullptr, preferably
If you did have an array of int pointers, you will probably have allocated them in a loop. In the same way, you can deallocate them and set them to 0 in a loop:
int* array[10];
for (auto& p : array) {
p = new int;
}
// Some time later...
for (auto& p : array) {
delete p;
p = 0;
}
Do consider whether you need to set your pointers to null after deleteing them.
I've been trying to make a program that adds 2 arrays of different size. But I would like to know to to dynamically increase the array size capacity? Ex: array[4] then upgrade the size to 2 to make array[6];?
EDIT: WIthout using vectors
I tried creating a new ptr but it does not work. I get the error: Read only variable is not assignable.
int *ptr2 = new int[a2.size];
// new ptr2 copies ptr1
for (int i=0; i<(a1.size); i++) {
ptr2[i] = a1.ptr[i];
}
// we want ptr1 to point to ptr2
for (int i=0; i<(a2.size); i++) {
ptr2[i] += a2.ptr[i];
}
delete [] a1.ptr;
a1.ptr=ptr2;
You can't change the size of the array, but you don't need to. You can just allocate a new array that's larger, copy the values you want to keep, delete the original array, and change the member variable to point to the new array.
Allocate a new[] array and store it in a temporary pointer.
Copy over the previous values that you want to keep.
Delete[] the old array.
Change the member variables, ptr and size to point to the new array and hold the new size.
int* newArr = new int[new_size];
std::copy(oldArr, oldArr + std::min(old_size, new_size), newArr);
delete[] oldArr;
oldArr = newArr;
#include<bits/stdc++.h>
using namespace std;
main(){
int *p = new int[5]; // locate memory in heap
int *q = new int[10];// locate memory in heap
for(int j=0; j<5;j++)
p[j] = j;
for(int i=0; i<5;i++)
q[i] = p[i];
delete []p;//Delete the old array 'p'
p = q; // Assign the pointer of 'q' to 'p'
q = NULL; // delete the location of pointer 'q'
return 0;
}
It may be late too answer but i'll explain some things to you..
Array Size cannot be increase because of contiguous memory allocation in the memory.
For Example => The address of the every location is
arr[5] => [2001,2002,2003,2004,2005]
Now, The main problem is if you allocate it to arr[10] so, as we don't know the next location 2006 will be free .
because array need to be in contiguous so, we won't be able to allocate memory
From my Suggestions use Vector or use dynamic Memory allocation in Cpp
int *old = new int[5];
int *nw = new int[10];
for (size_t i = 0; i < sizeof(old); i++)
nw[i] = old[i];
delete []old;
old = nw;
nw = NULL;
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p,*q;
int i;
p=(int *)malloc(5*sizeof(int));
p[0]=3;p[1]=5;p[2]=7;p[3]=9;p[4]=11;
q=(int *)malloc(10*sizeof(int));
for(i=0;i<5;i++)
q[i]=p[i];
free(p);
p=q;
q=NULL;
for(i=0;i<5;i++)
printf("%d \n",p[i]);
return 0;
}
I need to dynamically create an array of integer. I've found that when using a static array the syntax
int a [5]={0};
initializes correctly the value of all elements to 0.
Is there a way to do something similar when creating a dynamic array like
int* a = new int[size];
without having to loop over all elements of the a array? or maybe assigning the value with a for loop is still the optimal way to go?
Thanks
Sure, just use () for value-initialization:
int* ptr = new int[size]();
(taken from this answer to my earlier closely related question)
I'd do:
int* a = new int[size];
memset(a, 0, size*sizeof(int));
I'd advise you to use std::vector<int> or std::array<int,5>
Value initialize the elements with ()
Example:
int *p = new int[10]; // block of ten uninitialized ints
int *p2 = new int[10](); // block of ten ints value initialized to 0
To initialize with other values than 0,
for pointer array:
int size = 10;
int initVal = 47;
int *ptrArr = new int[size];
std::fill_n(ptrArr, size, initVal);
std::cout << *(ptrArr + 4) << std::endl;
std::cout << ptrArr[4] << std::endl;
For non pointer array
int size = 10;
int initVal = 47;
int arr[size];
std::fill_n(arr, size, initVal);
Works pretty Much for any DataType!
!Be careful, some compilers might not complain accessing a value out of the range of the array which might return a non-zero value
int *a=new int[n];
memset(a, 0, n*sizeof(int));
That sets the all the bytes of the array to 0. For char * too, you could use memset.
See http://www.cplusplus.com/reference/clibrary/cstring/memset/ for a more formal definition and usage.