Skip to main content
gamedev.net
🔒 Locked

problem assigning dynamic memory

Started by Fyber Jan 26, 2006 at 12:09 PM 4 replies 900+ views
Original Post
Fyber
Fyber
I have a problem when im assigning dynamic memory. This works oke: int * p; p = new int[10]; But when I try to do this is get errors: int * p; p = new int[10][10]; Is it possible to dynamicaly assign a 2d array. and if so what am i doing wrong. Thx Fyber
dalep
dalep
p = new int[10 * 10];

When you are doing dynamically-allocated arrays this way, compiler won't care how you address the subscripts.

int test = p[2, 3];

will work and so will

int test = p[3, 4, 1];

edit - I guess I should mention that by "work" I mean the compiler won't complain. But you'll have to implement your own subscripting offset logic for rows and columns in the 2D array.
Fyber
Fyber
Thx for your help but now im haveing another problem.
When im running this:
#include <stdlib.h>#include <stdio.h>#include <iostream>#include <fstream>int * pMap;int x,y;FILE * pFile;int ReadMap(char * filename){    pFile = fopen(filename, "rt");    fscanf(pFile,"%d %d\n", &x, &y);    pMap = new int[x,y];    int z;    for (int j=0; j < y; j++)    {        for (int i=0; i < x; i++)        {            fscanf(pFile, "%d ", &z);            pMap[j,i] = z;        }    fscanf(pFile, "\n");    }    fclose(pFile);}int main(){    ReadMap("something.mp");    cout << x << "  " << y << endl;    for (int j=0; j < y; j++)    {        for (int i=0; i < x; i++)        {            cout << pMap[j,i] << ", ";        }    cout << endl;    }    delete [] pMap;    system("PAUSE");    return 0;}


with this being in the something.mp:
5 4
3 42 4 12 57
34 87 124 568 2
32 57 234 8 2
83 672 76 78 5

when i run the following its show the last 5 characters 4 times.
How would is solve this?

Thx Fyber
demonkoryu
demonkoryu
pMap[j,i] = z;

This is not a multi-dimensional array access. You use the comma operator, which returns the result of the expression after the last comma, i.e. i.
You probably wanted to write
pMap[j][j] = z;
Zahlman
Zahlman
Quote:
Original post by dalep
p = new int[10 * 10];

When you are doing dynamically-allocated arrays this way, compiler won't care how you address the subscripts.

int test = p[2, 3];

will work and so will

int test = p[3, 4, 1];

edit - I guess I should mention that by "work" I mean the compiler won't complain. But you'll have to implement your own subscripting offset logic for rows and columns in the 2D array.


Not in C++. That invokes the seldom-used "comma operator", such that e.g. for p[2, 3], the effect is "evaluate '2', throw away the result, then evaluate '3'; then the result of evaluating '3' (i.e., 3) is used for subscripting". You need to calculate the offsets manually, in place, because there is nowhere you could "pass" the two arguments to - even if you made a custom struct or class, operator[] overloads can only accept one argument, period.

You should probably read this, and then forget about doing it yourself and use this.
dalep
dalep
Yeah. That's what I meant I suppose. I personally do it this way:

int test = p[i * numrows + j];

or i * numcols - whichever is most convenient in the context.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.