Thread: New Guys lost on Pg 154 jumping into C++

  1. #1
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61

    New Guys lost on Pg 154 jumping into C++

    Here we go again! On the bottom of Pg 154, Jumping into C++ it gets into pointers and 2D arrays. I understand that a pointer is set to the beginning address of every array, and that 2D arrays are actually laid out in a linear fashion in C++. So a array [4][4] in linear fashion is actually 16 addresses long. And in order to get the value at each address you have to dereference it. But in the middle of the paragraph is added another array [3][2] and I can't figure out what this has to do with the blue color in the linear layout that is shown there.

    Can someone please explain this?

    If it has some thing to do with pointer arithmetic (pa) its really confusing. Don't you just multiply, divide, add, and subtract when using (pa)? Yes, I'm referring to addresses here not values! Has anyone found a good tutorial on the I'net that gets into (pa) in depth?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Though the author of the book may own the forum, all the members do not have this book, so please post the relevant material if you can. All of us really cannot explain what's wrong without understanding the context.
    One thing I can point out is that (C) arrays and pointers are separate things. A pointer is not the beginning address of an array. It is true that you can point a pointer to the beginning of an array, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    Okay, I've pasted part of the page I can't make sense of below. In the book each set of (four)[][][][] are colored with the colors I marked next to them. In the linear layout the each set (four) [][][][] are grouped with the colors one after the other as I marked at the end of that line. I hope this make sense.


    What's happening is that only certain sizes are needed to do the correct pointer arithmetic! Two-dimensional arrays are actually laid out flat in memory—the compiler lets you, the programmer, treat it as a square block of memory, but it's really just a linear collection of addresses. The way the compiler does this is by transforming an array access, like array[ 3 ][ 2 ] into a position in memory. Here's an easy way to think about it. If you visualize a 4x4 array like this:
    [][][][] colored majenta
    [][][][] colored yellow
    [][][][] colored green
    [][][][] colored blue
    The array is actually laid out in memory like this: (in linear fashion)
    [][][][][][][][][][][][][][][][] colors here are magenta, yellow, green, blue ([][][][] four for each color)
    In order to use array[ 3 ][ 2 ] (which is located in the blue section), the compiler needs to access memory three rows down (past the magenta, yellow and green rows) and two columns across. To go three rows down with each row being four integers wide, we have to go 4 * 3 integer slots, and then add 2 more integer slots (to get to the 3rd element in the last row).
    In other words, array[ 3 ][ 2 ] turns into this pointer arithmetic:
    *(array + 3 * <width of array> + 2)
    Now you can see that we need the width of the array—without it, the math won't work. And the second dimension of a two-dimensional array is the width. You can't do the same thing with the height of the array because of how the data is physically laid out in memory. (The height would be needed if the array were laid out in the other direction, by row). Because of this, you can actually take as a function argument an array with a variable length for the height of the array, but the second dimension must always be fully specified. In fact, for any multidimensional array, you must specify the sizes for all dimensions except the height. You can think of a single dimensional array as just a special case of an array that only has a height.
    Unfortunately, because a hard-coded width is required when declaring a two-dimensional array, dynamically allocating a two-dimensional array with an arbitrary width requires one more feature of C++, pointers to pointers.

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    few short points:
    1)the name of the array becomes the pointer pointing to the first element .
    2) you can add and subtract memory addresses , but you can't multiply or divide them. this said, playing with memory address aside from studying is usually a bad idea. I can see some cases where it is somewhat useful in C , C++ makes the use of raw pointer-arithmetic unnecessary .
    3) the compiler turns the expression arr[n] to *(arr+n) at compile time, maybe this will help you understand more

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point the book is trying to make is that array is the name of the array and array[3][2] is trying to access the element at row 4 and column 3. The book is simply trying to say that this array[3][2] accesses the blue row in the "4x4" array that they've drawn, hence the connection between array[3][2] and the blue row.

    What the book is trying to say is that you can visualize a 4x4 array as 4 arrays of size 4. If you draw one array of width 4, you get [][][][]. Repeat that 4 times and you get

    [][][][]
    [][][][]
    [][][][]
    [][][][]

    So you could say that array[3][2] means access the 4th array (hence the row with blue color) and the 3rd element in that array. Since arrays are laid out contiguously in memory, you can actually see it as one big array of 16 elements (4 elements for 4 arrays). If you look at the drawing, you can see that array[3][2] refers to the 15th element, that is, the element with index 14. The compiler treats 2D arrays as flat arrays (i.e. 1D arrays). By using simple arithmetic, it can deduce the element in the flat array.

    Assume the array index 0 is stored at memory location X in memory. Then array[0][0] would read and write to address X. Now assume that each element is some constant size k. For example, ints are typically 4 bytes, so each element of an int array would be 4 bytes, i.e. k = 4. So that means the first int would be stored at location X, X + 1, X + 2, X + 3 (because each byte has an address in memory). That means the second element starts at X + 4, the third element at X + 8, and so on. Generically speaking, we can conclude that element N should be positioned at X + k * N (where N is 0-indexed). For example, 3rd element = X + 4 * 2 = X + 8.

    Until now we've just assumed the element is stored at row 0 (i.e. the first row, like array[0][2]). But what if it's stored on another row? Well, if it's on the second row, we need to add 4 elements (the width of the array as can be seen in the drawing). So the formula becomes X + k * w * r + k * N where r is the row (0-indexed) and w is the width of the array in number of elements (in this case, 4 elements; w is also not 0-indexed).

    Now assume that k = 1 (i.e. a char). Then the formula becomes X + w * r + N. Let's take array[3][2] as an example again from the example above. Putting the values into the formula, we get:

    X + 4 * 3 + 2
    Compare that to:
    array + 3 * <width of array> + 2

    If X = array, we swap the operands of the multiplication and replace "4" (our width) with "<width of array>", we get:

    array + 4 * 3 + 2 (replaced X with array)
    array + 3 * 4 + 2 (swapped arguments of multiplication)
    array + 3 * <width of array> + 2 (replaced size "4" with <width of array>)

    Compare to:

    array + 3 * <width of array> + 2

    They are identical!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    Thanks Elysia, great job of explaining this.
    I made the big mistake of thinking he was discussing two different arrays (array [3][2] and array [4][4]) he was not. Thank you for telling me to post the content from the book, otherwise I would be still scratching my head in wonderment. There are 2 mistakes in the book though, see three row down and two column over in the snippet below.
    In order to use
    array[ 3 ][ 2 ] (which is located in the blue section), the compiler needs to access memory three rows down (past the magenta, yellow and green rows) and two columns across. To go three rows down with each row being four integers wide, we have to go 4 * 3 integer slots, and then add
    Last edited by papagym177; 11-05-2014 at 11:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping into c++ help
    By Krunchee in forum C++ Programming
    Replies: 3
    Last Post: 08-03-2013, 10:37 PM
  2. after 'jumping to c++'
    By nish1013 in forum C++ Programming
    Replies: 2
    Last Post: 07-09-2013, 08:26 AM
  3. Jumping into C++
    By Tamim Ad Dari in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2013, 08:29 PM
  4. Jumping into C++
    By Tamim Ad Dari in forum C++ Programming
    Replies: 9
    Last Post: 01-11-2013, 09:45 AM
  5. Jumping to OOP in C++ from C
    By meadhikari in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2010, 05:26 AM