1

This is a small snippet of code I was writing to test my knowledge of pointer :

int *grades;
*(grades+0) = 100;
*(grades+1) = 87;
*(grades+2) = 99;
*(grades+3) = 92;

cout<<*(grades+0)<<endl;
cout<<*(grades+1)<<endl;
cout<<*(grades+2)<<endl;
cout<<*(grades+3)<<endl;

I know the flaw in assigning pointers like this. However the above snippet of code correctly printed out the value of each of the grades. The issue is if I use a for loop to automate the display of the content of the pointers.

int *grades;
*(grades+0) = 100;
*(grades+1) = 87;
*(grades+2) = 99;
*(grades+3) = 92;

for(int i=0;i<4;++i)cout<<*(grades+i)<<endl;

Why does addition of a for loop causing segmentation fault. I know the reason of the segmentation fault, but isn't both snippets of code basically the same? I was using https://www.onlinegdb.com/ for writing the code.

7
  • 4
    Your code is just a pointer, you need to assign it some valid address. Right now it is pointing to some garbage value Commented Jul 17, 2018 at 8:32
  • You have a pointer, but where does it point? Uninitialized local variables (even pointers) will have an indeterminate (and seemingly random) value. Using them in any way without initialization (except to initialize them of course) leads to undefined behavior. Ib you have UB (Undefined Behavior) then any discussion about behavior is moot. Commented Jul 17, 2018 at 8:33
  • 1
    On another note, for any pointer or array p and index i, the expression p[i] is exactly equal to *(p + i). I suggest you always use the former syntax (p[i]) because it's generally easier to read and understand. And also less to write. Commented Jul 17, 2018 at 8:34
  • "Using them in any way is UB" actually is not true - you could take the address of and pass it in form of pointer to pointer into a function (which then could assign something to)... Any usage that dereferences the uninitialised pointer is UB. Commented Jul 17, 2018 at 8:51
  • I am well aware of the fact that my pointer isn't pointing to any valid address. I am just testing the behavior of the compiler. Commented Jul 17, 2018 at 9:02

3 Answers 3

1

Both scenarios (with or without the for loop) suffer from the same undefined behavior, and the nature of UB is that anything can happen. That is why your two snippets that are "basically the same", differ in behavior.

Sign up to request clarification or add additional context in comments.

Comments

1

You code exhibits undefined behavior. grades points to invalid memory as it's uninitialized. Attempting to read or write through the pointer results in UB. It might look like it works but that's just one possible side effect of UB.

If you need a dynamic array use std::vector<int>, otherwise stick to std::array<int, 5>.

To make your current snippet work:

std::array<int, 4> a;
int* grades = a.data();
*(grades+0) = 100;
*(grades+1) = 87;
*(grades+2) = 99;
*(grades+3) = 92;

1 Comment

I'd add a warning for vectors: "std::vector<int> v(4); int* grades = v.data();: This is fine as long as v does not increase. If it does [...]" -> potential reallocation -> pointer dangling -> UB
1

Your dynamic array is unitialized. So now it is pointed to a random address. Try to do this:

int* grades = new int[4];
// do things
delete [] grades;

So, OS will assign some space for your 4 integers and not a random place where maybe there's another important value in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.