Archived

This topic is now archived and is closed to further replies.

Pointers

This topic is 5749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why do we use pointers.I can''t understsnd why we use them because we can assign a value to a variable either way why do it through a pointer? No comments like those on the previous post of mine please.

Share this post


Link to post
Share on other sites
there are lots of cases you need pointers. just the first thing that comes to my mind is that; you can control the lifetime of your object.

say you have a class that loads bitmap images into memory. each instance of the class, by definition will consume a lot of memory. because bitmaps require large memory chunks. when you declare your object normally (not pointer) you immediately create an instance of it, and it will live as long as you are in the scope it is created. by pointers, you can control its lifetime. such that;

    
XBitmap* pBitmap; // no allocations, just a simple long value for the address is reserved, constructor isn't called yet

XBitmap myBitmap; // constructor called, object is alive


//doing sth here not related to XBitmap


pBitmap = new XBitmap; // now the constructor is called


//doing sth here about XBitmap objects


delete pBitmap; //pBitmap is destructed, memory released


// note that myBitmap is still alive although we don't need it anymore


// keep on doing sth.. stupid myBitmap is STILL alive




hope that gives an idea..

cheers

-----------
my quote is under construction

[edited by - mentat on March 20, 2002 9:28:16 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Pointers become usefull as you get more advanced :D I use pointers for things like addressing memory...


  
//Clears a buffer of size.

void ClearBuffer(void *buff, int size)
{
unsigned long *buff_ptr = (unsigned long*)buff;
unsigned char *buff_ptr2;
int left_over=size&3;
size>>=2;
while (size--)
*(buff_ptr++)=0;
buff_ptr2 = (unsigned char*)buff_ptr;
while (left_over--)
*(buff_ptr2++)=0;
}


Now, this may look kinda of cryptic and it is, but it is a good example of how to use pointers. You pass a memory address over (for the pointer to point to).. this can be of any type (void), which means, you can call it like so:

struct Test_S
{
int Number;
int Size;
int Age;
};

Test_S Test;

ClearBuffer(&Test,sizeof(Test_S));

The function takes this pointer to Test (buff) and then creates 2 new pointers out of it (buff_ptr and buff_ptr2). The first is an unsigned long (4 bytes) and the second is an unsigned char (1 byte). This is so I can set 4 bytes at a time to 0 and then with the leftovers (if it''s not divisible by 4) set them 1 at a time to 0. This really isn''t possible without pointers. Another example would be things like... linked lists.. or dynamically allocated arrays and stuff.

Say you have Test_S Test[15]; //15 Test_S''s

And you are going to have 1 that''s current all the time..

Test_S *Current_Test = &Test[0];

When this changes you can simply Current_Test = &Test[x]; And you don''t need to worry about which is current.. simply call Current_Test->Number, etc... instead of actually knowing which is current (this can be done just as easily with an index variable though, but just an example).

When working with linked lists... you need a pointer to the next item in the list (and previous if you want) and also, your head (first item) and tail (last item). Since a linked list is dynamically created, you have to use pointers to the memory that was allocated in order to use them.

Billy - BillyB@mrsnj.com

ps. I hope this helped at least a little :D

Share this post


Link to post
Share on other sites
As stated above pointers have all kinds of nifty uses. A classic example is the swap(a,b) function. Here you need to modify more than one value. Since functions return only one value, you can''t go that route. To do this you would write a function like...

  
void swap ( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

You can''t do it like this...

  
void swap ( int a, int b )
{
int temp;
temp = a;
a = b;
b = temp;
}

...the reason being that this function is passing the arguments _by_value_, that is the function gets a copy of the values, where as in the first function the arguments are being passed _by_reference_, or in other words the function gets the addresses of the arguments. Doing this in this manner is obsolete in C++ though, but I''ll not go there.

Another use is when you need to pass a string to a function. You would decalare the function and use it thusly...

  
int main ()
{
char some_string[] = "Blah blah blah";

string (some_string);

return 0;
}

void string_func ( char * string )
{
/* code goes here... */
}

Notice that when I call the funtion the string didn''t have its brackets. That''s because an array without it''s brackets is a pointer to the first element, in this case a pointer to a character, exactly what the function expects.

OK, one more thing then I''ll stop. When you need to pass a large structure to a function it''s always a good idea to pass it by reference where the function only gets a pointer to the structure as opposed to by value where the function will get a new copy of the large structure. Again though C++ has made using pointers for this obsolete. Whew... OK, I hope that helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
without getting into specific examples, using pointers allows for the ''grammer to save memory, extend the life of instances out of their normal scope, transfer several effective return values from functions, and of course: make huge disgusting memory leaks. Theres probably a couple of other thigns pointers can do, but i can''t think of everything. Basically, sometimes you don''t want to look at the value of the variable, but rather, you want to know where that value is in memory.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Pointers become usefull as you get more advanced

And then they become less useful as you get even more advanced. :o

Or, that should be, they don''t become less useful but they should become less prevalent. For instance, the two examples that microdot posted should use references rather than pointers. A lot of "traditional" uses of pointers should also become concealed behind standard library containers such as string or vector, or a smart pointer, or a handle class, or... you get the picture.

Share this post


Link to post
Share on other sites
Thanks guys even though I have a hard time understanding the examples which are in my book about the -> pointer and the this pointer and generally the pointers(I jusy hate those cat examples)

Share this post


Link to post
Share on other sites