www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/14/11:44:51

From: "Chris A. Triebel" <cat AT sun4 DOT iol DOT unh DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How assign to NULL POINTER
Date: Thu, 14 Nov 1996 08:41:56 -0500
Organization: University of New Hampshire - Durham, NH
Lines: 99
Message-ID: <Pine.SUN.3.91.961114082846.14107E-100000@sun4.iol.unh.edu>
References: <9611131456 DOT AA13732 AT emma DOT ruc DOT dk>
NNTP-Posting-Host: sun4.iol.unh.edu
Mime-Version: 1.0
In-Reply-To: <9611131456.AA13732@emma.ruc.dk>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Wed, 13 Nov 1996, Ole Winther wrote:

> Date: Wed, 13 NOV 1996 14:56:22 GMT 
> From: Ole Winther <owsim AT ruc DOT dk>
> To: djgpp AT delorie DOT com
> Newgroups: comp.os.msdos.djgpp
> Subject: How assign to NULL POINTER 
> 
> I where asking about course of "segment violation at pointer " error, while
> running a gcc compiled program. I got the answer that it probaly is a Null
> pointer assignment and I must belive it'sw true, course the "BC" compiled
> program which where running the same code, was also complaining about the
> Null pointer assignment while program end was ended. 
> 
> I really dont know how I can assign value's to the NULL pointer, can someone
> describe how this can be done?
> 

You aren't supposed to assign to a NULL pointer!  It is guaranteed to be 
an invalid address which you can not use, but can test against.  This is 
useful in making sure that allocation occurs correctly, or if you want to 
override a parameter or something.

Let me try to throw some code to explain what I think I am failing to
Hopefully this will answer your question.  But anyway, you cannot officially
read/write/modify the address defined by NULL!  ( in the 16 bit world this
may or may not crash your code, and more likely will crash the machine ).

cat
-------------------------------------------------------------------------------
"It's not the programming languages that will kill you, its the syntax."
------------------------------------------------------------------------------
// sample #1
// this is obviously a dumb example, but I hope it gets the point across

int main(void)
{
.
.
.
	if (NULL==foo1())
	{
		cout << "foo1() FAILED" << endl;
		return 1;
	}
	else
	{
		cout << "foo1() DONE" << endl;
		return 0;
	}

}


void *foo1(void)
{
	if (sometest())	// if a test returns true then return a failure
	{
		return NULL;
	}
	else
	{
		return someptrvalue;  // if this is NULL it will cause the
				      // main() code to jam
	}
}

------------------------------------------------------------------------------
// sample #2

int main(void)
{
	char name[]="cat";

	foo2(foo2(name));
return 0;
}

char *foo2(char *someone)
{
	if (NULL==someone)
	{
		cout << "NO NAME PROVIDED" << endl;
		return NULL;
	}
	else
	{
		cout << name << endl;
		return name;
	}
}


output:

cat
NO NAME PROVIDED


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019