new and delete

This works when I compile with g++ with -std=c++11.

1
2
3
4
5
6
7
int main()
{
    int *pointer = new int;
    delete pointer;

    return 0;
}

But, when I compile with -std=c++14 it does not.

Compile error:
/data/user/0/com.n0n3m4.droidc/files/gcc/tmpdir/ccVlJIIr.o: In function `main':
temp.c:(.text.startup+0x18): undefined reference to `operator delete(void*, unsigned int)'
collect2: error: ld returned 1 exit status

It does work when I do this, though.

1
2
3
4
5
6
7
int main()
{
    int *pointer = new int;
    delete[] pointer;

    return 0;
}

Why?
Last edited on
on cpp.sh it works on both versions...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// operator delete[] example
#include <iostream>     // std::cout

struct MyClass {
  MyClass() {std::cout <<"MyClass constructed\n";}
  ~MyClass() {std::cout <<"MyClass destroyed\n";}
};

int main () {
  MyClass * pt;

  pt = new MyClass[3];
  delete[] pt;

  return 0;
}
c++ 11 and c++ 14
MyClass constructed
MyClass constructed
MyClass constructed
MyClass destroyed
MyClass destroyed
MyClass destroyed


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// operator delete[] example
#include <iostream>     // std::cout

struct MyClass {
  MyClass() {std::cout <<"MyClass constructed\n";}
  ~MyClass() {std::cout <<"MyClass destroyed\n";}
};

int main () {
  MyClass * pt;

  pt = new MyClass[3];
  delete pt;

  return 0;
}
c++11 and c+14
MyClass constructed
MyClass constructed
MyClass constructed
MyClass destroyed



http://www.cplusplus.com/reference/new/operator%20delete[]/

what compiler are you using? isn't cpp.sh a good compiler?
I'm using gnu gcc compiler.
Last edited on
Your snippets compile fine for me using either switch using g++ 6.1.0.

Last edited on
They both compile fine when you're using -std=c++14? Hmmm, not for me.
Last edited on
How exactly are you compiling the program?

Can you post the build log when you compile the program?

Also your file extension indicates a C file not a C++ file try renaming your file to temp.cpp.
Last edited on
Topic archived. No new replies allowed.