length of dynamic array in c++ [duplicate] - c++

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...

Related

Same value every time when want to get size of array [duplicate]

This question already has answers here:
Determine the size of a C++ array programmatically?
(20 answers)
Closed 1 year ago.
I am new to c++, so please do not judge me strictly. I am trying to get size of array, but this code sample always returns 2. Also I can use only C++11. What is the problem and how can I deal with it? Thank you in advance.
String string = "paris";
char * array = new char [string.length() + 1];
int arraySize = sizeof(array) / sizeof(array[0]);
It returns 2 because array is a pointer , meaning sizeof(array) returns the size of the pointer, not the array. You have 16-bit pointers?
sizeof(array) / sizeof(array[0]); only works for compile-time arrays.
Solution is to use std::vector or std::array.
Also use std::size_t for lengths, not int.
You can't find out the size of an array that was created using the keyword new(the way you did). The size of an array allocated using new[] is not stored in any way in which it can be accessed later.
You should instead use std::vector which stores the length for you.
std::string string = "paris";
std::vector<char> myVec(string.length() + 1); //creates vector of size string.length() + 1
int arraySize = myVec.size();

C++ - Arrays of the integer type [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 8 years ago.
I was just curious to know how to find the number of elements of an array of integers.
For character arrays we can loop thorough the array till we reach the null character,but how can I do it for integer array?
#include <iostream.h>
void main()
{
int a[] = {1,2,3,4};
for ( k = 0 ; a[k] ; k++)
cout<<k<<endl;
}
The above code counts from 0 to 8.
-A C++ noob with an open mind
A char array is terminated by 0 by convention. Such an array is called a C-style string, because it's used as a string of characters.
For integers, there is no termination value by convention, and you need to know the length by some other means. If it's your own array, store the length in a variable. If you receive the array from an API, there will be typically be a parameter receiving the length of the array, that you can use.
If the array is a global, static, or automatic variable (int array[10];), then sizeof(array)/sizeof(array[0]) works.
If it is a dynamically allocated array (int* array = malloc(sizeof(int)*10);) or passed as a function argument (void f(int array[])), then you cannot find its size at run-time. You will have to store the size somewhere.
Note that sizeof(array)/sizeof(array[0]) compiles just fine even for the second case, but it will silently produce the wrong result.

how can i know the number of elements in array [duplicate]

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 8 years ago.
i run the following code but it kept printing "4"
why its printing "4" and not "12"? and can I use malloc and then sizeof?(if i can then how)
#include<stdio.h>
int main()
{
int arr1[3]={1,2,3};
int *arr2=arr1,i;
printf("%d",sizeof(arr2));
return 0;
}
Pointers are not arrays. arr2 is a pointer to int. sizeof(arr2) will return size of pointer. To print size of an array, the operand of sizeof must be of an array type:
printf("%u",sizeof(arr1));
can I use malloc and then sizeof?
No. There is no portable way to find out the size of a malloc'ed block. malloc returns pointer to the allocated memory. sizeof that pointer will return size of the pointer itself. But you should note that, there is no need to use sizeof when you allocate memory dynamically. In this case you already know the size of array. (In case of char array use strlen).
Further reading: c-faq: Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer?
sizeof(arr2)
would print the size of pointer as it's a int*. However, if you try sizeof(arr1), it would print
sizeof(element_type) * array_size
i.e size of array. Remember that it's not taking into account how many elements are there in array. It would just consider how many elements can array store.
arr2 is a pointer and you are printing sizeof(pointer)
sizeof(arr1) will give you sizeof array which might give you 12.(Given your integer is 4 bytes)
It's printing 4 because arr2 is a pointer, and the size of a pointer is 4 bytes in 32bit architectures. You can't know the size of a dynamically allocated array ( array allocated with malloc ) given just a pointer to it.

int a[0]; What is the use of this statement? Where does it makes sense? [duplicate]

This question already has answers here:
Flexible Array Member (Zero Length Array) [duplicate]
(2 answers)
How to include a dynamic array INSIDE a struct in C?
(8 answers)
Closed 8 years ago.
I have seen the usage of this statement in driver programs at the end of the structure.
Can anyone explain me what is the use of this statement? and how does it works internally?
I mean will compiler considers it as array or a variable?
In C, it's a trick to allow you to put a variable-sized array at the end of a structure, by allocating enough memory for both the fixed-sized fields and whatever you want in the array. For example:
struct array {
size_t size;
int a[]; // strictly, it should be incomplete rather than zero sized
};
struct array * make_array(size_t size) {
struct array * array = malloc(sizeof (struct array) + size * sizeof (int));
array->size = size;
return array;
}
struct array * array = make_array(2);
array->a[1] = 42; // No problem: there's enough memory for two array elements
In C++, it's not valid. Use std::vector instead.
While arrays of size 0 are not supported by either standard, many compilers allow them as an extension. The standardised C way (C99+) instead leaves out the size altogether.
Thiis is used to describe a data-structure consisting of the starting fields and a variable number of array elements, as well as for comfortable access to them.

sizeof works unusual at c++ after array creation [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 am creating an array by using following code
float *A;
A = (float *) malloc(100*sizeof(float));
float *B;
B = (float *) malloc(100*sizeof(float));
but after these when I type an print the size of the A and B by the following, I get 2 as a result as I expect to see 100.
sizeof(A)/sizeof(float)
Your expectation is wrong. A is a float*, so its size will be sizeof(float*), regardless of how you actually allocate it.
If you had a static array - i.e. float A[100], then this would work.
Since this is C++, use std::array or std::vector.
Worst case, use new[]. Definitely don't use malloc.
This works only for static arrays, defined in the current scope.
All you get in your example is the size of a pointer to float divided by the size of float.