LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2008, 08:56 AM   #1
trashbird1240
Member
 
Registered: Sep 2006
Location: Durham, NC
Distribution: Slackware, Ubuntu (yes, both)
Posts: 463

Rep: Reputation: 31
General Question About C Pointer Initialization


I read in an introductory C text that it's "generally hazardous" to
introduce an unitialized pointer:
Code:
int *pointer;
so it's better to do this:
Code:
int *pointer = NULL;
Question 1: Why is it generally hazardous? What do I risk leaving it
uninitialized?
Question 2: If I get a segfault when I initialize the pointer, what's
the most likely cause? For example, failure to include a header?

Thanks,
Joel
 
Old 01-14-2008, 09:04 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

The danger with "int *pointer" is that if you declare it like that, and then use it later with something like printf("%d",*pointer) the value printed out is undefined. You would usually get a segmentation fault, but not if your pointer happens to point to some part of memory that belongs to your program. So this kind of bugs can be very hard to find. Your program can work when you compile it, but if you compile it again, maybe not. With "int *pointer = NULL" you will always get a segmentation fault since you are not allowed access to the memory at 0. Then the bug will be a lot easier to find and fix.
 
Old 01-14-2008, 11:03 AM   #3
trashbird1240
Member
 
Registered: Sep 2006
Location: Durham, NC
Distribution: Slackware, Ubuntu (yes, both)
Posts: 463

Original Poster
Rep: Reputation: 31
Excellent explanation.

Thanks!
Joel
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer question, djgerbavore Programming 6 08-13-2004 02:21 AM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM
Initialization of a pointer in C-a question xailer Programming 4 01-02-2004 12:57 PM
Initialization Question. Crossfire Linux - Newbie 3 07-27-2002 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration