Thread: Sensibility of returning a 2D array from a Class

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    22

    Sensibility of returning a 2D array from a Class

    I'm creating a class to handle coordinates, I want to call a function in it, this function needs to return 5+ sets of coordinates, obviously X and Y, to the program calling it.

    I think I can use a 2D array, but I want to make the program scalable, it already is in terms of the number of sets of coordinates it can return, but is a 2D array going to be good enough for returning say 30 sets of coords?

    Are there any better data structures for this task anyone can think of?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, I would use a
    Code:
    struct coord { sometype x, y };
    rather than a 2d array - unless you are have some particular reason to planning to use it as a 2D array for some reason, rather than as x and y coordinates.

    Second, I would pass a suitable size array into the function (either as a pointer or as a reference), with a size of the array (so that you can confirm that the data fits).

    Returning structs or arrays from a function means that you are copying the data every time you return to the calling function. Passing an array as reference (or pointer to the array) is more efficient, as you are only copying a few bytes, rather than 30 x sizeof(sometype) - which is most likely at least 8 bytes per entry -> 480 bytes.

    Another option is of course to encapsulate the array within the class, and have a member function that returns a const reference (or const pointer) to the array. The advantage here is that you can then change the size of the array as you like, without changing the calling code, just have a function that returns the current size. It also allows you to be flexible about how you store the array internally in the class - if it's fixed size array or if you allocate one as you need (e.g. using a different size for each instance of the class, based on what you need).

    If you need the calling code to update the array (without the overhead of calling a class member), you may implement a simple "lock/unlock" mechanism for the class, where a lock (if successful, e.g. no other lock is in place) returns a pointer to the array that can be modified (during which time no other member functions are allowed to change the array). unlock lets the class know that the array is now available, and can be updated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Arrays are virtually obsolete in C++. Unless you never plan on changing the size of the array, use a std::vector instead.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    22
    Thanks matsp, a structure should do the trick nicely.

    cpjust, i'll think about incorporating a vector into matsp's suggestion. - are arrays really nearly obsolete, surely there is still a time for an array and similarly a suitable time for vectors?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A direct array, or even a pointer, is a more direct approach. It should generate more efficient code than going through an extra layer. But of course, if you don't know how much data you want to store beforehand, arrays are pretty bad, particularly if there are many of them.

    It's all about choices, and understanding what works when, and how.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    22
    I'll have a look at both methods, although it looks like encapsulating the array in the class and returning a const reference or pointer to it might be the best way to go about it.

    In terms of how much data, doing some basic stuff I'm dealing with shapes, three sides, theoretically up to infinity (obviously dependent on the range of the variables). I expect I might be handling a lot of data quite quickly for numerous sided shapes.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Chapter 77. Use vector and string instead of arrays
    An array can be acceptable when its size really is fixed at compile time (e.g., float[3] for a three-dimensional point; switching to four dimensions would be a redesign anyway).


    Chapter 76. Use vector by default. Otherwise, choose an appropriate container
    • Guaranteed to have the lowest space overhead of any container (zero bytes per object).
    • Guaranteed to have inherent locality of reference, meaning that objects near each other in the container are guaranteed to be near each other in memory, which is not guaranteed by any other standard container.
    • Guaranteed to be layout-compatible with C, unlike any other standard container.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    22
    What is your reference cpjust?

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Click on the links.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    22
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning 2D Array
    By Stlcardinal50 in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2009, 02:56 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  4. returning a 2D array in a function
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-05-2002, 08:56 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM