answersLogoWhite

0


Best Answer

A pointer is initialized by assigning the address of some object to it ...

int a; // an integer

int *pa; // a pointer

pa = &a; // initialize the pointer

(*pa); // use the pointer

... by allocating memoryand assigning that address to it ...

int *pa; // a pointer to an integer

pa = malloc (1 * sizeof(int)); // allocate

if (pa == NULL) {... exception processing ...}

(*pa); // use the pointer

... or by doing address computation with it ...

int a[10]; // an array of integers

int *pa; // a pointer to an integer

pa = &(a+3); // initialize to the fourth element

(*pa); // use the fourth element

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Pointers should be initialized for the same reason that all variables should be initialized. If you don't initialize them before using them, your program's behavior is undefined. In the case of pointers, this is particularly true because a random pointer could easily point to something valid, but the wrong thing, and changing that wrong thing will cause all sorts of weird, hard-to-explain, behavior; and it will be difficult to track this down, because the point of real failure will not be the same as the point of detected failure.

My practice is to always initialize pointers to NULL at the point of definition, unless you can initialize them immediately. This way, accidentally using one results in a hard crash, usually memory access violation (0xC5 for windows) at location zero.

I would fire any programmer that repeatedly fails to initialize objects before use, and I would similarly flunk any student that does the same thing. It is that important.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why is it a good practice to initialize pointers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When does segmentation fault occur for a prog?

You either reference memory that is non existent, or you attempt to modify memory that is read only. This is usually a result of failure to properly initialize or use pointers or arrays.


Why array is a implicit pointer?

An array of pointers is exactly what it sounds like - one or more pointers arranged in order in memory, accessible through a common base name and indexed as needed. Philosophically, there is no difference between an array of pointers and an array of objects...int a[10]; // 10 integers, named a[0], a[1], a[2], ..., a[9]int *b[10]; // 10 pointers to int, named b[0], b[1], b[2], ..., b[9]If you initialize the array of pointers...int i;for (i = 0; i < 10; i++) b[i] = &a[i];... then *b[0] would be the same as a[0], etc.


Is it possible to initialize null character in string?

yes we can initialize null characterfor example syntax :string='\0';


Which condition is not necessary in dynamic stack?

in dynamic stack we don't have to initialize the size of array while in static stack we have 2 initialize it ......


Why do we need constructor?

when the object is instantiated(created) and delared,it has to assigned with variables.constructor is used for this purpose. syntax: classname ojbectname=new classname(); here classname() is a constructor. eg: box bb=new box(5,10); when this object is instantiated,it can be directly accessed by the constructor(similar to method but without a void because the instance variables are directly used) box(5,10) { }

Related questions

How do you get better at shooting 3 pointers?

Practice.


When does segmentation fault occur for a prog?

You either reference memory that is non existent, or you attempt to modify memory that is read only. This is usually a result of failure to properly initialize or use pointers or arrays.


Why array is a implicit pointer?

An array of pointers is exactly what it sounds like - one or more pointers arranged in order in memory, accessible through a common base name and indexed as needed. Philosophically, there is no difference between an array of pointers and an array of objects...int a[10]; // 10 integers, named a[0], a[1], a[2], ..., a[9]int *b[10]; // 10 pointers to int, named b[0], b[1], b[2], ..., b[9]If you initialize the array of pointers...int i;for (i = 0; i < 10; i++) b[i] = &a[i];... then *b[0] would be the same as a[0], etc.


How do you initialize variable?

initialize simple types: int i = 0; initialize objects: Object o = null; (in java)


What does initialize mean?

The term "initialize" means to start, begin, or prepare. Often the word "initialize" is used in context with starting something such as a program or sequence.


Why can't java compiler initialize local variables?

Its not that the compiler can't initialize local variables; its that the compiler does not initialize local variables.This is by design and language specification. If you want to initialize local variables, you must explicitly do so.


Do you have to initialize your SIM card?

No - there's no need to initialize it. Simply insert it in the handset for it to work.


Are German shorthaired pointers good water retrieving dogs?

yes


Where are lasers for sale in the UK?

There are many stockists of lasers in the UK. A good source is ebay if you are looking for a laser pointer. Another is laser pointers. Amazon is also a good source of different laser pens and pointers.


How do you now your good at basketball?

Practice, Practice, Practice, and have a good coach.


How can I get better at soccer and score goals?

One way to get better at soccer is to practice! Get together with other soccer players and ask them to give you pointers and practice with you. Make some home made goals at home and see if you can score. Practice makes perfect


Is it possible to initialize null character in string?

yes we can initialize null characterfor example syntax :string='\0';