Thread: any use for 4x pointer or higher?

  1. #1
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594

    any use for 4x pointer or higher?

    out of curiosity, have you found one?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    By 4x pointer, do you mean something like int ****x? If that's what you mean, you should never run into anything more than two in good code.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I've seen some pointers like that in tensor mathematics. For example, in mechanics, a stiffness tensor is 3x3x3x3, so often you want to have four subscripts. Although I personally would not use a 4d array, but rather a 1D array and use operator()(int,int,int,int) to index into it.
    Last edited by Cat; 11-21-2003 at 01:44 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I know its horroble programming style, but I just recently found out that you are able to have up to a 13x pointer (int *************x). I'm just asking around to see if anybody has ever found it practical to take advantage of this, because it is pretty useless.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm just asking around to see if anybody has ever found it practical to take advantage of this, because it is pretty useless.
    I've found use of up to four levels. I can't think of a situation for many more than that though.

    >you should never run into anything more than two in good code.
    I like to believe that I write "good code", yet I've used triple pointers quite a few times in non-trivial data structure code.
    My best code is written with the delete key.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I can't see any situations where two or less pointers couldn't accomplish the same thing. If you can, please show me.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I can't see any situations where two or less pointers couldn't accomplish the same thing.
    Let's say that I need to write a function to dynamically allocate a low level matrix of T. Both x and y need to be arbitrary, but the function is required to return a status instead of the pointer itself to meet consistency requirements for the project. In other words:
    Code:
    if ( matalloc ( &matrix, x, y ) != GOOD ) {
      // Enter error handling code
    }
    Now I have no choice but to perform the allocation on my original pointer. The most natural and clear solution is to pass a pointer to the pointer:
    Code:
    error_code_t matalloc ( T ***matrix, int x, int y )
    {
      // Allocate memory and return status
    }
    With no effort at all I managed to reach three levels of indirection through necessity. Of course, I could use a reference:
    Code:
    error_code_t matalloc ( T **&matrix, int x, int y )
    {
      // Allocate memory and return status
    }
    But tell me honestly whether you knew what the syntax was for that without seeing it or testing it. If you did, can you be sure that everyone else will too? *** is very obvious to anyone who understands pointers, but reference syntax can be tricky. The natural reaction is to do this:
    Code:
    error_code_t matalloc ( T& **matrix, int x, int y )
    You could also wrap the matrix in a structure to be more obvious:
    Code:
    struct matrix_s {
      int **matrix;
    };
    But we still need to use a pointer or a reference on the structure so you gain only extra complexity with the use of an unnecessary struct and member accesses to it. Some might argue that this has the benefit of clarity since instead of *** all we use is *, but others might respond that it's overkill. You could even use a global variable, but that would be a bad idea in general.

    So, while you can think of ways around more than two levels of indirection (I can as well), that isn't the issue. Sometimes we don't have the luxury of doing anything we want due to restrictions of the coding style or project requirements. I don't think that using the best solution within the restrictions given is bad style. If you think otherwise you should consider whether you are being naive.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by XSquared
    I can't see any situations where two or less pointers couldn't accomplish the same thing. If you can, please show me.
    You can ALWAYS use only two or less pointers, but it may not always be the most intuitive. As I said, I've seen tensors with lots of pointers, although I dunno if I like it much like that.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think tensors and matrices and all such data structures should be wrapped in classes anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM