Thread: discussion for C++ double pointers

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    23

    discussion for C++ double pointers

    I have referred to FAQ regarding C++ double pointers and I have written a small code to verify. Can someone have a look and let me know if I am freeing the memory correctly.

    I have not put any checks for NULL in case memory is not allocated for now if someone can suggest (if possible with example for double pointers) what would be the best way. Initially I thought something like this

    Code:
    if(XXXXX ==NULL)
    {cout<<"Error allocating memory";
    exit(0);
    }
    However, I remember using something better like cerror or cerr (I don't remember). If anyone could throw some light on it. Thanks

    Also i am thinking of requesting the moderator to put the freeing of memory in FAQ in the section addressing double pointers in C++ FAQ. Let me know if you think it's a good idea.


    This is the code for putting elements in a 2d matrix

    Code:
    #include <iostream.h>
    
    void main (void)
    {
    	
    	int **matrix,rows,columns,i,j;
    	cout<<"Enter the columns";
    	cin>>columns;
    	cout<<"Enter the no. of rows";
    	cin>>rows;
    	matrix = new int*[rows];
    	
    	for(i=0;i<rows;i++)
    	{
    		matrix[i] = new int[columns];
    	}
    	for(i=0;i<rows;i++)
    	{
    		for(j=0;j<columns;j++)
    		{
    			matrix[i][j]=j+i*10;
    			cout<<"matrix"<<"["<<i<<"]["<<j<<"]="<<matrix[i][j]<<"\t";
    		}
    	}
    
    	for(i=0;i<rows;i++)
    	{
    		delete[] matrix[i];
    	}
    	delete[] matrix;
    
    	cout<<endl;
    }

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    So, you've read the FAQ? That's great.
    #include <iostream.h>
    *grumbles, but continues reading the next line*
    void main (void)
    *aborts*
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes, you are allocating and deallocating memory correctly, but there are better ways to allocate a two-dimensional array (with fewer calls to new).

    First you allocate the entire memory (row*columns items). Then, you allocate an array of pointers which points into the large array. That way, you can allocate a two-dimensional array with only two calls to new, at the cost of a little more memory being used.

    Also, the value returned by new will never be 0; if the allocation fails, new will throw an exception.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Sang-drax
    Also, the value returned by new will never be 0; if the allocation fails, new will throw an exception.
    Of course, if a compiler allows void main, it is quite possible that it returns 0 on failed memory allocation. MSVC++ 6.0 does.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Sang-drax
    Yes, you are allocating and deallocating memory correctly, but there are better ways to allocate a two-dimensional array (with fewer calls to new).

    First you allocate the entire memory (row*columns items). Then, you allocate an array of pointers which points into the large array. That way, you can allocate a two-dimensional array with only two calls to new, at the cost of a little more memory being used.
    Can you explain this in more detail? Would you still be able to access the 2-d array using:
    array[i][j]

    or would you have to use:
    array[i*rows+j]

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by swoopy
    Can you explain this in more detail?
    Certainly.

    Code:
    int rows,cols;
     
    //Allocate the actual data
    int* rawData = new int[rows*cols];
     
    //Allocate pointers to the data
    int** data = new int*[rows];
     
    //Point the pointers into the data
    for (int i=0; i < rows; ++i){
      data[i] = &rawData[cols*i];
    }
     
    //Access element [i,j]
    data[i][j];
     
    //Delete the allocated data
    delete[] rawData;
    delete[] data;
    As you can see, the same [][] notation is used, no loops are required at deletion and most importantly, only two calls to new are made.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. calculations with pointers
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-17-2007, 01:46 PM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM