+ 14

In depth explanation?

So someone else made this to help me solve another question of mine. But I have 2 questions: 1. The grid points seem to be off by 1. Why? And how can I fix this? 2. Can someone explain this code to me in depth? Like, every detail and what "data" is? What is delete[]? so many questions. Here's he code: https://code.sololearn.com/cSUHaSc0FC4Q/?ref=app

11th Jan 2019, 3:58 AM
Daniel Cooper
Daniel Cooper - avatar
9 Antworten
+ 15
It doesn't really seem to be off, just, starts counting from 0. A simple "fix" would be to edit putPoint() and deduct 1 from the input value (and of course, tweak the constraints). "data", as you can see, is declared as char** data, which is essentially, a pointer to a pointer of type char. Since we can use regular char* pointers to point to the first element of a dynamically allocated array, a char** pointer type can be used to point to the first element of the first dynamically allocated array. If this doesn't make sense to you, just treat it as a 2D array (which is how it is used in the code above). Accessing data[1][2], for example, will access element at row 1, column 2. The delete keyword is used to free dynamically allocated memory (memory allocated using the 'new' keyword). If you allocated only one memory block, e.g. int* ptr = new int, then delete ptr would suffice. If you allocated more than that (i.e. an array), then use delete[]. E.g. int* ptr = new int[5]; delete[] ptr;
11th Jan 2019, 4:20 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
@Daniel Cooper Like said previously in another thread, i keep the starting-to-0 index access because is consistent with language one but its really easy "adjust" that like @Hatsy Rei said
11th Jan 2019, 12:01 PM
KrOW
KrOW - avatar
+ 3
The delete [] data [x]; It seems to be part of a destructor that deletes data and frees up storage. I remember like this, but not necessarily accurate, because my memory is not very good
11th Jan 2019, 7:37 AM
sweetswing2460
+ 2
check the first of course you can do it was the same thing
11th Jan 2019, 5:30 AM
Eldar Inomov
+ 2
nice!
11th Jan 2019, 5:27 PM
Mohammad Elahi
Mohammad Elahi - avatar
+ 2
I will agree with KrOW and Hatsy Rei , the indices will be off by one, but from what frame of reference? To a coder, they might suspect that the indices start at 0. Personally, if I were thinking in "grid mode", I would think that the X's were not on the axes (x=0, y=0), so I would (again, personally) make the function translate the grid coordinates to array coordinates. Just a thought I just had...happy coding!
12th Jan 2019, 4:43 AM
Zeke Williams
Zeke Williams - avatar