Thread: Memory allocation

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Red face Memory allocation

    I'm just wondering if it's possible to allocate a matrix with the "new" command in any way like this:

    matrix=new float [size][size];

    I've tried most things, and can't find it anywhere on the net.



    Leiken

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Code:
    int** matrix=new int*[size];
    
        for(int i=0;i<size;i++)
            matrix[i]=new int[size];

  3. #3
    Unregistered
    Guest
    it's possible.

    float ** maze;

    maze = new float*[sizeA];
    for(int i = 0; i < sizeA; i++)
    {
    maze[i] = new float[sizeB];
    }

    where sizeA and sizeB are integers. Also look up how to delete the dynamically memory so declared to prevent memory leaks in your program.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    The answer to your question: No.
    You have to use the code that Sorensen gave you.
    Deleting this memory is as simple as

    delete[] maze;

    The rest should be automatically done when you call this.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Deleting this memory is as simple as delete[] maze;

    After looping through and deleting the indivdual pointer to floats using a similar method to the allocation.

    Code:
     for (i=0;i<size;i++)
            delete [] maze[i];
    
        delete [] maze;

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Thanks

    Thanks loads guys! I'm happy now..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM