Re: Parsing cString - Silly question
Re: Parsing cString - Silly question
- Subject: Re: Parsing cString - Silly question
- From: Ryan Britton <email@hidden>
- Date: Fri, 18 Nov 2005 07:22:46 -0800
You could use something like this. Just keep a count of how many
variables you've parsed so you know how many actual valid entries are
in the array. Also, change DELTA appropriately to whatever a
reasonable step size is for the array. Fewer reallocs means less
overhead.
Typed in Mail so not guaranteed to be totally bug free, but it should
work I think.
#define DELTA 10
int count = 0;
int capacity = DELTA:
float *variables;
variables = (float *) malloc(sizeof(float) * capacity);
while (parsing)
{
if (count >= capacity)
{
capacity += DELTA;
variables = (float *) realloc(variables, capacity);
}
}
On Nov 18, 2005, at 7:13 AM, Lorenzo wrote:
Hi,
how can I add an element to an array of float?
I am parsing a text file contains variables, but until I parse the
whole
text I cannot know how many variables that text contains.
Actually I parse the text twice. The first time I count the
variables then
float *variables = malloc(sizeof(float) * totVar);
then I parse the text again to put the variables into my array.
Is a way to do everything in one loop only? Will be faster?
Sorry for the silly question.
Best Regards
--
Lorenzo
email: email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden