0

Inside a function, I make a 2d array that fills itself from a text file and needs to get returned to main. The array stays a constant size through the whole program.

I know this is something that gets asked a lot, but I always seem to get one of two answers:

Use std::vector or std::array or some other STD function. I don't really understand how these work, is there any site actually explaining them and how they act compared to normal arrays? Are there any special #includes that I need?

Or

Use a pointer to the array, and return the pointer. First, on some of the answers to this it apparently doesn't work because of local arrays. How do I tell when it does and doesn't work? How do I use this array back in the main function?

I'm having more trouble with the concept of pointers and std::things than with the actual code, so if there's a website you know explains it particularly well, feel free to just put that.

JTTCOTE
  • 1
  • 1
  • 2
  • Even if you don't know the number of rows, do you know the number of columns? – jxh Aug 27 '13 at 01:22
  • "Normal" arrays are actually the misbehaving ones. Passing and returning a `std::string` or `std::vector` is pretty much the same as passing a simple `float`. – MSalters Aug 30 '13 at 14:51
  • I trust that you already have looked at c++ books (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) regarding pointers, so I'll throw in a link to a C lecture from R. Buckland (caution: C and C++ are different languages, sometimes the difference is very subtle, but the concept of pointers works the same way): http://www.youtube.com/watch?v=Rxvv9krECNw. The iterators of the standard containters behave like pointers would and the containers behave a bit like arrays, but the container takes care of memory management and bounds checking. – mars Aug 30 '13 at 15:11

3 Answers3

3

Not necessarily the best solution, but the easiest way to get it working with vectors. The advantages are that you don't need to delete memory (happens automatically) and the array is bounds-checked in debug mode on most compilers.

#include <vector>
#include <iostream>

using array2D = std::vector< std::vector< int > >;

array2D MyFunc(int x_size, int y_size)
{
    array2D array(y_size, vector< int >(x_size));

    int i = 0;
    for (int y = 0; y < array.size(); y++)
    {
        for (int x = 0; x < array[y].size(); x++)
        {
            // note the order of the index
            array[y][x] = i++;
        }
    }

    return array;
}

int main()
{
    array2D bob = MyFunc(10, 5);
    for (int y = 0; y < bob.size(); y++)
    {
        for (int x = 0; x < bob[y].size(); x++)
        {
            cout << bob[y][x] << "\n";
        }
    }
}

Live example: http://ideone.com/K4ilfX

MSalters
  • 173,980
  • 10
  • 155
  • 350
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • Hi, I tried this and it has a small problem. That all works fine, but I need "using namespace std;" as it pretty much explodes without it - I can't use string, cout, or cin. How do I have two using statements, or be able to use those methods? (I have #include and #include , yes) – JTTCOTE Sep 04 '13 at 08:44
  • @JTTCOTE add std:: in front of cout, string, etc. or put using namespace std after your header includes. – Neil Kirk Sep 04 '13 at 11:08
  • Is there something I'm missing? That doesn't build on VS express, with tons of errors all related to it not making the array. – JTTCOTE Sep 04 '13 at 23:42
  • @JTTCOTE Someone edited my post for some reason.. The original code is in the live example link. Does that work? – Neil Kirk Sep 05 '13 at 04:13
  • Yes, that does. I guess someone tried to replace the long return type of MyFunc with a shorter thing with that "using" part, and it didn't work. Thanks! – JTTCOTE Sep 05 '13 at 07:06
  • @JTTCOTE You could use a typedef. I've never used this "using" like that before. – Neil Kirk Sep 05 '13 at 10:52
1

Sounds like you are new to C++. If this is indeed the case, I would suggest using arrays for now because you probably won't be using any of the stuff that STL containers give you. Now, let's talk about pointers.

You are correct that if you declare a local array in your function, the main function won't have access to it. However, this is not the case if you dynamically allocate the array using the new keyword. When you use new to allocate your array, you essentially tell the compiler to reserve a chunk of memory for your program. You can then access it using a pointer, which is really just the address of that chunk of memory you reserved. Therefore, instead of passing the entire array to the main function, all you need to do is pass a pointer (address) to that array.

Here are some relevant explanations. I will add to them as I find more:

Lawrence H
  • 467
  • 4
  • 10
  • If I get it right: Return some new allocated - no good. Use some shared/unique pointer instead, –  Aug 27 '13 at 00:39
0

The easiest way to create a 2d array is as follows:

char (*array)[10];
array = new array[5][10];

Two dimensional arrays can be tricky to declare. The parenthesis above in the variable declaration are important to tell the compiler array is a pointer to an array of 10 characters.

It is really essential to understand pointers with C and C++ unless using the std:: collections. Even then, pointers are widely prevalent, and incorrect use can be devastating to a program.

ash
  • 4,867
  • 1
  • 23
  • 33