Allocating matrices

This is a discussion on Allocating matrices within the C++ Programming forums, part of the General Programming Boards category; I forgot how to allocate dynamic matrices! How can I do it?? Code: int **matrix; matrix = new ... for(int ...

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Allocating matrices

    I forgot how to allocate dynamic matrices! How can I do it??
    Code:
       int **matrix;
       matrix = new ...
       for(int i...)
          for(int j...)
             matrix[i][j]=...
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    But the only thing I see is a matrix beeing allocated within a for. Can't I allocate all the memory that I want at once?
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can't I allocate all the memory that I want at once?
    Yes, but if you want to use array-like indexing you'll have to work a bit harder than that. You can find a number of different methods for allocating matrices through a board search even though the most common one uses loops to handle multiple dimensions.
    My best code is written with the delete key.

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Ok thanks than
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Got errors

    Hi there! I made what I was told to do, but now I get Segmentation fault. Does anyone know why?
    Code:
            int **edges;
            *edges = new int[num_nodes];
    	assert(*edges);
    	
    	for(int i=0;i<num_nodes;i++)
    	{
    		edges[i] = new int[num_nodes];
    	}
    
    	cout << "Edges generated" << endl;
    The cout never occur!!!
    Nothing more to tell about me...
    Happy day =)

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >*edges = new int[num_nodes];
    *edges doesn't point to anything. Never dereference until you have something to reference. Try this instead:
    Code:
    edges = new int*[num_nodes];
    assert(edges != 0);
    Also, the current standard requires new to throw an exception if it fails except when tagged with (nothrow). If you have a compiler that conforms to this then the assert is useless.
    My best code is written with the delete key.

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Talking

    THANKS SO MUCH!! IT WORKED(of course!)!!!
    Now, just one more question: Do I still need the for to allocate the matrix? I've never see this way of allocating before!!!!!!
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Do I still need the for to allocate the matrix?
    Yes. You're using a pointer to a pointer here so you'll need to allocate first an array of pointers, then allocate memory to each of the pointers in the array. Then you'll have a matrix.
    My best code is written with the delete key.

  9. #9
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Understood!!! Thanks again!!!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C simple Matrices
    By Cyberman86 in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 05:20 PM
  2. Help getting started..matrices
    By BobDole11 in forum C Programming
    Replies: 7
    Last Post: 11-15-2008, 08:51 PM
  3. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  4. Comparing Matrices (D3D9)
    By MicroFiend in forum Game Programming
    Replies: 2
    Last Post: 10-12-2005, 08:36 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21