length of array in c++ - c++

I read to get the length of array in C++, you do this:
int arr[17];
int arrSize = sizeof(arr) / sizeof(int);
I tried to do the same for a string:
where I have
string * arr;
arr = new (nothrow) string [213561];
And then I do
arr[k] = "stuff";
where I loop through each index and put "stuff" in it.
Now I want the size of the array which should be 213561, what's the correct way to do it and why is it so complex in C++?

What you are trying to do cannot work because sizeof works on types at compile-time (and pointer types never hold the size of the array they may be pointing to).
In your case, computing sizeof(arr) returns the size taken in memory by the pointer, not
size of the array * size of a std::string
I suggest you use one of these two options
either use fixed-size arrays (sizeof works)
or vectors (myVector.size() returns what you need)
... unless you have a good reason not to.

The correct way of doing this in C++ is to use a vector. That way you can either specify a size up-front, or resize it as you go.
Specifying size up-front:
using namespace std;
vector<string> arr(213561);
for (vector<string>::iterator p = arr.begin(); p != arr.end(); ++p)
{
*p = "abc";
}
Expanding the vector as you go:
using namespace std;
vector<string> arr; // <-- note, default constructor
for (int i = 0; i < 213561; ++i)
{
// add elements to the end of the array, automatically reallocating memory if necessary
arr.push_back("abc");
}
Either way, the size of the array is found with:
size_t elements = arr.size(); // = 213561

The sizeof method only works as long as your array is really an array, i.e. an object that has the array type. In your first example object arr has type int[17]. It is an array type, which means that you can use the sizeof method and get 17 as the result.
Once you convert your array type T[N] to a pointer type T *, you basically lose your array type. The sizeof method applied to a pointer will not evaluate to the size of the original array.
When you allocate array of type T[N] with new[], the result is a pointer of type T * right away. It is not an array type from the very beginning. The information about array size is lost right away and trying to use the sizeof method with such a pointer will not work. In order to preserve the size information about a dynamically allocated run-time sized array, you have to store it in a separate variable yourself.

Here is how you find the size of an array:
const size_t ARRAY_SIZE = 17;
int array[ARRAY_SIZE];
//...
std::cout << "My array size is: " << ARRAY_SIZE << "\n";
You can put ARRAY_SIZE into a header so that other translation units can access the array size.
If you want a dynamic array, that will grow as needed, try std::vector.

You need to keep track of the length using a separate variable. There is no way of getting the length of an area that you only have a pointer to, unless you store that length somewhere.

You cannot get the length of the allocated array.
What you can do is save it seperately at the time of allocation..
Also, you could check the length of the string (which isn't what you're asking, but still..) using strlen()

In c++ here arr is simply a reference to the first element of the array. In case of dynamic arrays it is not possible.

There is a subtle nuance in both C and C++ with memory allocation. Neither language supports dynamic arrays. Here is what you are seeing:
int ary[17];
int arrSize = sizeof(ary) / sizeof(ary[0]);
Here ary is a true array of 17 integers. The array size calculation works because sizeof(ary) returns the size of the memory block allocated for the entire array. You divide this by the size of each element and violĂ  you have the number of elements in the array.
std::string * arr;
arr = new (std::nothrow) std::string[213561];
In this case arr is a pointer to some memory. The new operator allocates a block of memory large enough to hold 213,561 contiguous std::string objects and constructs each of them into the memory. The arr variable simply points to the beginning of the block of memory. C++ does not track the number of elements that you have allocated. You didn't really create a dynamic array - instead, you have allocated enough memory for a bunch of contiguous objects.
C and C++ both allow you to apply the subscripting operator to a pointer as syntactical sugar. You will see a lot of comments about how arr[0] translates into *(arr + 0). The reality is that allocating memory using the new operator results in a block of memory that is not an array at all. The syntactical sugar makes it look like one. The next thing that you will encounter is that multi-dimensional arrays are similar sugar.
Consider the following snippet. Once you understand what is going on there, you will be a lot closer to understanding how memory works. This is the primary reason why C and C++ cannot tell you how large an array is if it is dynamically allocated - it does not know the size, all that it has is a pointer to the allocated memory.
#include <iostream>
int
main()
{
//
// The compiler translates array subscript notation into
// pointer arithmetic in simple cases so "hello"[3] is
// is translated into *("hello" + 3). Since addition is
// commutative, the order of "hello" and 3 are irrelevant.
//
std::cout
<< "\"hello\"[3] = '" << "hello"[3] << "'\n"
<< "3[\"hello\"] = " << 3["hello"] << "\n"
<< std::endl;
//
// All memory is linear in C or C++. So an 3x3 array of
// integers is a contiguous block of 9 integers in row
// major order. The following snippet prints out the 3x3
// identity matrix using row and column syntax.
//
int ary[3][3] = { { 1, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 } };
for (int r=0; r<3; ++r) {
for (int c=0; c<3; ++c) {
std::cout << "\t" << ary[r][c];
}
std::cout << "\n";
}
std::cout << "\n";
//
// Since memory is linear, we can also access the same
// 3x3 array linearly through a pointer. The inner loop
// is what the compiler is doing when you access ary[r][c]
// above - "ary[r][c]" becomes "*(ptr + (r * 3) + c)"
// since the compiler knows the dimensions of "ary" at
// compile time.
//
int *ptr = &ary[0][0];
for (int i=0; i<9; ++i) {
ptr[i] = i;
}
for (int r=0; r<3; ++r) {
for (int c=0; c<3; ++c) {
std::cout << "\t" << *(ptr + (r * 3) + c);
}
std::cout << "\n";
}
return 0;
}

Related

C++: creating a dynamic array using dynamic memory

I'm trying to create a dynamic array or what should you call that, using pointer, but when I try to cout the length of the array after setting the elements, it gives me 0. I'm not sure what I'm doing wrong here.
Here's the code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int *p = NULL;
int kek = 0;
cin >> kek;
p = new int[kek];
for (int i = 0; i < kek; i++)
{
p[i] = 0;
}
int sizeOfArray = sizeof(p) / 8;
cout << sizeOfArray << endl;
delete[] p;
}
Better use the stl vector, this have the size() method
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> p;
int kek = 0;
cin >> kek;
p.resize(kek);
for (int i = 0; i < kek; i++)
{
p[i] = 0;
}
int sizeOfArray = p.size();
cout << sizeOfArray << endl;
p.clear();
return 0;
}
You are just taking the size of the pointer.
But just use std::vector
You can't use sizeof() to determine the size of a dynamically allocated array because the size can't be determined at compile time, and therefore is not stored anywhere.
When you have a statically allocated array like:
int numbers[40];
The compiler is able to figure out that the size of the block of memory called numbers is 40 items * 8 bytes each = 320 bytes, and determine that a statement like sizeof(numbers) is equivalent to 320, and do the proper substitutions.
But when you have something like
int *numbers = new int[n];
numbers is defined explicitly as a pointer to some memory, and when you do sizeof(numbers), it'll try to evaluate the size of the pointer, which will be 4 or 8 bytes depending on the compiler and platform you're on.
Again, numbers is a pointer, pointing to the first item in a block of memory. There's no easy way to tell which one of the blocks of memory in the computer it's pointing to, and how big the block is in bytes
So that means you'll have to keep track of the size of the array yourself.
You already have the variable kek, so size in bytes should be kek * sizeof(int)
Or like the others have said, you can also use a vector to keep track of the length for you:
vector<int> numbers;
int sizeInBytes = numbers.size() * sizeof(int);
It is not possible to determine the length of a new[]ed array in C++ from a pointer returned by new[]. So, your attempt to "to cout the length" does not really do that. sizeof does not do that and will not help you with that.
The only way to "cout" the size of a new[]ed array is to remember that size and manually carry it from the point where you allocated the array to the point where you need to "cout" the size. If you somehow lose knowledge of that size along the way, you will never be able to restore it.
In your case the size is kek. This is what you "cout".

Are character arrays the only type that have some form of null-termination?

I've read some of my friend's code and seen functions like this:
int foo(int* arr, int n)
{
// ...
}
which he then calls like this:
int myArr [] = {69, 69, 69, 69, 69};
int f = foo(myArr, sizeof(myArr)/sizeof(int));
Now, I understand that sizeof(myArr)/sizeof(int) is dividing the size of myArr in bytes by the size of an int in bytes, thus returning the length of myArray. However, I don't understand how sizeof(myArr) is implemented unless there's some sort of generic null element that terminates arrays and then sizeof(...) works similar to how strlen(...) works:
size_t strlen(char* c)
{
size_t k = 0;
while (*c != '\0')
{
++k;
++c;
}
return k;
}
Now, if sizeof(...) does work similar to that, then I don't see why, when passing an array to a function, you can't simply do
int foo(int* arr)
{
int n = sizeof(arr)/sizeof(int);
// ....
}
which is simpler way of writing functions because the array is essentially being passed in as a single unit that gets unpacked.
My guess is that arrays of non-character type don't have the null-termination property that character arrays do. In that case, how does sizeof(...) work? And what is the point of null-termination in character arrays anyhow? Why are they created differently than any other array?
Anyways, I was wondering whether someone could clear up all the obvious confusion that I have.
sizeof works on arrays because the compiler knows the length at compile time. If you pass that array to a function, it turns into a pointer, at which point the compiler doesn't know the full size of the array anymore.
For example:
#include <iostream>
void printPointerSize(int* a) {
// a is a pointer, and all pointers are 8 bytes (64 bits) on my machine
std::cout << "int* pointer argument has size: " << sizeof a << std::endl;
}
int main() {
// the compiler determines from the initializer that this is an int[5]
int a[] = {1, 2, 3, 4, 5};
// since the compiler knows that a is an int[5],
// then sizeof a is 5 * sizeof int
std::cout << "int[5] has size: " << sizeof a << std::endl;
printPointerSize(a);
}
Output (on a platform with 64-bit pointers and 32-bit integers):
int[5] has size: 20
int* pointer argument has size: 8
Note that if you try to create a function that takes an array as an argument, the compiler will just turn it into a pointer anyway:
void printPointerSize(int a[5]) {
// this will print the size of a pointer,
// not the size of a 5-element int array
std::cout << "int[5] argument has size: " << sizeof a << std::endl;
}
In addition to Brandon's answer, you need to distinguish between array capacity and array size.
Array Capacity
Array Capacity is the maximum number items the array can hold. There can be from 0 to capacity number of items in the array. Which brings up the question, "how many items are in the array?"
Array Size
Array Size is the number of valid items in the array. An array that has a capacity of 20 items, may only have 3 valid items in it.
Example:
char array[20];
array[0] = 'M';
array[1] = 'e';
array[2] = 'a';
array[3] = 't';
The above array has a capacity of 20, but only 4 valid items.
Repeating the question, "How many items are in the array?"
The C++ language does not maintain the number of items in an array. The sizeof operator returns the capacity of an array, but not the size.
The size of an array must be maintained in a separate variable. Some crafty programmers can use an array slot to maintain this value. So when passing an array, you will have to pass: the array (or pointer to it), the capacity and the size. Most starting programmers forget about the size parameter, which leads to many difficult defects that are hard to find.

Defining a constant/declaring an array

I am trying to declare two arrays, one 2D and one 1D. I know the dimensions need to be const values. So the const value is assigned from the return value of a function call. That goes well, but when I use the derived value to declare the array, COMPILE errors! WHY???
Here is my code:
int populateMatrixFromFile(string fname) {
std::ifstream fileIn;
int s = determineDimensions(fname); // return value (CONST INT)
const int size = s; // assign to const
cout << "Value returned from determineDimensions(): " << size << endl;
if (size > 10){
cout << "Maximum dimensions for array is 10 rows and 10 columns. Exiting" << endl;
return 1;
}
fileIn.open(fname.c_str(), ios::in); //opened for reading only.
float aMatrix[size][size]; // ERROR
float bMatrix[size]; // ERROR
BUT it works here:
// assign the pth row of aMatrix to temp
const int alen = sizeof (aMatrix[p]) / sizeof (float);
float temp[alen]; // WORKS!!!
for (size_t i = 0; i < alen; i++) {
temp[i] = aMatrix[p][i];
}
Thanks for all help.
The compiler enforces this rule about a constant size of an array because it allocates the needed memory at compile time. In otherwords, all values needed to calculate the size of the array must be known at compile-time. In your first example, this is not the case, so the compiler complains.
If you really need to have dynamically sized arrays, you should use pointers and the new[] operator to allocate the array. You will also need to remember to use the delete[] operator to return the memory to the system and avoid any memory leaks.
The size of the second dimension, third, etc. in any array is always constant. Period. The standard is very clear about this.
The first dimension (actually the last index) can be variable if you allocate variable on the heap with array form of new, like this:
int size = 50;
float *p = new float[size];
.... do stuffs
delete[] p;
Some compilers allow variable sized arrays on the stack, but it is better not to use this.

Expanding a dynamically allocated array

I have allocated an array as follows.
#include <iostream>
int main() {
const int first_dim = 3;
const int second_dim = 2;
// Allocate array and populate with dummy data
int** myArray = new int*[first_dim];
for (int i = 0; i < first_dim; i++) {
myArray[i] = new int[second_dim];
for (int j = 0; j < second_dim; j++) {
myArray[i][j] = i*second_dim + j;
std::cout << "[i = " << i << ", j = " << j << "] Value: " << myArray[i][j] << "\n";
}
}
// De-allocate array
for (int i = 0; i < first_dim; i++)
delete[] myArray[i];
delete[] myArray;
}
Let's say I want to add a 4th element to the first dimension, i.e. myArray[3]. Is this possible?
I've heard that Vectors are so much more efficient for this purpose, but I hardly know what they are and I've never used them before.
Yes, but in a very painful way. What you have to do is allocate new memory which now has your new desired dimensions, in this case 4 and 2, then copy all the contents of your matrix to your new matrix, and then free the memory of the previous matrix... that's painful. Now let's see how the same is done with vectors:
#include <vector>
using std::vector;
int main()
{
vector< vector <int> > matrix;
matrix.resize(3);
for(int i = 0; i < 3; ++i)
matrix[i].resize(2);
matrix[0][1] = 4;
//...
//now you want to make the first dimension 4? Piece of cake
matrix.resize(4);
matrix[3].resize(2);
}
HTH
edit:
some comments on your original code:
In C++ ALL_CAP_NAMES usually refer to macros (something you #define). Avoid using them in other contexts
why do you declare FIRSTDIM and SECONDDIM static? That is absolutely unnecessary. If a local variable is static it means informally that it will be the same variable next time you call the function with kept value. Since you technically can't call main a second sime this is useless. Even if you could do that it would still be useless.
you should wrire delete [] array[i]; and delete [] array; so the compiler knows that the int* and int** you're trying to delete actually point to an array, not just an int or int* respectively.
Let's say I want to add a 4th element to the first dimension, i.e. myArray[3]. Is this possible?
Yes, but it's a pain in the neck. It basically boils down to allocating a new array, just as your existing code does (hint: put it in the function and make the sizes arguments to that function) and copying compatible elements over.
Edit: One of the things that std::vector does for you is properly de-allocating you memory. In the code you have, failure to allocate one of the arrays along the 2nd dimension will result in a memory leak. A more robust solution would initialize pointers to 0 before performing any allocation. An exception block could then catch the exception and free whatever was partially allocated.
Because this code becomes complex quickly, people resort to allocating a single buffer and addressing using a stride or using a 1D array of 1D arrrays (i.e. std::vector of std::vectors).

Dynamic Arrays

I'm just starting to learn C++ so excuse me for this simple question. What I'm doing is reading in numbers from a file and then trying to add them to an array. My problem is how do you increase the size of the array? For example I thought might be able to just do:
#include <iostream>
using namespace std;
int main() {
double *x;
x = new double[1];
x[0]=5;
x = new double[1];
x[1]=6;
cout << x[0] << "," << x[1] << endl;
return 0;
}
But this obviously just overwrites the value, 5, that I initially set to x[0] and so outputs 0,6. How would I make it so that it would output 5,6?Please realize that for the example I've included I didn't want to clutter it up with the code reading from a file or code to get numbers from a user. In the actual application I won't know how big of an array I need at compile time so please don't tell me to just make an array with two elements and set them equal to 5 and 6 respectively.Thanks for your help.
You don't want to work with arrays directly. Consider using a vector, instead. Then, you can call the push_back function to add things to the end, and it will automatically resize the vector for you.
#include <iostream>
#include <vector>
int
main() {
double value;
std::vector<double> values;
// Read in values
while (std::cin >> value) {
values.push_back(value);
}
// Print them back out
for (std::size_t i(0), len(values.size()); i != len; ++i) {
std::cout << values[i];
}
}
You should use a collection class to do this for you rather than managing it yourself. Have a look at the "vector" class. It's essentially a dynamic array that resizes automatically as required.
In your situation you would use "vector" with the "double" type. You may also need to read up on templates in C++.
http://www.cplusplus.com/reference/stl/vector/
Or, if you don't want to use STL or another dynamic thing, you can just create the array with the correct size from the beginning: x = new double[2];
Of course the problem there is how big to make it. If you don't know, then you'll need to just create it "big enough" (like a hundred, or a thousand)... which, at some point, won't be big enough and it will fail in some random looking way. So then you'll need to resize it. And once you get to that point, you'll wish you'd used the STL from the start, like the other answers are telling you to do.
#include <iostream>
using namespace std;
int main() {
double *x = new double[2];
x[0]=5;
x[1]=6;
cout << x[0] << "," << x[1] << endl;
return 0;
}
Here's an example though for good measure, so you can see the pattern:
#include <iostream>
using namespace std;
int main() {
// Allocate some memory for a double array of size 1 and store
// an address to the beginning of the memory in mem_address.
double* mem_address = new double[1];
// Assign 5 to the first element in the array.
mem_address[0] = 5;
// Save the address of the memory mem_address is currently
// referencing.
double* saved_address = mem_address;
// Allocate some memory for a double array of size 2 and store
// an address to the beginning of the memory in mem_address.
mem_address = new double[2];
// Copy over the 1 element from the first memory block
// to the new one.
mem_address[0] = saved_address[0];
// Done with the old memory, so clean it up.
delete [] saved_address;
// Assign 6 to the second element in the new array.
mem_address[1] = 6;
// Print out the 2 elements in the new array.
cout << mem_address[0] << "\n";
cout << mem_address[1] << "\n";
// Done with the new array memory now, so clean it up.
delete [] mem_address;
}
If for some reason you don't have access to STL -- or want to learn how to do this yourself -- you could use an algorithm like this:
Allocate your array as some arbitrary size, and remember how many elements are in it and how big it is:
int *a = malloc(int * ARBITRARY_SIZE);
int size = 0;
int allocated = ARBITRARY_SIZE;
each time you add a new element, increase "size". If size equals ARBITRARY_SIZE, multiply 'allocated' by 2, and reallocate the array. Either way, assign the new value to a[size].
void addElement(int value) {
++size;
if (size == allocated) {
allocated *= 2;
a = realloc(sizeof(int) * allocated);
a = new_a;
}
a[size] = value;
}
Note that your code above has at least one bug -- you aren't allocating enough space for x[1] in either case.
Also obviously in real code you'd check that the return from malloc & realloc isn't null.
An array always needs a contiguous block of memory. In a situation where you might need to resize the array later on, reallocation is probably the only solution. This is what Moishe and Shadow2531 do above.
The problem with reallocation is that it can be a costly operation. So if you need adding 5 more elements to a 5000 element array, you might end up copying all the 5000 elements across memory.
Using a linked list instead can be considered for such a scenario.