Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Pointers and Arrays | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
paraplayer

Snifit
Level: 22

Posts: 114/280
EXP: 57271
For next: 1079

Since: 06-06-05

Since last post: 44 days
Last activity: 44 days
Posted on 06-20-05 03:22 AM Link | Quote
Well i got a book on C and i've started learning the basics.

but this one chapter has completely stumped me.

Advanced pointer use! *Gaspeth!*

At this point i can't even figure out the source codes they give me to work with!

Most of the chaper works with multi dimensional arrays and how pointers work good with them.

Can someone help me out here? (I know i'm not being to specific but arrays have always been my weekpoint and pointers are just plain scary)
Ramsus

Octoballoon
Level: 19

Posts: 110/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-20-05 03:51 AM Link | Quote
Pointers are tricky at first, but they're the essential building block on which all of C is built. You can't do anything interesting in C without them.

I found the comp.lang.c FAQ to be really helpful when I was starting out. You can find it here. Read through a lot of the questions from Pointers to Pointers and Arrays, even if they don't seem relevant to what you're learning now. Learning assembly language helps too.

EDIT: Something I found on pointers. This should explain everything about pointers better than anything in my post, so go straight over there and read through it!

EDIT: Check this out too.

Anyway, I can't really explain pointers in a single post, but I can give you some advice and a few little bits of code to ponder over.

First of all, stop thinking of arrays in code as arrays. An array is really a block of memory, but what you use to access arrays are either (int *p; ) pointers or constants to the address of the array (int p[5]; ).

Each block of memory has its own address, but with pointers we don't have to worry about this. In fact, the type of the pointer determines how the memory is accessed. So a char *p will access memory 1-byte at a time, while an short int *p will access memory 2 bytes at a time.

To the computer, all memory is the same. However, the type of the pointer tells the computer how you want it to treat that memory when it accesses it.

Suppose we have a few pointers:

int *p;
char *pb;

p = malloc (sizeof(int) * 5); /* Gives us a block of memory the size of 5 int's */
pb = (char *)p; /* Copies the address in pointer p to pointer pb, typecasting it to a different type of pointer */
*p = 1; /* Copies 1 to the first 'int' size block pointed to by p */
p[0] = 1; /* Same as above */
*(p+1) = 2; /* Copies 2 to the second int size block pointed to by p */
p[1] = 2; /* Same as above */
*pb = 'c'; /* Copies the character 'c' to the first char size block pointed to by pb (and also p) */
pb[0] = 'c'; /* Same as above */
pb[1] = 'd'; /* Copies the character 'd' to the second char size block pointed to by pb */
printf("%d and %d with %c and %c\n", p[0], p[1], pb[0], pb[1]) /* But now p[0] is messed up, because its first byte of is now the value of 'c' instead of part of an integer, and its second byte is now 'd', but p[1] is fine, because two chars can fit into one int */

free(p); /* Return our malloc'd memory to the OS */


I might go into multidimensional arrays/pointers to pointers in another post, if you're still having troubles with them.

EDIT: I might as well explain what the deal is with the memory too, but this is just a quick post (as opposed to the seventh or eighth revision of a well-thought out chapter from a decent book or website).

There's three types of memory. There's global/static memory, stack memory, and heap memory.

Global/static memory is used by global and static variables. They create .data sections in the compiled program that is set aside for those variables. When the program is loaded by the OS, its memory layout (the code and data sections) are just mapped into memory. That's how space gets set aside for global and static variables, and why they're always available in the program.


The Stack is just a place to keep local variables. It's just a linear block of memory that your program lets all of its functions share. When a function is called, it can use the stack to store variables, because your program gives it a pointer to the top of the stack. Your compiler handles this pointer for you, but because there's just one pointer for the entire stack, when a function returns, it has to move the pointer back to where it was before. That's because when a function calls another function, it has to put all of its local variables on the stack so the new function can use the CPU. When the new function finishes, it has to restore the stack pointer to what it was before, so the original function knows where to find its local variables again.

That's why local variables "disappear" when a function returns.

The called function has no idea where the variables of the function below it are on the stack, so it can't access them. Instead, the calling function leaves copies of its local variables' values in CPU registers for the new function to use. However, since pointers are variables and the stack is a type of memory, a function can leave the address of one of its variables for another function to use. So by leaving a copy of the address to where its variable is on the stack for the function its calling to use, the function being called can use it as a pointer and change the value of the variable on the stack, regardless of how deep it might be.

The real use of pointers is for heap memory though. Heap memory is what most of the memory your OS keeps free is called. To access it, you have to ask the OS to give you some with malloc. Since it's not on the stack, it won't disappear when the function returns. However, this also means you have to tell the OS when you're done with it by free'ing it.

What's so great about this? Well, unlike stack or global memory (where the compiler determines what size everything is during compilation, fixing it into your program's code) we can ask for memory of any size at any time the program is running, so we can write code that accommodates variable amounts of data.

We need something to hold the address to the memory so we can access it, but also so we don't lose track of it and end up not free'ing it (causing a memory leak), and that's all a pointer does.

When declared in functions, pointers are just variables on the stack. They contain an address to blocks of memory, and when we dereference them (either *p or array notation with p[n] ) , we can set the value at those memory addresses. Since pointers on the stack disappear, we have to remember to either copy the addresses they hold before the function ends, or free them.




(edited by Ramsus on 06-19-05 07:10 PM)
(edited by Ramsus on 06-19-05 07:11 PM)
(edited by Ramsus on 06-19-05 07:15 PM)
(edited by Ramsus on 06-19-05 07:29 PM)
(edited by Ramsus on 06-19-05 07:39 PM)
(edited by Ramsus on 06-19-05 07:42 PM)
(edited by Ramsus on 06-19-05 07:45 PM)
(edited by Ramsus on 06-19-05 07:47 PM)
(edited by Ramsus on 06-19-05 08:32 PM)
(edited by Ramsus on 06-19-05 08:45 PM)
(edited by Ramsus on 06-19-05 08:52 PM)
(edited by Ramsus on 06-19-05 09:21 PM)
(edited by Ramsus on 06-19-05 09:21 PM)
paraplayer

Snifit
Level: 22

Posts: 115/280
EXP: 57271
For next: 1079

Since: 06-06-05

Since last post: 44 days
Last activity: 44 days
Posted on 06-21-05 02:48 AM Link | Quote
Thanks for your help!

i'm starting to understand them now.
Ramsus

Octoballoon
Level: 19

Posts: 117/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-21-05 03:17 AM Link | Quote
No problem. Let me know if you want to learn Lisp as well.
paraplayer

Snifit
Level: 22

Posts: 117/280
EXP: 57271
For next: 1079

Since: 06-06-05

Since last post: 44 days
Last activity: 44 days
Posted on 06-21-05 06:44 AM Link | Quote
What is Lisp? Programming language?
Ramsus

Octoballoon
Level: 19

Posts: 119/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-21-05 07:37 AM Link | Quote
A really fun one, but I suspect only if you like abstract stuff.
paraplayer

Snifit
Level: 22

Posts: 120/280
EXP: 57271
For next: 1079

Since: 06-06-05

Since last post: 44 days
Last activity: 44 days
Posted on 06-21-05 07:47 AM Link | Quote
I'm probably asking this waaaaay to early on but how would i go about creating a multiplayer game?

I know a little bit about computers but i honestly how no clue how the internet works at all

First time i'll just make two circles one controlled by someone connecting to you and one by the host. Something simple.

Ramsus

Octoballoon
Level: 19

Posts: 120/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-21-05 08:56 AM Link | Quote
You'll want to learn about threads (which let you run multiple functions at the same time as "lightweight processes" that can access global variables) so you can cleanly separate things like network code, user input/output, logic, and graphics code in your program.

You'll need to learn about interprocess communication using sockets. Basically, sockets provide a way to set up network connections (and local connections) between two processes (running programs) and send data between them (using streams, packets, or whatever methods the protocol you choose supports).

Then you would create a game like any other, but have one program act as the server, and the other program connect to it. Then one program would send the other program the input it receives from its user while the other does the same. Every now and then, one of the programs (usually the server) sends the other one game engine information like stats and object locations so both programs stay in sync.

So what you really want to learn about is socket programming.

Google gave me this: http://www.ecst.csuchico.edu/~beej/guide/net/html
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Pointers and Arrays | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.020 seconds.