Use `delete []` when `new [0]` [duplicate] - c++

This question already has answers here:
C++ new int[0] -- will it allocate memory?
(6 answers)
Closed 5 years ago.
I dynamically allocated an array (unsigned int n might have been passed as a parameter in a function):
int * ar = new int [n];
When I'm done using it:
delete [] ar;
But, what happens when n = 0?
Is allocate 0 ints the same as not allocating at all?
In which case, do bad things happen when I call delete?

It's ok to new a zero-sized array, and to delete it. This is more of a convenience than anything, as it means you don't have to write separate conditions for the 0 case.

Related

allocate new Zero-sized array can have effective value? [duplicate]

This question already has answers here:
C++ new int[0] -- will it allocate memory?
(6 answers)
Closed 2 years ago.
// new T[0] allocate a zero sized array can have values?
auto pv=new int[0];
cout<<pv<<endl; //0x... ?
*pv=123;
cout<<*pv<<endl; //123 ?
delete[] pv;
Why?
if so, what's difference between new T[0] and new T[1]
Why can I set the value of 0 sized array ...?
It is legal to create a new int[0] (though at first glance it may not appear to be useful!).
However, your use of it is just like any other buffer overrun: your program has undefined behaviour.
C++ does not check array bounds for you. That's your job.
Going past them can appear to work; it can cause a crash; it can instantaneously transport the sun to another part of the galaxy.
Just don't do it.

Another variable as the size of the array [duplicate]

This question already has answers here:
How to create an array when the size is a variable not a constant?
(6 answers)
Closed 3 years ago.
How to use the the value stored in a int variable as the size of my array
eg.
int a = 40;
int b[a]; // a = 40
You can't; not in standard C++ anyway, unless a is a constexpr or const integral type with a positive value.
The best alternative is a std::vector<int> b(a);
Create an array with new[]:
int *b = new int[a];
but remember to delete it later with:
delete[] b;
A better alternative is the std container, like std::vector.

Difference of variable initialization in C++ [duplicate]

This question already has answers here:
When should I use the new keyword in C++?
(12 answers)
Closed 7 years ago.
Is there any difference between these 2 ways of storing an integer?
int X = 100;
and
int *pX = new int(100);
"Is there any difference between these 2 ways of storing an integer?"
Yes, there is a significant difference.
int X = 100;
Initializes a variable X on the stack with the value 100, while
int *pX = new int(100);
allocates memory for an int on the heap, kept in pointer pX, and initializes the value to 100.
For the latter you should notice, that it's necessary to deallocate that heap memory, when it's no longer needed:
delete pX;
The first one is creating a variable on the stack while the second one is creating a variable on the heap and creating a pointer to point at it.

How to create an array with number of elements from a variable? [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 10 years ago.
I have an integer num that was read from a file. I want to create an array with the number of elements being num.
A sample code of what I want to do but doesn't work:
int num;
cin >> num;
int iarray[num];
Arrays in C++ have compile-time bounds.
Use dynamic allocation instead, or a healthy std::vector wrapper around the same process.
dynamic allocation being int * iarray = new int[num];
Just make sure to call delete[] iarray; at some point to free the memory.

length of dynamic array in c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
I declared a dynamic array like this:
int *arr = new int[n]; //n is entered by user
Then used this to find length of array:
int len = sizeof(arr)/sizeof(int);
It gives len as 1 instead of n . Why is it so?
Because sizeof does not work for dynamic arrays. It gives you the size of pointer, since int *arr is a pointer
You should store the size of allocated array or better use std::vector
Because arr is not an array, but a pointer, and you are running on an architecture where size of pointer is equal to the size of int.
Andrew is right.
You have to save n somewhere (depends on where do you use it). Or if you are using .NET you could use Array or List...