0

Here is a code 1

    int j[2];

    int *ptr = j;

    for(int i=0;i<__INT_MAX__;++i){
        cout << i << endl;
        j[i]=0;
    }

In this code, it prints 1 2 3 4 5 6 7 and again 1 2 3 4 5 6 7. It doesn't give segmentation fault. But when I assign array's adress to pointer and write ptr[i] instead of j[i], it prints 1 2 3 4 5 6 and it gives segmentation fault. That code:

        int j[2];

        int *ptr = j;

        for(int i=0;i<__INT_MAX__;++i){
            cout << i << endl;
            ptr[i]=0;
        }

So I think Netbeans handle the situation, but this is harmful for programmer. How can I disable it? Are there other "protections" like this?

Summary edit: It must give me segmentation fault but it is not. I want to get fault, so I'll be able to fix my code. Netbeans prevent that error.

Edit 2: This topic isn't duplicated. In that topic, he try just one address up. In my code, there is infinite loop and it must give seg fault

Addition 3: When I allocate from heap, give pointer that adress and put in to loop, i value is approaching 30.000's. But in first code, it increases until 6-7, again starts from 0. Because of this, I think my IDE prevent seg fault.

  • 1
    Regarding edit 2: Did you read the answer in the duplicate? He answers your question exactly. – R_Kapp Nov 12 '15 at 18:33

1 Answers1

1

Accessing memory that you do not own (which you do in both of your code examples) is undefined behavior. This means that the code can do anything it wants to; including, as it does for your Netbeans code, appear to work properly. There is no way to "disable" this, as it were, short of changing programming languages.

R_Kapp
  • 2,818
  • 1
  • 18
  • 32
  • I didn't mean that. It must give me segmentation fault but it is not. I want to get fault, so I'll be able to fix my code. Netbeans prevent that error. –  Nov 12 '15 at 18:26
  • 1
    @thoron: There is nothing in the standard that says you *must* get a segmentation fault. It is undefined behavior. You may get one, you may get code that appears to work, you may get demons to fly out of your nose; such is the nature of undefined behavior. – R_Kapp Nov 12 '15 at 18:27
  • If I tried that with pointer 5 times and I get seg fault each time and I tried 10 time with array and I'm not getting fault, It means program is handle that. It might be undefined behavior, but stack isn't infinite, when I go upper adresses always, I pass stack and reach read-only segment like data. –  Nov 12 '15 at 18:32
  • 1
    @thoron: "It means program is handle that". No, it doesn't. It means you have undefined behavior. Read the link I provided you, and the accepted answer in the duplicate. – R_Kapp Nov 12 '15 at 18:34
  • Okay, what about i never increase after 7 or 8? when I allocate from heap and put in that loop, i see i=30.000. I insist program is handle that. It doesn't go adress which cause seg fault. –  Nov 12 '15 at 18:37
  • 2
    @thoron: You can insist all you like; doesn't change the fact that you're wrong. I'm done responding here. – R_Kapp Nov 12 '15 at 18:38
  • Thank you for your responses. –  Nov 12 '15 at 18:40