Sign in to follow this  

problem assigning dynamic memory

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this