dinamic memory

i don't know what's wrong with this code, dude:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>
using namespace std;

int main () {
	int *row;
	row = new int [2,2];
	
	row [0,0] = 3;
	row [0,1] = 5;
	row [1,0] = 7;  //even if it's 5,0 and
	row [1,1] = 11; //5,1 in here, it's still works
	
	for (int i = 0; i < 2; ++i) {
		for (int j = 0; j < 2; ++j) {

			//it will always print the last array (e.g: 7 and 11)
			cout << row [i,j] << endl;
		}
	}

	getch();
	return 0;
}


i'm waiting...
If you are trying to create a 2D array: http://www.cplusplus.com/forum/articles/7459/
You're trying to make a multidimensional array from a single pointer. I'm afraid you can't do that the way you're doing it. :/

I'd suggest you use an int**, and note that new[] should accept only one integer. Also you really should delete[] the memory you allocated with new[], for fear of a memory leak.

ADVANCE WARNING: You'll probably need a for loop (or something like a for loop) to initialize your 2d array. :)

http://cplusplus.com/doc/tutorial/arrays/#multidimensional_arrays

EDIT: Ninja'd.

-Albatross
Last edited on
closed account (D80DSL3A)
Albatross wrote:
You're trying to make a multidimensional array from a single pointer. I'm afraid you can't do that.


Not to be rude, but according to my textbook it can be done like so:

Declare a pointer to an array of elements:
int (*p_fourInts)[4];// this is a single pointer to 4 elements, not an array of 4 pointers

then use it to allocate a 2D array:
p_fourInts = new int [3][4];// allocate 3 blocks of 4 integers

Deletion follows the usual convention, just:
delete [] p_fourInts;
Correction: he couldn't quite do it the way he was doing it. You know what I meant. :)

-Albatross
Last edited on
closed account (D80DSL3A)
Yes, I know what you meant. I just couldn't resist giving a counterexample. :)

I usually use the method you outlined as it allows BOTH dimensions to be determined dynamically.
The example I gave may miss the OP's needs as the 2nd dimension is fixed. Only the 1st dimension (the 3) can be set at run time with the method I showed.
@albatross: just say: "syntax error" dude...
@fun: so we have to do it like this?

1
2
int *p[4]; //to make 4 arrays first
p = new int [3][4]; //to make 2nd dimension of 4 arrays we just declared 


is that what you meant? can we just declare it like:

 
int *p = new int [3][4];



Only the 1st dimension (the 3) can be set at run time with the method I showed.



int (*p_fourInts)[4];// this is a single pointer to 4 elements, not an array of 4 pointers


what do you mean by these?

anyway, what's an OP? just curious :)
Last edited on
@chipp:
If I just said "syntax error", that wouldn't tell you much about what's wrong, would it? :)

int *p[4];

Note in your example that the parenthesis (which are missing) are rather crucial.

can we just declare it like:

Nope. If you compiled it, you'd get an error along the lines of "invalid assignment". :/

what do you mean by these?

The first quote states that one of the dimensions (the 4) has to be fixed: it must be known at compile time and I daresay you'd have a lot of fun trying to change it at runtime. ;)

The second explains basically how the method works. p_fourInts here is a pointer to an array. Specifically, it's a pointer to an array that's four elements long, where the elements are integers. On the next line, we allocate three of those arrays for the pointer to point to.

what's an OP?

It's a forum acronym for "Original Poster".

Happy coding!

-Albatross
Last edited on
so, if we just declare it like:
 
int * p [4];


it would be considered as 4 elements of pointer ("int *" type)?

you'd have a lot of fun trying to change it at runtime. ;)


how to change it? like this?

1
2
3
4
5
int count;
cout << "how much you want the array is: ";
cin >> count;

int (*p) [count];



On the next line, we allocate three of those arrays for the pointer to point to.


so is this the way to make a 2D array? how if you wanna make 3D array?

note: I'AM lot-of-question guy :))
int * p [4];
it would be considered as 4 elements of pointer ("int *" type)?
Yes

how to change it? like this?
1
2
3
4
5
int count;
cout << "how much you want the array is: ";
cin >> count;

int (*p) [count];
Not valid in C++, automatic array sizes must be known at compile time.




so, how?

after all, all i know this time, pointer is the type of "int *" but after this how could it called "pointer" because it separated by "(" like: int (*point) ? i'm still confused...
The paranthesis are just there to differentiate between a pointer to four ints, and four int pointers.
 
int *p[4];

p is an array of 4 pointers here -
 
int (*p) [4];

p is a pointer to an array of 4 ints.
and how to change the fixed array at the run time?
I can't think of a way in standard C++ off the top of my head without using inline assembly... there's a reason it's called fixed. *wink*

If you want control over the sizes of both dimensions at runtime, try something like this:
1
2
3
4
int** mat;
mat = new int*[size1]; //Create an array of pointers to ints.
for(int i = 0; i < size1; ++i)
    mat[i] = new int[size2]; //Create an array of ints for each pointer in the above array. 


...just remember to replace size1 and size2 with something meaningful and make sure that you delete [] each one of those arrays (including the array of pointers) after use...

1
2
3
for (int i = 0; i < size1; ++i)
    delete [] mat[i]; //Delete each of the arrays of ints we allocated.
delete [] mat; //Delete the array of pointers. 


Hopefully, my sleepiness didn't contribute to too many errors.

-Albatross
Last edited on
i wanna get it clear, so you called it fixed because it has to known before you declare the 2nd dimension in the next statement?

i see now, as far as i understand, you can explicitly declare the first dimension but it cannot be interrupted, otherwise (if it depends on user's input) you must use new, am i right?

CMIIW

oh i forgot it's night in there, while we're in asia is daylight :))
Last edited on
i wanna get it clear, so you called it fixed because it has to known before you declare the 2nd dimension in the next statement?

It's called fixed cause the dimension has to be known at compile time. Basically he created a 1 dimensional dynamic array of 1 dimensional fixed arrays, thus creating a 2 dimensional one.


i see now, as far as i understand, you can explicitly declare the first dimension but it cannot be interrupted, otherwise (if it depends on user's input) you must use new, am i right?

I don't understand that part... can you please rephrase that somehow?
what i mean about interruption is non-fixed value, that is it has to be a fixed value, like int (*p)[5] not like the "interruption" like: int (*p) [item1] which has non-fixed value.
My english isn't perfect, but I could swear "to interrupt" means to stop doing something.
well... just forget about what i've said about the interruption, dude... :))
closed account (D80DSL3A)
I wish to apologize for throwing such confusion into this thread with my example. I didn't think it would do that , but it did.

@chipp. Use the method given by Albatross in his last post. That method allows both array dimensions to be determined dynamically.
Topic archived. No new replies allowed.