help please: problems with arrays 
Author Message
 help please: problems with arrays

I am using Visual C++ and I am having trouble with this bit of code which is
unfortunately the core of my program in a way.

I don't know how to allocate memery properly and how to free the memory at
the end of each loop

// Here are my structures

typedef struct vertex
{
 float coords[3];

Quote:
} vertex;

typedef struct triangle
{
 int i[3];
 int adj[3];
 int angle[3];
    vertex *verts[3];
 vertex *normal;
 float creaseangle;

Quote:
} triangle;

// Here is my array of structure

triangle T[MAX_SHAPES][MAX_TRIS];

As you can see I've got an array of structure...

I have a loop which fills this array with values... I would like to allocate
memory and free memory because once I want to use the function another
time... the values in the array are very strange

here is an axample of my function:

void Cmyclass::fill_array()
{
for (int x=0; x<maxshapes; x++)
    {
        for (int y=0; y<maxtriangles; y++)
            {
                T[x][y].verts->coords[0] = data[0];
                T[x][y].verts->coords[0] = data[1];
                T[x][y].verts->coords[0] = data[2];
            }
    }

Quote:
}

Thanks a lot!!!

Nicolas Bussard



Fri, 30 Jan 2004 20:46:04 GMT  
 help please: problems with arrays
yikes! you could do this more efficiently with a nested union inside of your
struct.  Go read up on it you will see what I am talking about.  As for the
memory allocation and deallocation, you would want to create a new object of
the struct, then when you are finished with the object call the delete on
the struct.  You could also make a class out of it.

Steven


Quote:
> I am using Visual C++ and I am having trouble with this bit of code which
is
> unfortunately the core of my program in a way.

> I don't know how to allocate memery properly and how to free the memory at
> the end of each loop

> // Here are my structures

> typedef struct vertex
> {
>  float coords[3];
> } vertex;

> typedef struct triangle
> {
>  int i[3];
>  int adj[3];
>  int angle[3];
>     vertex *verts[3];
>  vertex *normal;
>  float creaseangle;
> } triangle;

> // Here is my array of structure

> triangle T[MAX_SHAPES][MAX_TRIS];

> As you can see I've got an array of structure...

> I have a loop which fills this array with values... I would like to
allocate
> memory and free memory because once I want to use the function another
> time... the values in the array are very strange

> here is an axample of my function:

> void Cmyclass::fill_array()
> {
> for (int x=0; x<maxshapes; x++)
>     {
>         for (int y=0; y<maxtriangles; y++)
>             {
>                 T[x][y].verts->coords[0] = data[0];
>                 T[x][y].verts->coords[0] = data[1];
>                 T[x][y].verts->coords[0] = data[2];
>             }
>     }
> }

> Thanks a lot!!!

> Nicolas Bussard



Fri, 30 Jan 2004 22:49:08 GMT  
 help please: problems with arrays

Quote:

<snip>
> // Here are my structures

> typedef struct vertex
> {
>  float coords[3];
> } vertex;

> typedef struct triangle
> {
>  int i[3];
>  int adj[3];
>  int angle[3];
>     vertex *verts[3];
>  vertex *normal;
>  float creaseangle;
> } triangle;

> // Here is my array of structure

> triangle T[MAX_SHAPES][MAX_TRIS];

Do the triangle.verts need to be a pointer array?

If you define it this way your vertex array will be allocated when you
declare the triangle:

typedef struct triangle
{
        int i[3];
        int adj[3];
        int angle[3];
        vertex verts[3];
        vertex normal;
        float creaseangle;

Quote:
} triangle;

<snip>

Check your indexing here:

Quote:
>         for (int y=0; y<maxtriangles; y++)
>             {
>                 T[x][y].verts->coords[0] = data[0];
>                 T[x][y].verts->coords[0] = data[1];
>                 T[x][y].verts->coords[0] = data[2];
>             }

-Alf


Sun, 01 Feb 2004 16:57:40 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

2. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

3. problems with array allocation and destruction::: HELP PLEASE

4. Arrays with no pointers - problem (please help!)

5. Problem with array's PLEASE HELP

6. Problems with sorting arrays-Please Help

7. Problems with array elements from user input, please help

8. array problem PLEASE HELP

9. Urgent please help LPSTR Array function passing problem

10. HELP Please Please (malloc on an array)

11. Please help!!!!Please help!!!!Please help!!!!

12. Please please help!!!!!!!!! (link problems)

 

 
Powered by phpBB® Forum Software