Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

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

0 votes
asked Jun 13, 2020 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, 2020 by gameforcer (2,990 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, 2020 by LiOS (6,420 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.
...