Himmayn
Himmayn

Reputation: 27

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;
}

Upvotes: 0

Views: 115

Answers (4)

AnT stands with Russia
AnT stands with Russia

Reputation: 320787

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

Upvotes: 0

Alex
Alex

Reputation: 3181

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];
  1. 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.

  2. 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);

Upvotes: 0

JuanCrg90
JuanCrg90

Reputation: 1016

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;
}

Upvotes: 2

Ed Heal
Ed Heal

Reputation: 60037

You are just taking the size of the pointer.

But just use std::vector

Upvotes: 0

Related Questions