A question about pointers.

Or perhaps more a question about memory.

Page 1 of 1

7 Replies - 1107 Views - Last Post: 12 March 2009 - 03:55 AM Rate Topic: -----

#1 erewnoh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 01-March 09

A question about pointers.

Posted 10 March 2009 - 10:19 PM

EDIT -- New question about pointers further on down a few posts. I thought it'd be more appropriate to add onto this as it's another question about pointers.

I'm sorry if this has been answered elsewhere, but I couldn't find it easily so figured I'd ask to see if I could get any deeper insight into it rather than a textbook explanation.

I'm now to a part in my book where I'm going to be implementing my own vector class. I know the STL has one, but the book insists on doing it to explore the (almost) extreme low levels of the language, as well as using it for an introduction to pointers. Ok, so here's my question.
int x = 5; //arbitrary value of 5
int* xPtr = &x; // so now I have xPtr of type int* pointing to the memory address of x

// Here's my question though. In the book when it was explaining why you can't switch
// pointer types without explicitly doing so, it used an example like this (the comments are mine)

cout << "The size of char is " << sizeof(char) << " " << sizeof('a') << endl; // ok, I can understand this
cout << "The size of int is " << sizeof(int) << " " << sizeof(2+2) << endl; // and this
int* p = 0; // ?? Is it referring to the very first part in my computer's memory? I've no idea
cout << "The size of int* is " << sizeof(int*) << " " << sizeof(p) << endl; // same size as an int



What in the heck is int* p = 0 pointing to? EDIT -- Ok, figured that one out, silly book.

Also, the sizes it returns for a char or bool is 1, int and long is 4, and double is 8. Is that the same as saying "a char or boolean refers to 1 4 bit chunk of memory" and so on? And that unfortunately leads my mind to another topic. If my computer is a 64 bit architecture with a 64 bit OS, how come an int can't hold a value in the range of -2^64 : 2^64 - 1?

Sorry if that's a lot of rambling.

This post has been edited by erewnoh: 11 March 2009 - 07:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: A question about pointers.

#2 webboy42  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-August 08

Re: A question about pointers.

Posted 10 March 2009 - 10:27 PM

int* p = 0;

In all cases I have seen, 0 is used as a null pointer.
Was This Post Helpful? 0
  • +
  • -

#3 erewnoh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 01-March 09

Re: A question about pointers.

Posted 10 March 2009 - 10:33 PM

Yep, a few pages later in the book it explained just that. Why they didn't explain it when they used it is beyond me.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4359
  • View blog
  • Posts: 12,176
  • Joined: 18-April 07

Re: A question about pointers.

Posted 10 March 2009 - 10:36 PM

int *p = 0; for all intents and purposes is the same as assigning NULL to the pointer. It "nulls or zeros" it out to nothing.

As for pointer sizes, when bool or char is referred to 1 it is talking about "bytes" that is, bool or chars are 1 byte long in that it stores its value as an 8 bit chunk. Ints and longs are 4 bytes, that is a 32 bit chunk. So the largest value they can hold is when all 32 bits are 2^32 - 1.

As for a 64bit system that is referring to the addressable memory and its registers, not the actual data width of the data types. ints are 32 bit on both 32 bit and 64bit systems, while things that address memory (such as pointers) can point to memory addresses above the 32 bit range.

Hopefully that answers everything. :)
Was This Post Helpful? 1
  • +
  • -

#5 erewnoh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 01-March 09

Re: A question about pointers.

Posted 10 March 2009 - 10:48 PM

It does, thank you for the quick answer.
Was This Post Helpful? 0
  • +
  • -

#6 erewnoh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 01-March 09

Re: A question about pointers.

Posted 11 March 2009 - 07:45 PM

Sorry to be a bother, but I've a new question. I'm self teaching, so I want to make sure I understand this as it seems like it might be mildly important. In my book it has the following code.

struct Link
{
	string value;
	Link* pred;
	Link* succ; 
	Link(const string& v, Link* p = 0, Link* s = 0)
		: value(v), pred(p), succ(s){}
};

Link* norse_gods = new Link("Thor",0,0); // 1
norse_gods = new Link("Odin",norse_gods,0); // 2
norse_gods->succ->pred = norse_gods; // 3
norse_gods = new Link("Freia",norse_gods,0); // 4
norse_gods->succ->pred = norse_gods; // 5


succ and pred stand for successor and predecessor.

Ok, I *think I understand what is going on here but something about it seems fishy to me. On line 1, I've created a pointer called norse_gods and assigned it a Link struct called Thor, and the pred and succ pointers are null?

On line 2, I'm creating a new Link object, calling it "Odin", and saying that Thor is it's pred, and succ is still null? This seems wrong to me.

On line 3, I'm saying Odin's successor's (Thor?? magic involved possibly?) predecessor is Odin? Which sets the pred pointer within the Thor object to point to Odin? Shouldn't Odin's succ pointer be null?

On line 4, basically the same thing as line 2. And line 5, same as line 3.

I'm a bit lost. It seems to me I should be calling Odin like
 new Link("Odin",0,norse_gods);
to assign Thor as Odin's successor. Or am I missing something fundamental? Currently I can interpret it so it works and also so it doesn't, which is a pretty unique feeling.

This post has been edited by erewnoh: 11 March 2009 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

#7 erewnoh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 01-March 09

Re: A question about pointers.

Posted 11 March 2009 - 09:33 PM

Ok, answered the question myself. After testing it and changing it around it looks like my suspicions were right. Stroustrup used this code to demonstrate a few topics on pointers, and made a mistake in typing. A quote from his website: "The code as written on page 599 will crash. Actually, the code posted on www.stroustrup.com/Programming says it will crash, but the text does not. This became a far better than intended example of why I (as stated) dislike this kind of code. Oops!"
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Karpov2007*


Reputation:

Re: A question about pointers.

Posted 12 March 2009 - 03:55 AM

View Posterewnoh, on 10 Mar, 2009 - 09:19 PM, said:

If my computer is a 64 bit architecture with a 64 bit OS, how come an int can't hold a value in the range of -2^64 : 2^64 - 1?

It depends on used data model.
Was This Post Helpful? 0

Page 1 of 1