[Lumiera] TimelineZoomScale Question About Pointers

Ichthyostega prg at ichthyostega.de
Sun Oct 9 15:05:57 CEST 2011


Am 09.10.2011 11:03, schrieb Odin:
> So, my_pointer is stored at address 0x999, and it's literal value is: 0xAAA. 
> my_struct is stored at address 0xAAA, and it's literal value is: 0x03 (and
> it's 4 long, so also 'h', 'e', 'y' are stored in 0xAAB, 0xAAC and 0xAAD).

> There might be errors, I'm mostly a javascript/python guy, I only remember
> this stuff from my asm days :-)

oh thanks, that was a nicely written introduction. Maybe we should even
consider to put it somewhere on the website, in the tutorials section?
Since that question regarding pointers pops up regularily....


A little warning though. This example shouldn't lure any newbie into
believing that Strings and text processing work that way in C or C++

It is just in this example, where you probably assumed the following

struct my_struct
  {
    const int length;
    char data[3];

    my_struct()
      : length(3)
      {
        const char *tmp = "hey";
        for (int i=0; i<length; ++i)
          data[i] = *(tmp+i);
      }
  };


see here for the full, working example:
http://git.lumiera.org/gitweb?p=lumiera/ichthyo;a=blob;f=src/tool/try.cpp;hb=d9f64c94bf5353033db6bf588ebe23550941b10b


Another thing I'd like to add is the recommendation, not to engage too much
into thinking in concrete addresses. For this example, this is just fine,
as it helps you understanding what is going on under the hood.

But these concrete addresses, as given by Odin, are correct only under very
specific assumptions. Already if you change the order of the member fields
in my_struct, the addresses will change. That's why the '&' (address-of
operator) is so nice: it allows you to take an address without the need
of knowing / hard coding it explicitly. It's always better not to
assume anything about the concrete layout of objects in memory
and to just leave that to the compiler. Thinking this way
makes your code less fragile.

Cheers,
Hermann



More information about the Lumiera mailing list