We help IT Professionals succeed at work.

initializing pointers in general and inside functions or structures

mitchguy
mitchguy asked
on
I recently had a bug in a program in which the use of a cout<< statement caused my program to crash in this one particular function. I had a pointer declared, but not initialized and after i initialized it to NULL the bug went away. So i was wondering do you always need to initialize pointers to something or just in functions and also what about in structures, for instance if i had in a header file.     struct object{
                      char *chptr
                      int *intptr
                      const char *cchar
                     };
when i declare in my program
oject example;
should i initialize these pointers?
Comment
Watch Question

Commented:
I would suggest that setting it to NULL merely masked a problem rather than solving it.  If a variable, pointer or otherwise, is never used, it matters not if it's been initialized or not.
As a rule of thumb: "Always initialize everything".
It's just worth it.

Regarding the sample code you've posted - you need to create a constructor for this class and that constructor should initialize the pointers to NULL (or whatever).
It shouldn't be the responsibility of the user of class, but of the class itself.
> I would suggest that setting it to NULL merely masked a
> problem rather than solving it.

this may well not be true... consider:

struct X
{
  X()
    : mx(0) // Initialise ... remove for bug...
  {}
 
  ~X()
  { delete mx; }

  int* mx;
};

using it like:
X* x = new X;
delete x;

failure to initialise in this case will definitely lead to a bug.
this is what probably happened:

u declared a pointer without initializing it. and then use cout to print the string associated or some such thing.

char* str;
cout<<str;

this crach is but something expected. this is what youve done. you are accessing the string pointed to by str which may not point to any valid null terminated string belonging to the data segment. if any part of the previous statement is true, then that is the reason for ur error.

u can get away from such errors by simply initializing the locations pointed to by the pointers, (not necessarily the pointer itself, as u have done). for example in ur case, just add some constructor to ur structure and use the following optional statements:

object ob1;
or
object ob1=new object[size];

i preffer the latter, since, that helps in getting pointers pointing to the data in the data segment only and not some invalid place.

anyway, initailizing any varaible always helps, though that is not necessary, if you are taking the necessary care.

have fun

Commented:
And therein lies the problem I was talking about.  By initializing it to something so as to avoid an error, one makes debugging a tad harder when the program runs but doesn't work.

Author

Commented:
I would suggest that setting it to NULL merely masked a problem rather than solving it.  If a variable,
pointer or otherwise, is never used, it matters not if it's been initialized or not.

so if i have a procedure and at the top i declare a pointer
and then assign it later in the program when i need it, i should not have any problems? I was recently told that since i didn't intialize it to something that what ever was in memory at the place where i declared it was now gone and could effect the way my program ran. So i thought i would try to clarify whether this was true or not.
Commented:
...
char *thingy;

If never initialised and never used, all you've lost is a miniscule amount of compile time.


'and then assign it later in the program when i need it, i should not have any problems?'
Assuming it's assigned and used correctly, no, you won't - regardless of whatever trash may have been in it.

'I was recently told that since i didn't intialize it to something that what ever was in memory at the place where i declared it was now gone and could effect the way my program ran'
Somebody likes to feed you drivel.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.