Thread: Returning a Dynamic array of pointers

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    11

    Returning a Dynamic array of pointers

    I'm having trouble picturing what this looks like in memory which is one of problems. Maybe someone can help me out.

    Code:
    #include <iostream> 
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string>
    
    using namespace std;
    
    
    typedef struct {
            char* name;   
    	int number;
        } SomeStruct;
    
    //function prototypes
      char * getbuf(SomeStruct ***ptrs);
      SomeStruct * GetNextSomeStruct(void);
    
    
     
    
    
     int main()
     {
    	 
    
    	// array of pointers to SomeStruct
    	 SomeStruct **array_ptrs = NULL;
     
    	  
     	char * b, q, *r;	   
    	
    	  
    	b=getbuf(&array_ptrs);	
            //I want to do something like this 
             //to fill the array_ptrs with data but I keep getting errors
    	//array_ptrs[2][1] = GetNextSomeStruct();  
           //I tried to test it like this but it crashes here too 
    array_ptrs[2][1].number = 8;    
                                                          
    	 
      
    	  q = *b;	  
    	
     	 
     
    
     		 
     }
    char * getbuf(SomeStruct ***ptrs)	   
     	{	  
    		 
    	 
    		char buff[8] ;	   
     	 	
    		//int n = 5; //can also be determined by user at runtime
    		 
     
      //   allocate the memory for the array  
    
          
    	   
       int m;
    	*ptrs = (SomeStruct**)malloc(  sizeof( SomeStruct* )* n );
       for (m = 0; m < 1; m++)
        {
         (*ptrs)[m] = (SomeStruct*)malloc( sizeof( SomeStruct ) * n );
         }
       
       
    
     
     
    	return  (char *)  buff;	   
     	}
    
    
     
    
    
    
        // Return the next structure (to fill the 
         //                          abovementioned array with).
         
          
        SomeSeq * GetNextSomeStruct()
    	  {
                    //I don't really know what I should do here
                    //create some data and then return it to 
                    //array_ptrs in main.
    		SomeStruct *p=NULL;
    		 
    		 
    		p  = (SomeStruct*)malloc( sizeof( SomeStruct* ) );
    		  
    
    			
    		return p ;
    	  }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Why don't you start by properly indenting and whitespacing your code. What you show here is NOT legible, which may be part of your confusion.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Is this better?
    Code:
    #include <iostream> 
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string>
    
    using namespace std;
    
    
    typedef struct {
            char* name;   
    	int number;
        } SomeStruct;
    
    //function prototypes
    char * getbuf(SomeStruct ***ptrs);
    SomeStruct * GetNextSomeStruct(void);
    
    
     int main()
     {
    	 // array of pointers to SomeStruct
    	 SomeStruct **array_ptrs = NULL;
     
    	 char * b, q, *r;	   
    	
    	  
    	b=getbuf(&array_ptrs);	
            //I want to do something like this 
            //to fill the array_ptrs with data but I keep getting errors
    	//array_ptrs[2][1] = GetNextSomeStruct();  
           	//I tried to test it like this but it crashes here too 
    	array_ptrs[2][1].number = 8;    
                                                          
    	q = *b;	  
    	
     	 
    }
    
    
    char * getbuf(SomeStruct ***ptrs)	   
    {	  
    		 
    	 char buff[8] ;	   
     	 int n = 5; //can also be determined by user at runtime
    		 
     
      	//allocate the memory for the array  
    	int m;
    	*ptrs = (SomeStruct**)malloc(  sizeof( SomeStruct* )* n );
       		for (m = 0; m < 1; m++)
        			{
         				(*ptrs)[m] = (SomeStruct*)malloc( sizeof( SomeStruct ) * n );
         			}
       
       	return  (char *)  buff;	   
     }
    
    
     
    
    
    
    // Return the next structure (to fill the 
    // abovementioned array with).
         
          
    SomeSeq * GetNextSomeStruct()
    {
         //I don't really know what I should do here
         //create some data and then return it to 
         //array_ptrs in main.
    	SomeStruct *p=NULL;
    		 
    		 
    	p  = (SomeStruct*)malloc( sizeof( SomeStruct* ) );
    	// fill this with data and return
    
    			
    	return p ;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That indentation isn't completely awful, thanks for that.

    So what do you think this does:
    Code:
    char * getbuf(SomeStruct ***ptrs)	   
    {	  
    		 
    	 char buff[8] ;	   
     	 int n = 5; //can also be determined by user at runtime
    		 
     
      	//allocate the memory for the array  
    	int m;
    	*ptrs = (SomeStruct**)malloc(  sizeof( SomeStruct* )* n );
       		for (m = 0; m < 1; m++)
        			{
         				(*ptrs)[m] = (SomeStruct*)malloc( sizeof( SomeStruct ) * n );
         			}
       
       	return  (char *)  buff;	   
     }
    and why do you think it has any relation to making an m x n array? And why are you returning an invalid pointer to a local object?

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    I guess thats my problem, thats the best that I can come up for making an array of pointers , it seems to work to some extent but I am lost on some it. As far as the return char buff, thats the instructions I have. The function must return that but also return an array of pointers to SomeStruct's.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The function can not return two things. Either your instructions are impossible to achieve, or you are misreading them. (Since buff does not appear anywhere, or do anything, it's existence is extremely hard to justify.)

    If you wanted to make an array of pointers, you would not need a *** variable. You're also not even trying to make an array of pointers; you appear to be trying to make a dynamic two-dimensional array. If so, then I suggest you think about what the word two-dimensional means, and what the two dimensions of your array are supposed to be.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Here is an array of pointers
    Somestruct *arrayOFpointers[10]; //Not dynamic
    So I did this:
    Somestruct **arrayOFpointers; //is this a dynamic array of pointers? I don't know.
    What I mean by returning this is gaining access to the array without actually using the return keyword.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by smhillis
    So I did this:
    Somestruct **arrayOFpointers; //is this a dynamic array of pointers?
    No, it declares a pointer to a pointer. However, this pointer could point to the first pointer in a dynamically allocated array of pointers, in which case it would effectively be a dynamic array of pointers.

    Quote Originally Posted by smhillis
    What I mean by returning this is gaining access to the array without actually using the return keyword.
    I do not understand what you are trying to say as it sounds contradictory.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    I mean passing in by reference and then changing it, as I did in the code. That way I can effectively "return" the array. As far as the way I allocated the memory for the array of pointers that is correct isn't it?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by smhillis
    I mean passing in by reference and then changing it, as I did in the code. That way I can effectively "return" the array.
    Ah yes, an out (or in/out) parameter. You could actually do the same for buff and thus avoid the problem with returning a pointer to a local variable, as tabstop commented on. However, it is not clear to me why you need buff in the first place.

    Quote Originally Posted by smhillis
    As far as the way I allocated the memory for the array of pointers that is correct isn't it?
    It looks correct, except for the lack of error checking since malloc could return a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    I know the program doesn't make it sense. It an assessement of my skills to see if I can do it, I am free to ask for help however. If I allocated it correctly then why am I unable to access the array back in main? Like this:
    array_ptrs[2][3].number = 5;

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by smhillis
    If I allocated it correctly then why am I unable to access the array back in main?
    Oh, I did not notice a mistake: the loop in getbuf loops from 0 to 1-1 rather than 0 to n-1.

    By the way, I just noticed these includes:
    Code:
    #include <iostream>
    #include <string>
    and this using directive:
    Code:
    using namespace std;
    Is this program supposed to be written in C or C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    C only.
    Thanks for that, I should have caught that error. So now I can access the array in main. I need to get data from the user into it using GetNextSomeSeq(). Any suggestions?
    Last edited by smhillis; 08-01-2009 at 11:44 PM.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would recommend that you pass in an array of SomeSeq stuff and then do input on that.

    If you arrange for values in your main data array that stand for an empty or unused space, you could have the function search for an empty space and put the valid input there. I don't really know what these values should be, but for example, "" for name and -1 for number.

    On what you currently have for this: while it's probably not a big deal, I don't see the point in allocating more memory in your function and then waiting to do the assignment to your actual array. I think as written it's leaking memory anyway.

  15. #15
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Yeah I agree it is overkill, I just couldn't think of any other way. I try some of your suggestions. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  2. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  5. dynamic array of base class pointers
    By Corrington_j in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 05:58 AM