array creation on the fly?

This isnt really OpenGL related but seems that I am creating a format to read data into my OpenGL app Ill try squeeze it by.

I have a text file that looks like this:

----> Begin Text file
0,1,2 ,0,1,2 ,0,1,2
2,1,0 ,2,0,1 ,0,2,1
----> End Text File

Now I want to read this data and dynamically create an array such as (language is C)

short face[2][9] =
{0,1,2 ,0,1,2 ,0,1,2 },
{0,2,3 ,0,3,1 ,0,2,1 }

Now if the text file did look like this
----> Begin Text file
----> End Text File

The resulting array would need to look like this

float texture [4][2] =
{0.5, 0.8),
{0.7, 0.8),
{0.9, 0.2),
{0.2, 0.4}

So basically the job is to create the array on the fly using elements read from the data file. IE no fixed array size until the data file is processed.

Thanks in advance for any help

[This message has been edited by dans (edited 07-19-2000).]

Not the right forum, but:

----> Begin Text file
0,1,2 ,0,1,2 ,0,1,2
2,1,0 ,2,0,1 ,0,2,1
----> End Text File

short *arr;
int sizex, sizey;

sizex = (read in first array size [2])
sizey = (read in second array size [9])

arr = new short[sizex*sizey];

Now you got an array 2*9. Here your read
in the values from the file sequentially.

When reading the array you can’t use notation
like arr[x][y], but you have to use something like arr[x + y * sizex].

Second Tip:
Learn C++ and the use of containers :wink:


Cool i get it. Thanks !! I did use the same method with 680x0 assembler cpu copy back on the miggy, for some reason i just could not click it over to C. I will finish sussing C out then Ill move onto c++ or maybe Java. Im sure i have created my own hybrid language by now anyhow

Originally posted by dans:
Im sure i have created my own hybrid language by now anyhow

Former Blitz programmer, by any chance?

[This message has been edited by MikeC (edited 07-20-2000).]

if this is straight ANSI C, and not C++, then the “new” operator doesn’t exist.

modified code is:

#include “stdlib.h”

int main() {

int i, j;

// a two-dimensional array is a pointer-to-pointer-to-short
short **arr;
int sizex = 2, sizey = 9;

// in the x dimension, arr is an array of pointers to short
arr = (short **) malloc( sizeof(short *) * sizex);
for (i = 0; i < sizex; i++) {
// in the y dimension, each of the pointers in the x dimension point to a short.
arr[i] = (short *) malloc( sizeof(short) * sizey );

// here, we just access every element in the array (so the program will crash if something is wrong.)
// elegant debugging. :slight_smile:
for (i = 0; i < sizex; i++)
for (j = 0; j < sizey; j++)
arr[i][j] = 0;

// free all of the pointers that make up the array.
for (i = 0; i < sizex; i++) {
free( arr[i] );
free( arr );

// then it quits. you’ll obviously want to do more.
return 0;

you can now access arr with arr[x][y] like you would normally want to.

and when you’re done with that array, you need to free the memory, as per the example.

[This message has been edited by phlake (edited 07-20-2000).]

[This message has been edited by phlake (edited 07-20-2000).]

Nope Just ASM here, HiSoft’s Devpac was my compiler of choice. I never touched c until late last year. and it took me along time to give up the poor old Miggy.

Thanks for the input matey, The routine seems to work really well. I gather that **arr is a offseted pointer to *arr
I hope i am reading this right.

explanation of what **arr is (and bear with me, cuz it’s long. i don’t know what you know or don’t know, so i start at the top. forgive my belaboring the point. =):

in C, an array is synonymous with a pointer. and that pointer has a certain type, like int or short or whatever. that just dictates how many bytes are allocated for each element.

the difference between saying, for example

short myArray[4];


short *myArray;

is that with the former the compiler knows how big the array should be, and automatically allocates space for it, as well as the pointer to its base (element 0).

with the latter, the compiler only knows that you need a pointer to type short, and gives you space for the pointer. if you want it to actually refer to something, you must do that yourself.

(pointers have other uses than just arrays, as you probably are aware. but i’m focusing on just the arrays part, because of the nature of the original question.)

by reserving space with malloc for the number of elements we desire. malloc returns a pointer to void, which we then cast to the type of pointer we need. also, we use sizeof(element) * numElements to tell malloc how many bytes to reserve. and so:

myArray = (short *) malloc( sizeof( short ) * numElements);

now, a two-dimensional array is essentially an array of arrays. if an one-dimensional array is one row, and its elements are columns in that row, then a two-dimensional array is a set of those rows.

it is a collection of pointers, each of which point to a single pointer, which points to the base address of the row.

so with:

short **arr; // also means *arr[]

we declare what will become an array of pointers. each of those pointers is in turn an array. so it’s an array of arrays or a pointer to pointers.

and that is the long-winded way to what it is. email me if you’ve got questions.

[getting silly with it:] to make it really tough, try passing that array of arrays by reference to a function. then you need a pointer-to-pointer-to-pointer-to-short. and that can be a pain to deal with, i know.

for the record, c++ makes this somewhat easier to deal with, and more difficult. (i think it hates multi-dimensional arrays. at least, it seems to go out of its way to make them throw compiler errors.)

[This message has been edited by phlake (edited 07-21-2000).]