[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  Pointer explination for a newbie please?

Pointer explination for a newbie please?

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Dralnu   USA  (277 posts)  [Biography] bio
Date Sun 28 Aug 2005 01:36 AM (UTC)
Message
I think I understand part of pointers, but it still kind of evades me. I was looking over a tutorial on them, which didn't help much, and the programers refrence I have is a bit complex, so if someone would maybe break it down into lay-mans terms that someone who hasn't done alot of programming and never really dealt with pointers might oculd understand it?
[Go to top] top

Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Reply #1 on Sun 28 Aug 2005 04:32 AM (UTC)
Message
Pointers... If I get something in here wrong, please someone correct me.

Memory that is used in a program is stored in blocks. Each block is 8 bits, or 1 byte. Each of this blocks has a unique identification known as a "memory adress". If all the blocks were in a row(side by side) the first would be 0, the second would be 1, etc. A pointer is a group of blocks(typicly 4, I think) that contain the memory adress of another block. So blocks 0-3 could contain the adress of block 4.

So a pointer really tells the system something like, "I don't have the information you need, but if you look at (adress) you should find what you need". A big problem with how pointers are misproperly used is that you can, for exampe, may a pointer point to somewhere where no data has been set, so when the system tries to look at the adress, there is nothing there and depending on the use, may crash.

Take PCDATA for example.
PCDATA *pcd;
pcd = NULL;
pcd->age
This would cause a crash. The system already knows what a structure of type PCDATA looks like, and it knows that the age section may be 100 bytes into the allocated space. So, when 0x0(NULL) is passed as the value of pcd, and the system tries to access 100 bytes after that for age, it may find nothing at all, it may find something that makes no sense(a string, for example), or it may find something that will work but is wrong(-500).

Someone please correct what I have wrong there, but that should be the jist of it.

P.S. I know that it is not strictly true, but it should get the idea across to someone that does not know the inner workings of their compiler.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #2 on Sun 28 Aug 2005 04:57 AM (UTC)
Message
Pointers are indeed 4 bytes, on 32-bit machines. The rules change slightly on 64-bit machines, but not a whole lot, for most purposes at least.

The problem with dereferencing a null pointer is that you end up in protected memory space, and the OS or C runtime library (usually the OS, unless you have a special CRT library) will kick you out with a segmentation fault. A segmentation fault is not a random name; segments are the chunks (e.g. pages) of memory that comprise your program (code + data + heap + stack) and a segmentation fault is when you try to do something that violates the table rules.

I believe that it would be possible to do something like this:
PC_DATA * pc = (valid_pc_data);
pc = (PC_DATA*) ( ((char*)pc) +1);
printf("%d", pc->age);
In this case, we take the address of a valid pc data structure, and we move it forward one byte. So, if age is an integer, we'll get the first three bytes of it, plus the last byte of whatever comes afterwards...


Aaaaanyways, yes, a pointer is basically a way of referring to a block of memory. It's kind of like a mailing address... your variable, address, "points to" where you live. However you can change where it points, but that doesn't affect the old address nor does it affect the new mailing address - all it does is change which house 'address' refers to.

As a nitpicky detail, generally program data is stored in 4-byte chunks, not 1-byte chunks. That is why structure padding is necessary; e.g. if you have a struct like this:
struct foo { char bar; };
, it will take up four bytes of memory even though it only uses one byte. This is because of the 32-bit nature of the machines.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Dralnu   USA  (277 posts)  [Biography] bio
Date Reply #3 on Sun 28 Aug 2005 07:10 PM (UTC)
Message
Ok, I think I undertsnad that, but what about how to tell it WHERE to point? This may have been explained earlier, but I didn't see it, and if I did, didn't understand it. I think I kind of understand the:

PCDATA *pc=

but that is kind of it. Is the directory listed as the =, or is it just the name of the line and the directory is listed elswhere in the code?
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #4 on Sun 28 Aug 2005 08:26 PM (UTC)
Message
I'm not sure what you mean by the 'directory'. If you mean the block of memory to which the pointer points, that is typically allocated using malloc (or 'new' in C++). Once you have this address, you can then share it. So...
CHAR_DATA * ch = (CHAR_DATA*) malloc(sizeof(CHAR_DATA));
ch->age = 123;
CHAR_DATA * anotherCh = ch;
anotherCh->age= 456;
Here, we've created one character data, changed its age to 123, but then we created another pointer to the same block of data, and changed its age to 456. Both pointers, 'ch' and 'anotherCh', point to the exact same block of memory, so the end result is that both 'ch' and 'anotherCh' have age 456.

Does this clear things up or did I misunderstand your question?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Dralnu   USA  (277 posts)  [Biography] bio
Date Reply #5 on Mon 05 Sep 2005 06:27 AM (UTC)
Message
If you could give me a refrence in some code I may have (smaugfuss1.6), I think I might could figure it out. I think I understand it better now, just want to see it in some code myself.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #6 on Mon 05 Sep 2005 07:35 AM (UTC)
Message
Uh... well, shared pointers are all over the place. For starters, char_data points to other char_data for things like a group leader. If multiple people are following the same person, they all will point to the same block of memory in their char_data group leader entry. But all these pointers are to the same "physical" block of memory. It's kind of like lots of arrows all pointing (hence the term 'pointer') at the same thing.

Is that the kind of example you were looking for?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Dralnu   USA  (277 posts)  [Biography] bio
Date Reply #7 on Mon 05 Sep 2005 06:12 PM (UTC)
Message
Not exactly. I was thinking more along the lines of something like comm.c: 4312 or something (just a random number I put up)
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #8 on Mon 05 Sep 2005 08:38 PM (UTC)
Message
Well, shared pointers typically don't occur in a few lines of code because it's not generally not useful to share many pointers to the same memory if you already have a pointer there.

That being said many of the string processing functions might have pointers to the same string, with one pointing to the beginning and one "walking" the string. I don't have the code handy to find a reference right now but if you search for "p++" or something like that you mind find it. Or just go look at string processing functions like one_argument, maybe display_prompt, etc.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


21,406 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]