How are C++ Pointers Useful?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1938 Views - Last Post: 28 July 2011 - 08:36 AM Rate Topic: -----

#1 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

How are C++ Pointers Useful?

Posted 26 July 2011 - 12:47 PM

Hi, Im learning c++ from this site:C++ Tutorial

And there is an chapter about Pointers.


But how is it usefull?

Thanks.


Arno(Cornetto456)
Is This A Good Question/Topic? 0
  • +

Replies To: How are C++ Pointers Useful?

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: How are C++ Pointers Useful?

Posted 26 July 2011 - 12:55 PM

It teaches you all about pointers, if you wanted to lean. I'm not sure how else I could answer your question.
Was This Post Helpful? 1
  • +
  • -

#3 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: How are C++ Pointers Useful?

Posted 26 July 2011 - 12:58 PM

http://en.wikipedia....ramming%29#Uses
Was This Post Helpful? 1
  • +
  • -

#4 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: How are C++ Pointers Useful?

Posted 26 July 2011 - 01:09 PM

its very very very important and usefull. you must learn it completely

This post has been edited by ahura_24: 26 July 2011 - 01:10 PM

Was This Post Helpful? 1
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: How are C++ Pointers Useful?

Posted 26 July 2011 - 01:12 PM

If you're asking how useful pointers are, then the answer is very. Don't worry though, they are not difficult to understand.
Was This Post Helpful? 1
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: How are C++ Pointers Useful?

Posted 26 July 2011 - 01:12 PM

Well you might find this writeup I did a while back useful:

Re: Why use pointers? What's the point? #entry1070393
Was This Post Helpful? 1
  • +
  • -

#7 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: How are C++ Pointers Useful?

Posted 26 July 2011 - 02:10 PM

Thanks, for all the info.


:)


Arno,Cornetto456
Was This Post Helpful? 0
  • +
  • -

#8 ahura_24  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 69
  • Joined: 07-March 10

Re: How are C++ Pointers Useful?

Posted 26 July 2011 - 02:17 PM

if you want learn STL you must know pointer and function pointer very well
Was This Post Helpful? 1
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: How are C++ Pointers Useful?

Posted 26 July 2011 - 02:49 PM

View Postahura_24, on 26 July 2011 - 05:17 PM, said:

if you want learn STL you must know pointer and function pointer very well


Actually if you are doing it right you should almost never use a pointer with STL. Kind of the whole point is to abstract away from direct memory access via pointers.

I suppose you should understand the concept of a pointer but usually you will be using iterators not pointers.

Father more, you should not really need to know anything about function pointers. You may being using them but you probably will not have to think about them much and should not have to get into their crazy syntax.
Was This Post Helpful? 2
  • +
  • -

#10 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: How are C++ Pointers Useful?

Posted 27 July 2011 - 02:06 PM

There are two types of memory allocation: static and dynamic. Statically allocated objects are terminated at the end of the scope:
void function()
{
    int a;
    // a is terminated at the end of this function.
}



Dynamically allocated objects will remain existent even outside of the scope. This can be useful. To dynamically allocate an object in C++, you use the new
void function()
{
    new ClassType();

    // ClassType object will still exist at the end of this function.
}


As you can see, I created an object of ClassType. It exists in memory somewhere. To use it, I would have to reference it. In the above example, I cannot reference it because when I created it, I didn't assign it to anything I can reference it with. But below, I can.
ClassType* pointer = new ClassType();



Now I can reference the object I just created through the pointer: pointer->CallMethod();
Was This Post Helpful? 1
  • +
  • -

#11 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: How are C++ Pointers Useful?

Posted 27 July 2011 - 02:08 PM

So its actualy a global object?

This post has been edited by cornetto456: 27 July 2011 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#12 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: How are C++ Pointers Useful?

Posted 27 July 2011 - 02:20 PM

Yeah, I guess you can say that. It exists in memory. But your pointer is still scoped. For example:
void function()
{
  ClassType* pointer = new ClassType();
}


Your ClassType object will still exist, but after the function, your pointer will go out of scope. What I mean is if you try to reference the object like: pointer->CallMethod(); it won't work because pointer only existed in the scope of function.
Was This Post Helpful? 1
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: How are C++ Pointers Useful?

Posted 27 July 2011 - 02:45 PM

Quote

There are two types of memory allocation: static and dynamic. Statically allocated objects are terminated at the end of the scope:


This is actually not really true. There are a number of different ways to allocate memory in C/C++ (not counting OS specific ways).

There is:
Static memory or "data segment(s)" and your program can actually have multiple ones but generally there are 2, one for static data such as global variables and static variables in functions and classes. Then there is the const segment that generally contains all constants (for which memory was allocated) - this will contain all of the string literals your program uses etc. -- Note this memory does not consume available stack space - it also can not be resized (without OS specific functions) at runtime.

Local Data or "The Stack" or "automatic allocation" -- these are variables that are scoped. These exist on the stack and are "popped off" of the stack when the current function (or scope) ends. There are your local variables and function arguments.

-- note that technically "static memory" refers to the "data segment" and globaly scoped variables and static variables.

-- Note that you actually CAN allocate arbitrary blocks of memory on the stack like with new/malloc but it was found to be a poor practice however you will still find it in use in embedded systems that don't really have a "heap" to grab memory from.

Dynamic Memory or "the heap" OR "free store" -- memory allocated via the OS for your program's use by the new operator or malloc().

While we are at it, lets not forget about register variables (allocated as registers if available).

So there are many different ways to allocate memory. Most of them can be accessed via pointers.

pointers allow variables to transcend scope (like passing a pointer to an array in main to a function findSum which calculates the sum of the array and returns a value -- even though the array is out of findSum's scope, it can access the array via a pointer.)

Pointers let us pass small variables (the pointer is just an integer in memory) rather than large block back and forth. Imagine loading a large image into memory, you really don't want to have to keep making copies of this bitmap every time you wanted to pass it to a function.

Pointers let us access shared memory (like accessing the BIOS keyboard buffer (older DOS programming)) or buffers where we can get structured data from the OS.

Pointers let us have dynamic structures like linked lists and trees that can grow to huge sizes.

Pointers enable OOP.

Without pointers programming computers is really very hard. Even lanugages that don't give you direct access to pointers like BASIC and Java use pointers because there just is no way to do the things that they do without them.
Was This Post Helpful? 3
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1048
  • View blog
  • Posts: 4,455
  • Joined: 09-June 09

Re: How are C++ Pointers Useful?

Posted 27 July 2011 - 02:53 PM

Quote

So its actualy a global object?

No, it's handle is limited to the scope in which it is declared. A global variable's scope is that in which "Global" of course.

In sparkart's example, you can return a memory location from a function to prevent losing your pointer handle.

I'm not going to reiterate whhat everyone else has already said, but I'll add the advantages of pointer by using polymorphism.

Link : Polymorphism

This post has been edited by ImaSexy: 27 July 2011 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: How are C++ Pointers Useful?

Posted 27 July 2011 - 03:15 PM

View PostImaSexy, on 27 July 2011 - 05:53 PM, said:

Quote

So its actualy a global object?

No, it's handle is limited to the scope in which it is declared. A global variable's scope is that in which "Global" of course.


Well... no. Its handle is just an address and while it may be stored in a local pointer, when you leave the local scope without freeing the memory it is STILL allocated even if you no long have a pointer containing its handle. But the block of memory allocated remains and can be used if you have saved the handle somehow (like storing it in some pointer).

Dynamic objects don't really have "scope". As you said their handles or pointers have scope but then memory itself kind-a does not... well I suppose that it has "process scope" meaning that (at least on windows/linux) other processes do not necessarily have access to it and since you application can have multiple processes in one application I suppose it does have some sense of scope but really no. It is "like" a global variable in that it can be accessed from anywhere in the program. It is "unlike" a global variable in that it has limited life (can be de-allocated) and the pointers used to access it are generally scoped (no reason you can't put that address in a global variable though).

Summary: It is not a global variable as its scope depends upon you the programmer and is not determined by specification in the language.

it CAN be accessed globally if you choose, or it can be limited - but it is up to you.

its global-ish
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2