Thread: storage class, arrays, functions and good layout

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    41

    storage class, arrays, functions and good layout

    Code:
    void mycreatedynamicstorage(int **MyArray)
    {
    
    
    by malloc -allocate some 2D storage for the variable above.
    
    
    }
    
    void populatestorage(int **MyArray)
    {
    
    Simply populate the sample.
    
    
    }
    
    int main(void)
    {
    
    int **MyArray;
    mycreatedynamicstorage(MyArray);
    populatestorage(MyArray);
    
    
    }
    My question comesdown to storage class. I have following set-up. The create routine works as it should and creates an array,

    but when it is sent to the next function it seems that I get a segmentaion fault and it would appear the array has gone

    missing. Should I use a storage class for this. Now these routines are tested and work if I dont not explicity call the arrays

    in the function and place the int **MyArray out of the main and into global. Please help if you can, I would send the code, but

    I amworking on Linux without a connection so have had to type in the salient points. Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    void mycreatedynamicstorage(int **MyArray)
    I'd expect that the double pointer to be returned from this function instead -- otherwise I'd expect that you pass a pointer to a pointer to a pointer to an int.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just follow malloc's calling convention and do

    myArray = mycreatedynamicstorage();

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    41
    what do you mean malloc naming convention.
    these are heavily simplified functions, i need to return more than just one array. how do you suggest I do this???

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like malloc is
    p = malloc(size);

    Not
    malloc(p,size); // this CANNOT change p

    Because it has to return a value to the calling function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM