Engineering Forum

India Education

Engineering Colleges Forum

Pointers

This is a discussion on Pointers within the Computer engineering forums, part of the ENGINEERING WORLD category; Introducing Pointers In the previous lectures, we have not given many methods to control the amount of memory used in ...


Go Back   Engineering Forum > ENGINEERING WORLD > Computer engineering

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

   

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 08-28-2008, 06:31 PM
aayush_005's Avatar
Administrator
 
Join Date: Aug 2008
Posts: 225
Default Pointers

Introducing Pointers

In the previous lectures, we have not given many methods to control the amount of memory used in a program. In particular, in all of the programs we have looked at so far, a certain amount of memory is reserved for each declared variable at compilation time, and this memory is retained for the variable as long as the program or block in which the variable is defined is active. In this lecture we introduce the notion of a pointer, which gives the programmer a greater level of control over the way the program allocates and de-allocates memory during its execution.
Declaring Pointers

A pointer is just the memory address of a variable, so that a pointer variable is just a variable in which we can store different memory addresses. Pointer variables are declared using a "*", and have data types like the other variables we have seen. For example, the declaration
int *number_ptr;

states that "number_ptr" is a pointer variable that can store addresses of variables of data type "int". A useful alternative way to declare pointers is using a "typedef" construct. For example, if we include the statement:
typedef int *IntPtrType;

we can then go on to declare several pointer variables in one line, without the need to prefix each with a "*":
IntPtrType number_ptr1, number_ptr2, number_ptr3;

(BACK TO COURSE CONTENTS)



Assignments with Pointers Using the Operators "*" and "&"

Given a particular data type, such as "int", we can write assignment statements involving both ordinary variables and pointer variables of this data type using the dereference operator "*" and the (complementary) address-of operator "&". Roughly speaking, "*" means "the variable located at the address", and "&" means "the address of the variable". We can illustrate the uses of these operators with a simple example program:
#include <iostream>
using namespace std;

typedef int *IntPtrType;

int main()
{
IntPtrType ptr_a, ptr_b;
int num_c = 4, num_d = 7;

ptr_a = &num_c; /* LINE 10 */
ptr_b = ptr_a; /* LINE 11 */

cout << *ptr_a << " " << *ptr_b << "\n";

ptr_b = &num_d; /* LINE 15 */

cout << *ptr_a << " " << *ptr_b << "\n";

*ptr_a = *ptr_b; /* LINE 19 */

cout << *ptr_a << " " << *ptr_b << "\n";

cout << num_c << " " << *&*&*&num_c << "\n";

return 0;
}
Program 7.1.1

The output of this program is:
4 4
4 7
7 7
7 7

Diagramatically, the state of the program after the assignments at lines 10 and 11 is:



after the assignment at line 15 this changes to:



and after the assignment at line 19 it becomes:



Note that "*" and "&" are in a certain sense complementary operations; "*&*&*&num_c" is simply "num_c".
(BACK TO COURSE CONTENTS)



The "new" and "delete" operators, and the constant "NULL".

In Program 7.1.1, the assignment statement
ptr_a = &num_c;

(in line 10) effectively gives an alternative name to the variable "num_c", which can now also be referred to as "*ptr_a". As we shall see, it is often convenient (in terms of memory management) to use dynamic variables in programs. These variables have no independent identifiers, and so can only be referred to by dereferenced pointer variables such as "*ptr_a" and "*ptr_b".

Dynamic variables are "created" using the reserved word "new", and "destroyed" (thus freeing-up memory for other uses) using the reserved word "delete". Below is a program analogous to Program 7.1.1, which illustrates the use of these operations:
#include <iostream>
using namespace std;

typedef int *IntPtrType;

int main()
{
IntPtrType ptr_a, ptr_b; /* LINE 7 */

ptr_a = new int; /* LINE 9 */
*ptr_a = 4;
ptr_b = ptr_a; /* LINE 11 */

cout << *ptr_a << " " << *ptr_b << "\n";

ptr_b = new int; /* LINE 15 */
*ptr_b = 7; /* LINE 16 */

cout << *ptr_a << " " << *ptr_b << "\n";

delete ptr_a;
ptr_a = ptr_b; /* LINE 21 */

cout << *ptr_a << " " << *ptr_b << "\n";

delete ptr_a; /* LINE 25 */

return 0;
}
Program 7.1.2

The output of this program is:
4 4
4 7
7 7

The state of the program after the declarations in line 7 is:



after the assignments in lines 9, 10 and 11 this changes to:



after the assignments at lines 15 and 16 the state is:



and after the assignment at line 21 it becomes:



Finally, after the "delete" statement in lines 25, the program state returns to:



In the first and last diagrams above, the pointers "ptr_a" and "ptr_b" are said to be dangling. Note that "ptr_b" is dangling at the end of the program even though it has not been explicitly included in a "delete" statement.

If "ptr" is a dangling pointer, use of the corresponding dereferenced expression "*ptr" produces unpredictable (and sometimes disastrous) results. Unfortunately, C++ does not provide any inbuilt mechanisms to check for dangling pointers. However, safeguards can be added to a program using the special symbolic memory address "NULL". Any pointer of any data type can be set to "NULL". For example, if we planned to extend Program 7.1.2 and wanted to safeguard against inappropriate use of the dereferenced pointer identifiers "*ptr_a" and "*ptr_b", we could add code as follows:
#include <iostream>
...
...
delete ptr_a;
ptr_a = NULL;
ptr_b = NULL;
...
...
if (ptr_a != NULL)
{
*ptr_a = ...
...
...
Fragment of Program 7.1.3

In the case that there is not sufficient memory to create a dynamic variable of the appropriate data type after a call to "new", C++ automatically sets the corresponding pointer to "NULL". Hence the following code typifies the kind of safety measure that might be included in a program using dynamic variables:
#include <iostream>
#include <cstdlib> /* ("exit()" is defined in <cstdlib>) */
...
...
ptr_a = new int;
if (ptr_a == NULL)
{
cout << "Sorry, ran out of memory";
exit(1);
}
...
...
Fragment of Program 7.1.4

Pointers can be used in the standard way as function parameters, so it would be even better to package up this code in a function:
void assign_new_int(IntPtrType &ptr)
{
ptr = new int;
if (ptr == NULL)
{
cout << "Sorry, ran out of memory";
exit(1);
}
}
Fragment of Program 7.1.5

(BACK TO COURSE CONTENTS)



7.2 Array Variables and Pointer Arithmetic

In the last lecture we saw how to declare groups of variables called arrays. By adding the statement
int hours[6];

we could then use the identifiers
hours[0] hours[1] hours[2] hours[3] hours[4] hours[5]

as though each referred to a separate variable. In fact, C++ implements arrays simply by regarding array identifiers such as "hours" as pointers. Thus if we add the integer pointer declaration
int *ptr;

to the same program, it is now perfectly legal to follow this by the assignment
ptr = hours;

After the execution of this statement, both "ptr" and "hours" point to the integer variable referred to as "hours[0]". Thus "hours[0]", "*hours", and "*ptr" are now all different names for the same variable. The variables "hours[1]", "hours[2]", etc. now also have alternative names. We can refer to them either as
*(hours + 1) *(hours + 2) ...

or as
*(ptr + 1) *(ptr + 2) ...

In this case, the "+ 2" is shorthand for "plus enough memory to store 2 integer values". We refer to the addition and subtraction of numerical values to and from pointer variables in this manner as pointer arithmetic. Multiplication and division cannot be used in pointer arithmetic, but the increment and decrement operators "++" and "--" can be used, and one pointer can be subtracted from another of the same type.

Pointer arithmetic gives an alternative and sometimes more succinct method of manipulating arrays. The following is a function to convert a string to upper case letters:
void ChangeToUpperCase(char phrase[])
{
int index = 0;
while (phrase[index] != '\0')
{
if (LowerCase(phrase[index]))
ChangeToUpperCase(phrase[index]);
index++;
}
}

int LowerCase(char character)
{
return (character >= 'a' && character <= 'z');
}

void ChangeToUpperCase(char &character)
{
character += 'A' - 'a';
}
Fragment of Program 7.2.1

Note the use of polymorphism with the function "ChangeToUpperCase(...)" - the compiler can distinguish the two versions because one takes an argument of type "char", whereas the other takes an array argument. Since "phrase" is really a pointer variable, the array argument version can be re-written using pointer arithmetic:
void ChangeToUpperCase(char *phrase)
{
while (*phrase != '\0')
{
if (LowerCase(*phrase))
ChangeToUpperCase(*phrase);
phrase++;
}
}
Fragment of Program 7.2.2

This re-writing is transparent as far as the rest of the program is concerned - either version can be called in the normal manner using a string argument:
char a_string[] = "Hello World";
...
...
ChangeToUpperCase(a_string);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-04-2009, 04:32 AM
Junior Member
 
Join Date: May 2009
Location: England
Posts: 1
Send a message via ICQ to MagicOPromotion
Default Help for downloading for free

Hi, my friends... I want to get program XRumer 5.07 Palladium for free. Have you any url???
I'm so need this magic program! It's can break captchas automatically! Activate accounts via email automatically too! Absolutely great software! Help me!
And did you hear news - price for XRumer 5.0 Palladium will grow up to $540 after 15 may 2009... And XRumer 2.9 and 3.0 - too old versions, it's cant break modern catpchas and cant break modern anti-bot protections. But XRumer 5.0 Palladium CAN!!!!
So help me for download this great program for free! Thanks!
__________________
XRumer 5.0 Palladium: best promotion software
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-20-2009, 11:25 AM
Junior Member
 
Join Date: May 2009
Location: USA
Posts: 1
Send a message via ICQ to JLoes
Default Help for downloading for free

hey!
there is NO cracks for XRumer 4.0 and XRumer 5.0
Just buy it. This software really effective and powerful.
Botmaster.Net : Powerful SEO software - for english
Botmaster.Ru : лучший софт для вебмастера - for russian

Good luck.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-21-2009, 11:51 PM
Junior Member
 
Join Date: May 2009
Posts: 1
Default Need to download XRumer 5.0 for free!

Yes.
XRumer 5.0 Palladium it the best soft for promo anyway.
I was promoted more than 100 my web-satellites up to Google's TOP-10 (keywords - adult and pharma)
__________________
Cookies, yam-yam!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT. The time now is 09:49 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.