CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340

    Obscure code help

    Came across the following:

    typedef double (*COMPLEX_ARRAY)[2];

    What does this do??

    Thanks for any help you might give me,

    Pankaj

  2. #2
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    pointer to double array

    Evidently the type define of COMPLEX_ARRAY is a pointer to an array of two doubles. The example below should clear it up somewhat.

    Chris.



    Code:
    #include <iostream>
    #include <iomanip>
    
    typedef double (*COMPLEX_ARRAY)[2];
    
    int main(int argc, char* argv[])
    {
      double d[2] = { 1.0, 2.0 };
    
      COMPLEX_ARRAY ca = &d;
    
      std::cout.precision(15);
    
      std::cout << std::fixed << (*ca)[0] << std::endl;
      std::cout << std::fixed << (*ca)[1] << std::endl;
    
      return 1;
    }
    You're gonna go blind staring into that box all day.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I sincerely hope that that typedef comes from a C source file and not a C++ source file.....

    Are you sure, dude? I had thought that, given the name, it's supposed to represent what you said: an array of pairs of doubles. But then I had to ask myself whether the parentheses change that? Does it actually declare a two-element array of pointers to double?

    The point I'm trying to make is that, even for experienced programmers, you have to think really hard about whether it actually does what it appears to say. The author of that line should be hauled up in front of the whole workforce and publically humiliated for perpetrating an obfuscation. As an absolute minimum, the code ought to be:
    Code:
    typedef double COMPLEX[2];
    typedef COMPLEX *COMPLEX_ARRAY;
    A simple change and it's immediately obvious whether or not it does what it says on the tin.

    Of course if we're talking C++ here, the offending coder needs shooting.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340
    You would not believe it, the line comes from a C++ source file. Ex-C coders, I guess. It is crazy maintaining all this code though. Thank you so much guys for the explanations Really appreciate it.

    Xargon

  5. #5
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340
    Hi,

    If I change the typedef to

    typedef double COMPLEX[2];
    typedef COMPLEX *COMPLEX_ARRAY;

    How would I go about initializing a COMPLEX_ARRAY. Here is what I do:

    COMPLEX_ARRAY comp = new double[2 * length];

    But when I do this, the compiler gices errors:

    for (size_t i = 0; i < length; i++)
    {
    comp[i][0] = someVar;
    comp[i][1] = 0;
    }

    Compiler throws the following errors:

    error C2109: subscript requires array or pointer type
    C2106: '=' : left operand must be l-value

    Any idea on what I might be doing wrong?

    Thanks,

    Xargon

  6. #6
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340

    Red face

    Never mind the previous post. I am STUPID!

    Xargon

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Given that this is C++, the best definition would be:
    Code:
    typedef std::vector<std::complex> complex_array;
    Do it the C++ way, not as "C with objects".
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340
    I know!

    However, this is for code maintainance. We are taking small steps at a time. I would like to see everything converted to STL containers and objects in the next few months. I hate going through C code. It is really unnerving.

    Thanks for your reply

    Xargon

  9. #9
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    having a bit of a complex...

    and I mean a mental complex.

    Indeed, I was a bit unsure of this typedef.

    Graham, I think your two typedef's should do the trick. Upon reading the post, it seems like the original code authors wanted to define some type for an array of complex numbers. It looks like it should be possible to maintain the original C-style character of the legacy code using the typedef's suggested by Graham.

    This has been worked into a second example below.

    Chris.



    Code:
    #include <iostream>
    #include <iomanip>
    
    typedef double   COMPLEX[2];
    typedef COMPLEX* COMPLEX_ARRAY;
    
    int main(int argc, char* argv[])
    {
      COMPLEX_ARRAY ca = new COMPLEX[10];
    
      int i;
      for(i = 0; i < 10; i++)
      {
        ca[i][0] = static_cast<double>(i);
        ca[i][1] = 1.5 * i;
      }
    
      for(i = 0; i < 10; i++)
      {
        std::cout << std::fixed
                  << std::setprecision(15)
                  << ca[i][0]
                  << ", "
                  << ca[i][1]
                  << std::endl;
      }
    
      delete [] ca;
    
      return 1;
    }
    You're gonna go blind staring into that box all day.

  10. #10
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340
    Yeah, I got it to work with Graham's typedef Kudos to him.

    The sad part is that it is part of a C++ file
    I cannot believe people still code like that.

    Xargon

  11. #11
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340
    Check out the memory allocation in the old code:

    COMPLEX_ARRAY someArr = NULL;

    someArr = new double[2 * length];

    where it could be someArr = new COMPLEX[length] as you guys pointed out.
    2 for two doubles for the array. Makes the whole thing soooooo confusing! So, you have "magical" numbers all over the place.

    Xargon.

  12. #12
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    xargon: I sympathise with your position. I look back on the amount of dreadful code I've had to maintain and think about the frustration of not being able to just do it properly.

    The key thing to learn here, I think, is the importance of clarity, even if that means more typing (longer identifiers that explain what the object is, two simple typedefs rather than one obscure one).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  13. #13
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340
    Yeah, I guess it is the problem with discipline and habit. I am assuming the previous guy was a hard-core C programmer and just used C style code wrapped inside classes.

    Anyways, we have a new project starting soon and I will get a chance to work on something new. Really looking forward to that.

    Xargon

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by xargon
    Yeah, I guess it is the problem with discipline and habit. I am assuming the previous guy was a hard-core C programmer and just used C style code wrapped inside classes.
    Ugh.

    In many organizations, this would lead to a reprimand by the project leader, and in the extreme case, termination of employment.

    Personally, I would have a fit if he/she had <complex> available and proceeded to code their own complex number library. Hopefully the person was willing to learn proper C++ techniques later on. However, there are some who never want to learn proper C++ techniques and will take the path of least resistance, and that is to resort to what they feel comfortable with -- 'C' style coding. Many times they do this without knowing that the code they are writing is not necessary, hard(er) to maintain, and in a lot of cases, just plain old invalid i.e. not guaranteed to work with respect to the rules of ANSI C++.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Aug 2002
    Location
    Germany
    Posts
    340
    Yup Paul, you got it. Well, the guy is not working here anymore. I am guessing they got fed up with him after complaints from fellow programmers.

    Xargon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured