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.
Related
This question already has answers here:
how to create a contiguous 2d array in c++?
(7 answers)
Closed 3 years ago.
OK, this question seems to be silly but bear with me. When I trying to create a 2D array in C++, it gave me some warnings (len is an integer):
double a[len][len];
// warning: variable length arrays are a C99 feature
// warning: variable length array used
So I tried another:
double **a = new double[len][len];
// error: only the first dimension of an allocated array may have dynamic size
// read of non-const variable 'len' is not allowed in a constant expression
How can I do it correctly in C++11?
double** a=new double*[len];
for(int i=0;i<len;++i)
{
a[i]=new double[len];
}
Are there any restrictions on what you can use? If you're planning to do array manipulations I'd say just use [Eigen] (http://eigen.tuxfamily.org/index.php?title=Main_Page)
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.
This question already has answers here:
Quick strlen question
(9 answers)
Closed 9 years ago.
I am writing simple function for merging two C-strings. While trying to dynamically alocate new chunk of memory i found out that :
char * out = new char[size];
std::cout << strlen(out) <<std::endl;
Returns 16 when size is 4 and 40 when size is 22 and so on.
Does anyone have an idea why it acts like that and how to allocate memory for c-string with certain number of chars?
The allocated memory is not initialized. strlen() just keeps charging through memory until it encounters a null character ('\0'). Since you have not initialized the memory pointed to by out, the contents of this memory are undefined, and therefore so is the behavior of strlen().
strlen() does not measure the size of an allocation, so your interpretation that new is allocating too much memory is incorrect simply because the tool you are using to measure the size of the allocation doesn't do that at all.
In your case, strlen() is reading beyond the boundary of this allocation. In this circumstance, the result could be a "wrong" value (as you see here) or your program could simply crash.
Using new to allocate memory does not initialize the memory:
char * out = new char[size];
// Add this code to initialize out
memset(out,'A',size-1);
out[size-1]='\0';
std::cout << strlen(out) <<std::endl;
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 10 years ago.
I have the below code:
int* d = (int*) malloc(100 * sizeof(int));
cout<<"size of d which is pointer is: " << sizeof(d)<<endl;
I know that sizeof outputs 4 as d is a ptr. But, how can I find the sizeof the entire array using sizeof
You cannot - sizeof is a compile time operation and hence not dynamic.
As you are using c++ use std::vector instead. Otherwise create a structure to store both the pointer and the size of the array. Pass that around instead.
The pointer gives you the place in memory where your data is stored which is why you can't get it's size only from that information.
It's analogous to knowing how big my house is from knowing my address (without prior knowledge or the use of tools like Google Maps)
The direct ans. is no you can't but you can try this :
int x[]={1,2,3,4};
int *ptr=x;
decltype(sizeof(*ptr)) size=0;
while(*ptr<5){
size =size+sizeof(*ptr);
ptr++;
}
cout<<"Size is : "<<size;
Output:
Size is:16
You could argue that you already know the size of the entire array using sizeof - you've got it in your original malloc call - 100 * sizeof(int). Although the malloc machinery must know the amount of memory associated with the pointer internally (it needs it for a corresponding free call), and apparently some implementations provide functions to return this information, as far as I know there is not implementation-independent and portable way of doing this without handling it yourself.
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...