a strange pointer error

Hi,

Would you please have a look at this simple code (its name is test1)? I don't know why my VS 2015 compiler gives an error :(

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
	int* p = new int(4);
	*(p + 0) = 2;
	*(p + 1) = 4;
	*(p + 2) = 6;
	*(p + 3) = 8;
        
	return 0;
}


The error: test1.exe has triggered a breakpoint.

I have installed my VS 2015 on Windows 7
Last edited on
Interesting it run's on my VS2013 CE.

However the problem is that you access memory that doesn't belong to you.
int* p = new int(4);
You just created a pointer to an int with the value 4. I guess you wanted to create an array int* p = new int[4];
Yes, you are right. Thank you very much. :)

I only have forgotten the correct form. I don't know why the programming statements are that volatile!

PS: Something more interesting, cpp.sh runs it successfully!
Last edited on
cpp.sh runs it successfully!


This code has no successful behaviour, because there is no correct behaviour.

You're into behaviour explicitly labelled as "undefined"; the compiler writer can do whatever they like.
Topic archived. No new replies allowed.