User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 11 Dec 2002, 11:49   #1
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Question Dynamic 2D arrays?

How do I make a 2D array dynamic?
When I try this:

Code:
float *vertexA;
vertexA		= new float[vertexcounter][3];	//vertexes has 3 floats
I get the following error:
C:\WINDOWS\Desktop\Stage_Johan\PRJ\GL\GL10\object3D.cpp(440) : error C2440: '=' : cannot convert from 'float (*)[3]' to 'float *'
__________________
"Yay"
Structural Integrity is offline   Reply With Quote
Unread 11 Dec 2002, 12:24   #2
Cyp
∞+♪˛
 
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
Cyp is an unknown quantity at this point
Re: Dynamic 2D arrays?

Quote:
Originally posted by Structural Integrity
How do I make a 2D array dynamic?
When I try this:

Code:
float *vertexA;
vertexA		= new float[vertexcounter][3];	//vertexes has 3 floats
I get the following error:
C:\WINDOWS\Desktop\Stage_Johan\PRJ\GL\GL10\object3D.cpp(440) : error C2440: '=' : cannot convert from 'float (*)[3]' to 'float *'
float **vertexA;
float *(vertexA[3]);
float (*vertexA)[3];

Probably one of the above, might be the one in the middle.
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics...
<!--Last incarnation: Nov 2000-->
Cyp is offline   Reply With Quote
Unread 11 Dec 2002, 13:40   #3
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
yeh! The third one doesn't give any errors (haven't tried the others tho )

But one more question: basically this is an array of pointers to arrays of floats (o, that was difficult)

Code:
 |array of pointers
\|/

pointer1 -> array of floats
pointer2 -> array of floats
pointer3 -> array of floats
...
Now, I know that I have to delete an dynamically made array, but do I have to delete each row in this array sperately?

Like:
Code:
 |array of pointers
\|/

pointer1 -> array of floats <-- Delete this seperately (delete [] vertexA[0])
pointer2 -> array of floats <-- Delete this seperately (delete [] vertexA[1])
pointer3 -> array of floats <-- Delete this seperately (delete [] vertexA[2])
... etc ...

delete [] vertexA; //delete remaining array of pointers
or can I delete it in one go:

Code:
delete [] vertexA;
????
__________________
"Yay"
Structural Integrity is offline   Reply With Quote
Unread 11 Dec 2002, 14:51   #4
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
You can delete it in one go. It's not a pointer to an array of pointers, it a pointer to a large array:

vertexA[0][0]
vertexA[0][1]
vertexA[0][2]
vertexA[1][0]
vertexA[1][1]
vertexA[1][2]
......
queball is offline   Reply With Quote
Unread 11 Dec 2002, 14:52   #5
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Quote:
Originally posted by queball
It's not a pointer to an array of pointers, it a pointer to a large array:
???

It's dynamic, surely it's an array of pointers?
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 11 Dec 2002, 16:01   #6
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by Gayle28uk

???

It's dynamic, surely it's an array of pointers?
It's not dynamic in two directions though, it's a dynamic array of float[3].
queball is offline   Reply With Quote
Unread 11 Dec 2002, 16:20   #7
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
I've confused myself now

I thought arrays in C worked like this...

x[0][0] x[0][1] x[0][2] x[1][0] x[0][1] ...

is equivalent to...

float * x[0] x[1] x[2] x[3] x[4] x[5] ...

Obviously that falls down when you don't have contiguous memory addressing but that's why dynamic arrays in C were always a pain in the butt.

[edit]I just realised, you're talking C++. I wondered why malloc/calloc (the way I remember) weren't coming into this. No longer confused, I just don't know anything about C++[/edit]
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 11 Dec 2002, 16:28   #8
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Well it's mostly the same in C,
int i, n;
float *array[3];

n = 57;
assert (array = malloc (sizeof (float) * n));

for (i = 0; i < n; i++)
{
array[i][0] = 1.0;
array[i][1] = 2.0;
array[i][2] = 3.0;
}

free (array);

Not an array of pointers.
queball is offline   Reply With Quote
Unread 11 Dec 2002, 16:40   #9
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
I was thinking like this though...

float *array is allocated a block of memory using calloc, an element is to be added so array[counter]=calloc(3,sizeof (float)).

A static block is allocated to hold the maximum number of vertices (in SIs case) and the memory to hold the vertex data is allocated as required.

Hopefully that made sense.
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 11 Dec 2002, 16:51   #10
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
I don't like calloc with floats
Do you mean like this?

float **block = calloc (n, sizeof (float *));

for (i = 0; i < n; i++)
block[0] = malloc (3 * sizeof (float));

for (i = 0; i < n; i++)
{
array[i][0] = 1.0;
array[i][1] = 2.0;
array[i][2] = 3.0;
}

This sort of structure is possibly more "dynamic", you could have the same vertex pointed to twice or something. But it adds more indirection and isn't usually more useful or memory efficient especially with such small data structures. Though it's important for anyone doing C arrays to realise that float *p[3] is very different from float **p.
queball is offline   Reply With Quote
Unread 11 Dec 2002, 17:01   #11
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Pretty much. It's so long ago that I can't remember the specifics of pointer referencing and dereferencing but hopefully you see why I got confused trying to see what was going on in the original now I KNEW it didn't look familiar!
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 07:46.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018