12 Replies - 650 Views - Last Post: 28 May 2019 - 07:00 PM Rate Topic: -----

#1 TonyRymond   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-February 19

Null pointer in C/C++

Posted 23 May 2019 - 03:13 PM

Hi !
About the Null pointer, I know what it's and how we can manipulate it over software programs that we program ! but I'm still confused about it, what's confusing me, is null pointer as NULL is a pointer in PC that PC knows it's NULL/TRASH?
if so, as we can assume every PC understand what's NULL pointer? HOW's NULL pointer translated to the PC ? is the compiler converting it to ZERO? if so , as how PC knows it's NULL?!

sorry for that question but it's really confusing !

what's I know about NULL, is something like TRASH OR ERROR but how PC deal with it? the PC doesn't have trash as I what explained but how he's dealing with it?

maybe NULL is a pointer having specific addrss that's operating system give us?

Is This A Good Question/Topic? 2
  • +

Replies To: Null pointer in C/C++

#2 jimblumberg   User is offline

  • member icon

Reputation: 5732
  • View blog
  • Posts: 17,558
  • Joined: 25-December 09

Re: Null pointer in C/C++

Posted 23 May 2019 - 03:26 PM

A NULL pointer (nullptr in modern C++) is a pointer that basically points to "nothing".

What do you mean by PC in the above? A Personal Computer has no knowledge of a NULL pointer, or any other pointer for that matter. A NULL pointer and pointers in general are C language constructs.

A pointer can only be a nulptr if something explicitly sets the pointer to nulptr otherwise a pointer points to some address.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6929
  • View blog
  • Posts: 23,546
  • Joined: 05-May 12

Re: Null pointer in C/C++

Posted 23 May 2019 - 03:29 PM

An interesting aside that is related to this discussion: Null Pointer References: The Billion Dollar Mistake
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7434
  • View blog
  • Posts: 15,411
  • Joined: 16-October 07

Re: Null pointer in C/C++

Posted 24 May 2019 - 02:13 AM

View PostTonyRymond, on 23 May 2019 - 05:13 PM, said:

is the compiler converting it to ZERO? if so , as how PC knows it's NULL?!

You are correct, zero and null are two different concepts. Interestingly, zero mostly functions as null in the domain of real numbers. Outside the numeric domain, when describing any value, zero is still a numeric value, so fails as a nil there.

The domain of pointers is actually different than the domain of any value. A pointer has a meaning: it holds an address which contains data. For a pointer to be null, then, the address it contains need simply be invalid for a given system. That address could be 42, or anything below 0x100, or some system constant, or whatever. However, since most languages follow the convention of zero being truthy, or false, then NULL ~= FALSE ~= 0 is a helpful convention.

From a C perspective:
int n = 0; // not null, is zero
int *p = &n; // not null, points to address of n, currently with a value of zero
p = NULL; // p is now null and points to... doesn't matter, as long as p == NULL is true


Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2348
  • View blog
  • Posts: 4,463
  • Joined: 30-May 10

Re: Null pointer in C/C++

Posted 24 May 2019 - 02:37 AM

How many times does ryan need telling?
https://cboard.cprog...ll-pointer.html
https://cboard.cprog...-subject-c.html
https://cboard.cprog...69640-null.html
https://cboard.cprog...69356-null.html
https://cboard.cprog...ll-pointer.html
https://cboard.cprog...-statement.html
https://cboard.cprog...l-operator.html

4 years of being clueless.

See also http://slash7.com/2006/12/22/vampires/
Was This Post Helpful? 2
  • +
  • -

#6 unixplumber   User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 8
  • Joined: 02-January 19

Re: Null pointer in C/C++

Posted 24 May 2019 - 10:59 AM

Sorry guys, I accidentally clicked on the "+" in the OP to give this... being... a positive reputation. I wish I could find a way to undo it.

RyanC:TonyRymond: Are you deliberately asking the same questions over and over again just to annoy everyone else on this forum and other forums? Or do you have some sort of learning disability?
Was This Post Helpful? 1
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15077
  • View blog
  • Posts: 60,210
  • Joined: 12-June 08

Re: Null pointer in C/C++

Posted 24 May 2019 - 11:17 AM

Woah.. pump the breaks on the condescension.

Though I appreciate the information, but if someone has information this is a duplicate account hit the report button or shoot me a PM. I mean help vampire is as a help vampire does.
Was This Post Helpful? 0
  • +
  • -

#8 TonyRymond   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-February 19

Re: Null pointer in C/C++

Posted 26 May 2019 - 01:37 PM

Hi ! , just last question about NULL pointer and I really appreciate your effort.

what's still confusing me, is NULL pointer an constant address for every PC ? I mean we know that NULL pointer is 0x00000000000000 (depend 32bit or 64bit) , what I'm asking, is how will the PC know that this address "0x000000" is unique and not deferencing to any value? is that the management of the compiler to tell the PC that this address is NULL ?

Or maybe who made the architecture of the PC define that at address 0x0000 can't be deferencing pointer?

what's confusing me, how PC knows that this value "NULL" can't be deferenced ... ?!!!



thanks alot and sorry for that question, but it's always plot up in my mind.
Was This Post Helpful? -2
  • +
  • -

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7434
  • View blog
  • Posts: 15,411
  • Joined: 16-October 07

Re: Null pointer in C/C++

Posted 26 May 2019 - 02:08 PM

View PostTonyRymond, on 26 May 2019 - 03:37 PM, said:

PC know that this address "0x000000" is unique and not deferencing to any value?

It's not unique. And, as I noted before, any address used a null place holder would need be invalid for anything else.

However, as you don't believe me:

Quote

some modern operating systems such as FreeBSD, Linux and Microsoft Windows[2] actually make the zero page inaccessible to trap uses of NULL pointers. This is useful, as NULL pointers are the method used to represent the value of a reference that points to nothing. Since code operating on a reference will be written assuming it that the reference actually refers to some valid structure or value, catching the case where the reference points to nothing is of use since it is a sure sign of a bug.

-- https://en.wikipedia.../wiki/Zero_page

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6929
  • View blog
  • Posts: 23,546
  • Joined: 05-May 12

Re: Null pointer in C/C++

Posted 26 May 2019 - 04:20 PM

Merged duplicate topic.
Was This Post Helpful? 0
  • +
  • -

#11 NicVene   User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 8
  • Joined: 28-May 19

Re: Null pointer in C/C++

Posted 28 May 2019 - 03:18 PM

Tony, I can't tell yet if you're just really trying hard to learn, or if the complaints that come to you in various forums under different names is really deserved. Some just don't have patience, and some don't really deserve it.

That said, I'll assume you're serious about your inquiry.

Factually, there are rare and special contexts in which the memory location 0x0000 (and however many zeros are appropriate) is a valid address to access. That just isn't the case in typical applications. As given by baavgi, there is an arbitrary definition regarding this location (where it generates a fault).

In the now ancient but still relevant design of the IBM PC standard, the location zero in physical RAM was reserved for the first entry in a list of function pointers, identifying what code to run when the processor receives an interrupt signal. It is what you'd recognize as the BIOS interrupt table. Programmers were still prohibited from directly referencing these locations.

A related point to understand is that in modern operating systems, the applications do not actually "see" the physical addresses of memory. If you run two separate debugging sessions of an application and report the memory locations in use, you may notice what seems to be a confusing reality. You may find the two, completely separate processes, appear to reference the same memory locations. What's more, they don't appear to interfere with each other.

This is because the operating system presents applications with memory that is mapped. The memory locations "seen" by an application are not specific physical locations. They are relative to a mapping system the operating system controls, which maps the memory space of a process to the physical resources of memory. As an extension of this mapping, locations like 0x0000 are arbitrarily defined as invalid. Access to such unmapped locations generate exception in the processor, and the operating system deals with them where appropriate (sometimes by sending a kill signal to the application).
Was This Post Helpful? 0
  • +
  • -

#12 Catacombs   User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 44
  • Joined: 24-May 19

Re: Null pointer in C/C++

Posted 28 May 2019 - 03:36 PM

OP sounds like this notorious help vampire on the C/C++ programming board. His or her questions sound awfully familiar.
Was This Post Helpful? 0
  • +
  • -

#13 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1859
  • View blog
  • Posts: 6,697
  • Joined: 19-February 09

Re: Null pointer in C/C++

Posted 28 May 2019 - 07:00 PM

Computer Says No

The PC doesn't know. The null pointer is an invention and convention used by programmers/designers.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1