6 Replies - 512 Views - Last Post: 19 July 2010 - 05:56 PM Rate Topic: -----

#1 Guest_Wondering*


Reputation:

Pointers

Posted 19 July 2010 - 04:27 PM

“Pointers are without a doubt one of the most important—and troublesome—aspects of C++. In fact, a large measure of C++’s power is derived from pointers.”

Resource- "C++ from the ground up" pg. 106 - Herbert Schildt

Ok…

int main()
{
int balance;
int *balptr;
int value;
balance = 3200;
balptr = &balance;
value = *balptr;
cout << "balance is: " << value << '\n';
return 0;
}


Ok. I get it. You access one variable indirectly through another variable.
Depending on the type of pointer, a certain amount of memory is set aside
Char 1 bit, int, float get 4 and double gets 8 bits… I get it.
int *p;
(*p)++; // adds 4 because of memory allocation

-- I get that too.

What I don’t get, is WHY is it so powerful? I could have just as easily assigned
value=balance;


Now, I realize I am new, and I’m just starting with pointers, just touching the surface. I also realize the c++ programmers reading this are probably rolling their eyes, and having a seizure at the thought of someone asking about the power of a pointer as you read this… Please forgive me… But in plain, simple 3rd grade english, can someone tell me please, what’s the big to do about pointers? I don't get the whole concept...

Is This A Good Question/Topic? 0

Replies To: Pointers

#2 adtd8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-July 10

Re: Pointers

Posted 19 July 2010 - 04:32 PM

View PostWondering, on 19 July 2010 - 03:27 PM, said:

“Pointers are without a doubt one of the most important—and troublesome—aspects of C++. In fact, a large measure of C++’s power is derived from pointers.”

Resource- "C++ from the ground up" pg. 106 - Herbert Schildt

Ok…

int main()
{
int balance;
int *balptr;
int value;
balance = 3200;
balptr = &balance;
value = *balptr;
cout << "balance is: " << value << '\n';
return 0;
}


Ok. I get it. You access one variable indirectly through another variable.
Depending on the type of pointer, a certain amount of memory is set aside
Char 1 bit, int, float get 4 and double gets 8 bits… I get it.
int *p;
(*p)++; // adds 4 because of memory allocation

-- I get that too.

What I don’t get, is WHY is it so powerful? I could have just as easily assigned
value=balance;


Now, I realize I am new, and I’m just starting with pointers, just touching the surface. I also realize the c++ programmers reading this are probably rolling their eyes, and having a seizure at the thought of someone asking about the power of a pointer as you read this… Please forgive me… But in plain, simple 3rd grade english, can someone tell me please, what’s the big to do about pointers? I don't get the whole concept...


One aspect of it, is when you deal with memory allocation. When you allocate some piece of memory, then it doesn't matter when you enter or leave a function, that pointer still points to the allocated memory.

Hope this helps a little.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: Pointers

Posted 19 July 2010 - 04:34 PM

So it can help you get around the scope issue? Ok.. That is a plus.
Was This Post Helpful? 0

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,451
  • Joined: 09-June 09

Re: Pointers

Posted 19 July 2010 - 04:39 PM

There are many different things you can do with pointers that you cannot do in most languages.

Dynamic Memory Allocation
- very large data should be allocated from the heap
- You don't know at compile time the length of the array to allocate,
- Improve code efficency
Scope Advanages (scroll to lesson 9.3
- You need a variable to live longer than the scope in which it is declared


those are just off the top of my head

Posted Image

This post has been edited by ImaSexy: 19 July 2010 - 04:42 PM

Was This Post Helpful? 2
  • +
  • -

#5 Guest_Guest*


Reputation:

Re: Pointers

Posted 19 July 2010 - 04:58 PM

Ok... So for example (Psuedo code only)
int number[5000];
int i=1 to 5000{
number[i]=rand()%50)+1;
}

i=1 to 5000{
int temp=0;
if number[i]>temp{
temp=number[i];
}
}


temp should = largerst number of array... (give or take)

Lets assume this was a code of sorts. How could pointers help me out in a case like this. I see it eats up 5000 bytes of memory. But that gets dumped once the function ends. So that shouldn't be a major issue, or am I missing something else. I'm just trying to understand, I'm not looking for, nor require a code. Or is this just a function that pointers would be useless in. If it is, maybe you have a better example.
Was This Post Helpful? 0

#6 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Pointers

Posted 19 July 2010 - 05:33 PM

Our very own NickDMax wrote a great entry on the subject today. You can find it here:
http://www.dreaminco...ost__p__1070393

To summarize what he said, pointers help your program in many ways:
1. They're dynamic. This means you can allocate memory with them, and that memory can grow as needed. In your above example, there were 5000 ints, but with pointers, the user could specify any number of int, and you could handle it correctly.

Pointers are fundamental to data structures because of this. Even the simplest 'dynamic array' couldn't exist without pointers.

2. They're efficient. A pointer holds and address of something. Passing it around is roughly equivalent to passing an int. Now, imagine you have a large object; something that is large in bytes. Passing it around would cause a lot of copying to data. Enter the pointer. Now you can pass it to a function quickly, and efficiently. You're only copying a pointer, and that's cheap.

3. They modify data passed. As has been stated, a pointer 'points' to an object. In normal passing 'pass-by-copy' (also called "pass-by-value") the object used isn't the actual object passed. It's an equivalent 'copy' that is assigned to the parameter. To illustrate this, consider the following:
#include <iostream>
using namespace std;
void pass_by_copy(int c) {
    c = 5;
}

void pass_by_pointer(int* p) {
    *p = 5;
}

int main() {
    int x = 7;
    cout << "x = " << x << '\n';

    // pass by copy...
    pass_by_copy(x);
    // ...means x isn't modified
    cout << "x = " << x << '\n';

    // pass by pointer...
    pass_by_pointer(&x);
    // ...means x is modified
    cout << "x = " << x << '\n';
}


As you can see, even though the variable passed to pass_by_copy is assigned a value, that is not reflected in the original 'x'. But since pass_by_pointer works with addresses, the update can be seen.

Anyways, I'm just scratching the surface. Take a look at the link to get even more info.

Hope I helped :)
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Wondering*


Reputation:

Re: Pointers

Posted 19 July 2010 - 05:56 PM

Ok ok ok... I'm convienced.

I'll finish reading the chapter, and then I'll check out Mr. Nicks intro. I think pointers will be tough, but it seems to be a concept worth l

Thank you gents (and ladies if it applies)...
Was This Post Helpful? 0

Page 1 of 1