Why doesn’t the program work with new / delete?

0 votes
asked Jun 13 by Q Nor (120 points)
After starting, "10" should be displayed, this does not happen. What is the problem?

#include <iostream>
using namespace std;

int* createPtr(int value)

{

    int *ptr = new int(value);

    return ptr;

}

void usePtr()

{

    int *p1 = createPtr(10);

    cout << *p1 << endl;

    delete p1;

}

int main()

{

    usePtr();

    return 0;

}

2 Answers

0 votes
answered Jun 14 by gameforcer (2,950 points)
I don't see a problem while compiling with C++ compilator here on onlinegdb.

Unless (just as you tagged it) you want it to run in C then of course it wouldn't work since it's clearly a C++ program.
+1 vote
answered Jun 14 by LiOS (3,880 points)
It's a C++ program, not a C program. When running the code using C++ compiler it works and prints 10.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...