-1

While learning the dynamic object creation in C++ i have encountered a doubt . Here is my code.
And my question is , when the limiting condition in the loop is same as that of the no of objects created it works fine. But what happens when the loop works for more than the size given , it seems printing the values entered , but we have created only 4 objects and changed the condition of loop to more than 4

#include <iostream>
using namespace std;

class item{
    int number;
public:
    item(){
        cout<<"Constructor"<<endl;
    }
    ~item(){
        cout<<"Destructor"<<endl;
    }
    void get_num(int num){
        number = num
    };
    void show_num(){
        cout<<"Number is "<<number<<endl;
    }
};

const int size=4;
int main() {
    item *itemObj = new item[size];
    item *d = itemObj; //copy the address of itemObj inorder to access its member functions later
    int tempNum; 

    for (int i = 0; i < size; ++i) { 
        cout<<"Enter the Number"<<endl;
        cin>>tempNum;
        itemObj->get_num(tempNum);
        itemObj++;
    }
    //to print the numbers entered
    for (int i = 0; i < size; ++i) {
        d->show_data();
        d++;
        cout<<d<<endl;
    }
    delete itemObj;
    return 0;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • I don't really follow your question because the code you've shown doesn't appear to demonstrate the situation you're describing, but it sounds like you're encountering [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) by performing invalid array accesses. Doing so can have any output whatsoever; no requirements are imposed by the standard, and in practice the observed behavior can vary from machine to machine, compiler to compiler, or even run to run of the same binary. – Nathan Pierson Mar 17 '22 at 07:07
  • 1
    Please post a *complete* (with all the #include directives) program that *does not work as expected*. Do not post a good program and then say "but if I change it then there is a problem". – n. m. could be an AI Mar 17 '22 at 07:08
  • @n.1.8e9-where's-my-sharem. i have tried adding the #include but unfortunately it was not appearing – hakkeempa Mar 17 '22 at 07:13

1 Answers1

0

Your code isn't working fine at all. Because you change the value of the pointer that you requested from the new operator. When you call the delete for the itemObj at the last line, it doesn't have its original value. So, instead of modifying the itemObj, you should modify the copy of it which is the pointer d here. Therefore, the problem isn't about the iteration amount of the loop. It's actually the violation on the heap memory.

Also, if you're creating a dynamic array, you should call delete [] instead of delete.

Caglayan DOKME
  • 948
  • 8
  • 21
  • I dont know what "problem" the first paragraph is talking about when the last sentence is refering to the wrong `delete`. I mean, yes its a memory leak, but I am not sure if thats what you mean – 463035818_is_not_an_ai Mar 17 '22 at 07:52
  • @463035818_is_not_a_number Oh, my fault. It's a typo. Now, I've changed it to "violation on the heap memory". And yes, I meant memory leak. – Caglayan DOKME Mar 17 '22 at 08:00