Pointers and arrays - why is this working?
| radhack242 (4) | |||
| I'm studying on my own using a book. I'm working my way through this chapter on pointers and I wanted to try something (testing is what learning is all about right? ^_^). Specifically, I wanted to modify this prog using a pointer to allow the user to specify a number. After compiling the prog works just like I wanted it to, but how is that possible? Here's the code:
Thanks a bunch! Sorry if this is simple and I just missed it, I've read and reread this many times... | |||
| Faldrax (244) | |||
| You haven't said exactly what about the way it works you don't understand, but there are a few oddities in there that i can see which may be causing confusion. function define(int *p) You take a pointer to an integer as a parameter, but do not do anything with that pointer in the function. Instead you use the global varaible r directly. function sort(int n) You are calling this with a hard-coded 10 for n, but then overwrite that with the global variable r (line 61). arrays - general There is no 'bounds checking' on arrays in C++, so although you declare int a[10], the compiler will let you try and access a[37] if you wish. What is happenign 'behing the sceens' is that an array is actualy a constant pointer of the type the array is of (int in this case), and indexing is done by pointer arithmetic. So a is actulay an int pointer to a[0]. a[1] is a pointer to the address of a[0] + sizeof(int) bytes a[2] is a pointer to the address of a[0] + 2 x sizeof(int) bytes etc. There is a bit more on pointer and arrays on the site tutorial http://www.cplusplus.com/doc/tutorial/pointers.html | |||
| guestgulkan (150) | |||
| Luck - ahthough technically speaking it is flawed - you are overwriting space that does not belong to "array a" - and in this case you have got away with it. Here is a simple program to illustrate my point. I have two other arrays C and B - these are initialised to zero's. Variable r decides how many repateing values we write to array A.
This is the output of the program when I stay WITHIN the bounds of array A (all values correct) Array A 1 1 1 1 1 1 1 1 1 1 Array C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Array B 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Press any key to continue . . . **Now here is what happens when I write 50 numbers to array A (well beyond it's bounds)** Here are all the array(s) elements Array A 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Array C 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 Array B 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Press any key to continue . . . As you can see we have started to overwrite array C. If we continue even further past the bounds of array A we will completely overwrite C and possibly even B | |||
| radhack242 (4) | |||
| @ Faldrax - thanks for the reply. The code itself has comments next to what I didn't understand. I wanted to use a pointer on a global variable to change the value of the variable. I know this is unnecessary with this little prog but I just wanted to test it. It does make more sense now though; I didn't know/understand that array parameters aren't verified. Also I didn't realize until now that i wrote define(int *p) and not define(int *p3) - so obviously I need to spend some more time studying... @ guestgulkin - thanks also. Your explanation does clear it up even more. Thanks guys - I thought I was cleaver when it worked, but I knew something was wrong. I tried to think it through before I posted - I don't want to waste anyone's time. Thanks again! ^_^ | |||
Registered users can reply in this forum.
