-2

I've been really troubled by this problem. It may sound ridiculous, but this is exactly what happened in my terminal.

int* nums = new int[100];
cout << sizeof(nums);

but the block above outputs 8 instead of 100; can anyone please help?

enter image description here

Full program (readfile.cpp):

// imports

pair<int, int*> Readfile::readfile (string dirc) {
    string fn;
    if (dirc.compare("") == 0) {
        cout << ">>> Input file name: "; cin >> fn; cout << endl;
    }
    else
        fn = dirc;

    ifstream infile(fn);
    string line;
    vector<int> ints;

    while (getline(infile, line)) {
        istringstream iss(line);
        for (string k; iss >> k; )
            ints.push_back(stoi(k));
    }

    int* nums = new int[100];
    cout << sizeof(nums);
    copy(ints.begin(), ints.end(), nums);
    int n = sizeof(nums)/sizeof(nums[0]);
    
    pair<int, int*> pii(n, nums);
    return pii;
}

int main() {
    Readfile rdf;
    rdf.readfile("..\\insertion\\temp.txt"); // a text file consisting of 100 random integers separated by spaces
    return 0;
}

Any help is greatly appreciated.

crimsonpython24
  • 2,223
  • 2
  • 11
  • 27
  • 2
    Does this answer your question? [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) – 273K Mar 28 '21 at 03:27
  • I'd really like an answer except for "no," to be honest; I've done copying values from an int vector to an int array before but somehow this time it won't work – crimsonpython24 Mar 28 '21 at 03:31
  • 1
    @crimsonpython24 -- A pointer knows nothing except that it points to a single entity. It has no idea that what it points to has multiple items after it. The issue is that you were led to believe that there is some information the pointer knows about, other than that single element it points to. – PaulMcKenzie Mar 28 '21 at 05:12
  • duplicate: [How to find out what the size of dynamically allocated array is (using sizeof())?](https://stackoverflow.com/q/30367512/995714), [What should I do to get the size of a 'dynamic' array?](https://stackoverflow.com/q/25756090/995714) – phuclv Mar 28 '21 at 05:37

2 Answers2

2

You are confusing your self here!!! Size of pointer type int*, char* or double * is 8 bytes or 64 bits. That is what sizeof() is giving you.

If you want to increase size of place where memory is pointing to you have to reallocate memory. To do so you can make your own resize() function:

void resizeIntArray (int**, int, int);

int main (void) {
   int sizeOfA = 55;
   int* a = new int(sizeOfA); // 55 slots for integers have been allocated
   std::cout << sizeof(a); // gives you 8 because it not size of allocated memory
                           // rather it is size of type.
   int newSizeOfA = 100;
   resize(a, sizeOfA, newSizeOfA);

   std::cout << sizeof(a); // AGAIN!!!! gives you 8 because it not size of allocated memory
                           // rather it is size of type.

   return 0;
}

void resize (int **a, int oldSize, int newSize) {
   int* temp = new int[newSize];

   for (int i = 0; i < oldSize; ++i)
      temp[i] = a[i];

   delete[] a; // destroys old A
   a = temp;
}

So in my example, new space allocated is 100 instead of 55.

Think about pointers like address of your house. And if you have 1000 sq ft, to have more space you got to remove everything from your old house, and build a new house (lets say of 5000 sq ft).

1

The reason you are getting 8 is because that is the size of the pointer nums.

  • Can you please elaborate? How do I use the size of the array instead of size of the pointer? – crimsonpython24 Mar 28 '21 at 03:21
  • When you use `new` you were essentially saying "allocate space on the heap for 100 integers and give me the memory address for the first one." Since you're on a 64-bit machine, your memory addresses are 64 bits (i.e., 8 bytes) long. So when you do `sizeof(nums)` you're essentially asking for the machine to return the size of one of its memory addresses. The size of a pointer is a product of the operating system. – iandinwoodie Mar 28 '21 at 03:25
  • I found a forum that said to use something like `int n = sizeof(nums)/sizeof(nums[0]);` but it still won't give the correct output. The size of `ints` is 100, but `n` only yields 2. Unless something is wrong with the `copy` function, I don't know where the problem is – crimsonpython24 Mar 28 '21 at 03:29
  • 1
    The size of `nums` is 8. Not 100 (you're probably thinking 400 with how you want it to behave). That 'trick' only works if you are in the same scope of an actual array and not a pointer. You just have to have stored the size elsewhere for referencing. – sweenish Mar 28 '21 at 03:30
  • @crimsonpython24 if you're wondering "well then how do I know the size"? The answer is that you need to store that number you passed to `new` any manually track the number of integers you've allocated space for. One of the benefits of the containers provided by the STL (e.g., `std::vector`) is that they take care of managing that size for you. – iandinwoodie Mar 28 '21 at 03:32
  • @crimsonpython24 the line `int n = sizeof(nums)/sizeof(nums[0]);` will work if `nums` is simply an array of ints (e.g., `int nums = int[100];`). However, in your code you have a pointer to a block of memory allocated for 100 ints (e.g., `int* nums = new int[100];`). – iandinwoodie Mar 28 '21 at 03:35