Dynamically allocated matrices

This is a discussion on Dynamically allocated matrices within the C++ Programming forums, part of the General Programming Boards category; can anyone please reply with some code to allocate arrays on the free store, like this (but than one that ...

  1. #1
    Unregistered
    Guest

    Dynamically allocated matrices

    can anyone please reply with some code to allocate arrays on the free store, like this (but than one that can compile..):
    int * a;
    a = new int[100][100];

    do you get what I mean?
    thanks in advance

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if you consider that a 2d array is really only an 1d array of 1d arrays then im sure you will see the answer!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    cool nick

    hmm I'm probably kind of an idiot, all I can think of is:

    int **a;
    for(int b=0;b<10;b++)
    {
    a[b] = new int[10];
    for(int c=0;c<10;c++)
    a[b][c] = new int;
    }

    and that gives a runtime error

  4. #4
    Unregistered
    Guest
    Originally posted by Unregistered
    [B]cool nick

    hmm I'm probably kind of an idiot, all I can think of is:

    int ***a;
    for(int b=0;b<10;b++)
    {
    a[b] = new int*[10];
    for(int c=0;c<10;c++)
    a[c] = new int;
    }

    and that gives a runtime error
    this is what I meant..

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Code:
    int ** matrix = new int*[a];
    for (int i = 0; i < a; i++){
    	matrix[a] = new int[b];
    }
    .
    .
    .
    for (int i = 0; i < a; i++){
    	delete[] matrix[a];
    }
    delete[] matrix;

  6. #6
    Unregistered
    Guest

    Talking

    thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically allocated strings??
    By CS_Student8337 in forum C Programming
    Replies: 18
    Last Post: 03-19-2009, 05:14 AM
  2. Dynamically allocated size
    By maverickbu in forum C++ Programming
    Replies: 12
    Last Post: 06-26-2007, 01:16 PM
  3. scope of dynamically allocated variables
    By lanzyzhang in forum C Programming
    Replies: 4
    Last Post: 07-20-2004, 10:59 AM
  4. I need a dynamically allocated linked list
    By Flex in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 01:28 PM

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