Thread: [Dev-C++] =?utf-8?q?Pointer_location?=
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: <ba...@o2...> - 2007-08-24 11:05:37
|
Hi. Could=20you=20tell=20me,=20why=20pointer=20'pa'=20shows=206=20element=20o= f=20the=20table=20(x[6]).=20Why=20it=20does=20not=20show=20second=20eleme= nt?=20Here=20is=20the=20code: #include=20<cstdlib> #include=20<iostream> #include=20<stdio.h> #include=20<conio.h> using=20namespace=20std; struct=20bartek=20{ =20=20=20=20=20=20=20=20=20int=20a; =20=20=20=20=20=20=20=20=20char=20c[4]; =20=20=20=20=20=20=20=20=20int=20b; }=20*wsk1=5Fbartek; int=20x[1000]; int=20main(int=20argc,=20char=20*argv[]) { =20=20=20=20system("PAUSE"); =20=20 =20=20=20=20bartek=20*pa; =20=20=20=20 =20=20=20=20pa=20=3D=20(bartek*)=20x; =20=20=20=20 =20=20=20=20for(int=20i=20=3D=200=20;=20i=20<=20100;=20++i)=20=20=20{ =20=20=20=20=20=20=20=20=20=20=20=20x[i]=20=3D=20i; =20=20=20=20} =20=20=20=20 cout=20<<"=20After=20shift=20+2:=20"=20<<=20(pa+2)->a=20<<=20endl; =20=20=20=20 cout=20<<"=20Global=20object=20read=20on=206=20position=20x[6]:=20"=20<<=20= x[6] <<=20endl; =20 =20=20=20=20system("PAUSE"); =20=20=20=20return=20EXIT=5FSUCCESS; } ...and=20here=20is=20the=20output: Press=20any=20key=20to=20continue... After=20shift=20+2:=206 Global=20object=20on=206=20position=20x[6]:=206 Press=20any=20key=20to=20continue... |
From: Siva C. <siv...@ya...> - 2007-08-24 11:54:56
|
Hi, First let me make a few notes which will help understand your code better. I am assuming that you are working on a 32 bit system. 1. pa is a pointer of type bartek. 2. The size of the bartek datatype is 12 bytes. It is not 9 bytes since the size in bytes should be a multiple of 4 in a 32 bit system. 3. Since size of bartek is 12 bytes, pa+2 will point to a location which is 24 bytes ahead of the location pointed by pa. 4. The size of int is 4 bytes. With the above points in mind, pa+2 points to a location which is 24/4=6 int locations away from pa. Which is to say that pa+2 points to the 7th integer in the array x. The 7th integer in the array is x[6]. Hope I could clear your confusion. /Siva Chandra --- Bartosz Åliwa <ba...@o2...> wrote: > Hi. > > Could you tell me, why pointer 'pa' shows 6 element > of the table (x[6]). Why it does not show second > element? Here is the code: > > #include <cstdlib> > #include <iostream> > #include <stdio.h> > #include <conio.h> > > > > using namespace std; > > struct bartek { > int a; > char c[4]; > int b; > } *wsk1_bartek; > > int x[1000]; > > int main(int argc, char *argv[]) > { > system("PAUSE"); > > bartek *pa; > > pa = (bartek*) x; > > for(int i = 0 ; i < 100; ++i) { > x[i] = i; > } > > cout <<" After shift +2: " << (pa+2)->a << endl; > > cout <<" Global object read on 6 position x[6]: " << > x[6] > << endl; > > system("PAUSE"); > > return EXIT_SUCCESS; > > } > > > ...and here is the output: > > Press any key to continue... > After shift +2: 6 > Global object on 6 position x[6]: 6 > Press any key to continue... > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? > Stop. > Now Search log events and configuration files using > AJAX and a browser. > Download your FREE copy of Splunk now >> > http://get.splunk.com/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: > http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > ____________________________________________________________________________________ Sick sense of humor? Visit Yahoo! TV's Comedy with an Edge to see what's on, when. http://tv.yahoo.com/collections/222 |
From: Smitha V. B. <smi...@so...> - 2007-09-04 13:09:25
|
=20 =20 Hi ... Does any one knows abt anything abt MC68k emulator. Is there any free software is available using MC68k emulator.. Regds smitha |
From: Per W. <pw...@ia...> - 2007-08-24 12:03:38
|
6c6c6cMany errors. The 6th element of an array is not array[6] but array[5]. The first element is array[0], not array[1]. In this case, your pointer usage is wrong. An int pointer may be used with the address of an integer. x is an array of integers, and the language does not promise any guaranteed behaviour if you use a type cast to assign a pointer of a different type with the array, and then try to use this pointer to try to reach around. If we ASSUME that the struct is laid out as three integers, where the middle integer is 4 charactesr, and the integers are 4 bytes large and that the struct totals 12 bytes etc, etc then you would get: (pa+2) would be the same as pa[2] and be 2*12 =3D 24 bytes forward in the int array. 24 bytes - given 4-byte integers - would mean that the pointer jumps over 6 integers. So, it will access the 7th integer (x[6]) which has the value 6. Just as expected if all assumptions was correct. But the code would still be wrong. A change of the memory layout of the struct - which the compiler is allowed to do - or a change of the integer size - depending on architecture - would make your code to either crash or to emit completely different values. Never play with pointers. Only convert between compatible pointers. an int pointer may be set to point at the a member of a bartek struct. But may not be used to access anything but that specific a element. A bartek pointer may never be used to point to an integer. A type cast will make the compiler happy, but your code would break the assumptions allowed by the language standard. /pwm On Fri, 24 Aug 2007, [UTF-8] Bartosz =C5=9Aliwa wrote: > Hi. > > Could you tell me, why pointer 'pa' shows 6 element of the table (x[6]). = Why it does not show second element? Here is the code: > > #include <cstdlib> > #include <iostream> > #include <stdio.h> > #include <conio.h> > > > > using namespace std; > > struct bartek { > int a; > char c[4]; > int b; > } *wsk1_bartek; > > int x[1000]; > > int main(int argc, char *argv[]) > { > system("PAUSE"); > > bartek *pa; > > pa =3D (bartek*) x; > > for(int i =3D 0 ; i < 100; ++i) { > x[i] =3D i; > } > > cout <<" After shift +2: " << (pa+2)->a << endl; > > cout <<" Global object read on 6 position x[6]: " << x[6] > << endl; > > system("PAUSE"); > > return EXIT_SUCCESS; > > } > > > ...and here is the output: > > Press any key to continue... > After shift +2: 6 > Global object on 6 position x[6]: 6 > Press any key to continue... > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
From: Adam J. <aj...@i-...> - 2007-08-24 14:07:47
|
There are some other items that I would be concerned with in your code. Maybe because this is a test is why you did what you did. Here are some comments about what you are trying to do. I believe the explanation you got from the others explain what is happening. It also appears that You are trying to mix two styles (C style programming with C++ Style programming). My advice is to pick one. You mix C headers with C++ header files, some of which overlap functionality. > #include <cstdlib> <== This is a C++ implementation of the C stdlib > #include <iostream> <== This is a C++ io implementation header > #include <stdio.h> <== This is a C io implementation header > #include <conio.h> <== This is a C Style console io implementation file that is not support by all compilers on different OSes As you can see you have three different io implementations, and what makes it worse is that you only use the functionality in iostream. The other io headers aren't used in your code. My suggestion is only include the header files you need. > pa = (bartek*) x; The cast listed above is a C Style cast not a C++ cast (Mixing Styles...). C++ uses static_cast, dynamic_cast, etc. Be careful when you do cast. You can get yourself in a lot of trouble. Just because the compiler doesn't complain, doesn't mean your program will run correctly. You are mixing two types that don't really go together nicely. Your user defined abstract data type (bartek) and the native int type. > Per Wrote: > Never play with pointers. Only convert between compatible pointers. He is exactly right. I would expand on this by saying types instead of pointers. But the idea is the same. As you found out this utimately was part of your problem. The assumetions you made about how the casting would be implemented. One final point, if you are going to ceate your own types and manupilate them, don't try and reinvent the wheel. C++ has the STL, part of the STL is containers and iterators. If you create your abstract types to conform to the STL, it will make your life a lot easier. You should use the STL containers, iterators, and algorithms as much as you can. They are time tested and very efficient at what they do. -Adam Jones -i-softwareproducts.com |