Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Memory Allocation

Status
Not open for further replies.

houksyndrome

Technical User
Jun 27, 2001
7
US
I am writing a program that reads (x,y,z) coordinates from a text file and then manipulates them. I need to store the (x,y,z) coordinates in an array. The problem is that there is a different number of points in each file, so I need to use dynamic memory allocation. I have tried using a command like:
float ptarray=new datatype[arraysize], with arraysize being a variable. However, this doesn't work. I would be grateful for any help.
 
Try this syntax to create a dynamic array:
datatype * name = new datatype[size]; // create array
delete[] name; // destroy array

You can also use a struct to represent your coordinate type.


struct _coord {
float x, y, z;
} coord;

coord * pArray = new coord[arraysize];

/* ... */

pArray[n].x = 1.0f; /* access any coord */


Do not forget to free the memory if no longer need
the array (delete[] pArray).

hth

-- ralf
 
what about a linked list?

you can compile and try this:

#include <stdio.h>
#include <stdlib.h>

typedef struct p{
int x,y,z;
struct coord *prev;
struct coord *next;
}coord;

// param current is NULL for the first element ;)
coord *add_coord(coord *current){
coord *newcoord;
if((newcoord = (coord *)malloc(sizeof(coord))) == NULL)
return NULL;
// init the new element and link it
newcoord->prev = current;
newcoord->next = NULL;
newcoord->x = newcoord->y = newcoord->z = 0;
// nextcoorder of the previous element to this element
if(current != NULL)
current->next = newcoord;
return newcoord;
}


int main(){
coord* firstElement;
coord* currentElement;

firstElement = add_coord(NULL);
currentElement = firstElement;

currentElement->x = 3;
currentElement->y = 9;
currentElement->z = 15;

currentElement = add_coord(currentElement);
// now currentElement is the second Element...
currentElement->x = 38;
currentElement->y = 99;
currentElement->z = 35;
// the third and so on...
currentElement = add_coord(currentElement);
currentElement->x = 987;
currentElement->y = 87;
currentElement->z = 25;

currentElement = add_coord(currentElement);
currentElement->x = 4562;
currentElement->y = 1235;
currentElement->z = 1;

currentElement = firstElement;
while(currentElement->next){
printf(&quot;x: %d\ny: %d\nz: %d\n\n&quot;, currentElement->x,currentElement->y,currentElement->z);
currentElement = currentElement->next;
}
// not to forget the last...
printf(&quot;x: %d\ny: %d\nz: %d\n\n&quot;, currentElement->x,currentElement->y,currentElement->z);
}

if you insist on using array, you could try
realloc for dynamic memoryallocation

;)
netcomander

 
Linked list can be overkill to store XYZ coordinates (it can have a sense when in one node you will have more then one point)
I would sugest use realloc function with some reallocation number more around 50. (I has performed some test with reallocation and found that reallocation by 50 is the optimal solution (reallocation by 1 takes in 100 times more time, by 100 almost the same as by 50).
Note: it is a C forum, so you can not use new, delete in C.
You can try this:

double (*padPoints)[3] = NULL; /*ptr to array of XYZ */
int nPnt = 0, nAlloc = 0, nRealloc = 50;

int AddPoint(double adPoint[3])
{
double (*padPntTmp)[3] = NULL;

if (nPnt >= nAlloc)
{
/* allocate/reallocate memory */
padPntTmp = realloc(padPoints,
nAlloc+nRealloc * sizeof (double[3]));
if (padPntTmp != NULL)
{
padPoints = padPntTmp;
nAlloc += nRealloc;
}
else
return 1; /* not enough mem */
}
/* add point */
memcpy(padPoints, adPoint, sizeof (double[3]));
nPnt++;
return 0;
}
int main()
{
double adPoint[3];
/* read X,Y,Z */
if (AddPoint(adPoint))
{
return 1; /* not enough memory */
}
for(i=0; i<nPoint; i++)
printf(&quot;XYZ: %12.5e, %12.5e, %12.5e\n&quot;,
padPoint[0],padPoint[1],padPoint[2]);
.....
if (padPoints != NULL)
free (padPoints);
return 0;
}

I did not test it, but should work.
 
sorry in memcpy should be:
memcpy(padPoints+nPnt, adPoint, sizeof (double[3]));



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top