Thread: Dynmic 2d array, Problem with returning it from function

  1. #1
    Wen Resu
    Join Date
    May 2003
    Posts
    219

    Dynmic 2d array, Problem with returning it from function

    Hello,
    My program creates a two dimensional array and populates it dynamicly. Following a thread i found with search, i did it by creating the dynamic array and then filling that array with arrays.
    That much works fine. My problem lies in the fact that i need to return this array fro mthe function that creates it to the main part of the program.
    Function returns array as char**. I would like to put this into a global variable. Now i can't pre allocate the size of the global i want to store it in, as i dont know size of array till its amde.
    I am considering makign the array static within the function and then simply using pointers to access it as needed during my program, but this seems kinda, sloppy.
    The question at hand is, how can i properly return my array from my function, and how can i set up the Global variable to recieve it.
    Thanks. i can include code if you need but dont think code would do much good as it would "seem" to be a kind of universal syntax thing.
    I shall continue my search and Post if i solve this myself
    Thanks

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Just a noob but it sounds like you are going about things correctly by declaring it static. I don't know of any other way than this. You may want to post some of your 'sloppy' code and maybe someone could suggest ways of making it pretty.

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    I though i that i shuld be able to return with a pointer** to the array and then dereference it into an array, but again syntax eludes me. I'll psot some code when i neaten it up :P

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I used something like this in my TGA file loader (which, by the way, is working correctly now ) But it was probably bad design. Consider the following code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    
    int** some ;
    
    int** somefunc()
    {
    some = new int*[10];
    
    for (int i=0;i<10;i++)
      some[i] = new int[10];
      for (int x=0;x<10;x++)
       for(int y=0;y<10;y++)
        some[x][y] = x;
    return some;
    
    }
    
    int main()
    {
    int x;
    int ** temp2 = somefunc();
    temp2[0][0] = 100;
    cout<<some[0][0];
    cin>>x;
    
    }
    Basically, returning a pointer to a two-dimensional array could be a problem, because it allows access to private data. However, if you want to do this, you don't have to allocate the array that is going to be assigned to the returned array.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Thanks. trying it out now. Code cleared up my main ponderations :P
    Shuld get code workign soon from look of things
    BTW what i am doing is basic path findign threw a maze.
    Last edited by Iamien; 10-15-2003 at 10:13 PM.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    There are several ways to approach the problem you had, and I'd be interested to see what code you ended up using for your particular problem if you're willing to post it.

  7. #7
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    sory for delayed responce. My solution was simple nuff.
    i made a global array
    char** loc;

    passed it to my function, then i just allocated space to it and accessed char as needed. Proabably not bestway, but it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM