Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is there a better and fast way to initialize a dynamic array?

Reply
Thread Tools

Is there a better and fast way to initialize a dynamic array?

 
 
jerry
Guest
Posts: n/a
 
      09-30-2011
Consider the following codes:

static int POOL_SIZE 256
static int BUFF_SIZE 1024

static int *buff_pool[POOL_SIZE]={NULL};

void init_buff(int idx)
{
int i;
buff_pool[idx]=(int*)malloc(BUFF_SIZE * sizeof(int));
for (i=0; i<BUFF_SIZE;i+=)
buff_pool[idx][i] = 1;
}

There is an alternative way is to use memset instead of for loop, but
it is said that there is no guarantee that the machine representation
of zero is all-bits-zero. My codes need to work with different
compiler and machine.

I wonder if there is a better and fast way to initialize the dynamic
array without running the whole for loop?

Thanks

Jerry
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-30-2011
On 10/ 1/11 09:30 AM, jerry wrote:
> Consider the following codes:
>
> static int POOL_SIZE 256
> static int BUFF_SIZE 1024
>
> static int *buff_pool[POOL_SIZE]={NULL};
>
> void init_buff(int idx)
> {
> int i;
> buff_pool[idx]=(int*)malloc(BUFF_SIZE * sizeof(int));
> for (i=0; i<BUFF_SIZE;i+=)
> buff_pool[idx][i] = 1;
> }
>
> There is an alternative way is to use memset instead of for loop, but
> it is said that there is no guarantee that the machine representation
> of zero is all-bits-zero. My codes need to work with different
> compiler and machine.
>
> I wonder if there is a better and fast way to initialize the dynamic
> array without running the whole for loop?
>

How about:

enum { POOL_SIZE = 256, BUFF_SIZE = 1024 };

static int *buff_pool[POOL_SIZE]={NULL};

typedef struct X { int buf[BUFF_SIZE]; } X;

static X x;

void init_buff(int idx)
{
buff_pool[idx]=malloc(BUFF_SIZE * sizeof(buff_pool[idx]));

*(X*)buff_pool[idx] = x;
}

int main(void)
{
for( int i = 0; i < BUFF_SIZE; x.buf[i++] = 1);

// stuff.
}
--
Ian Collins
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      09-30-2011


"jerry" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> There is an alternative way is to use memset instead of for loop, but
> it is said that there is no guarantee that the machine representation
> of zero is all-bits-zero. My codes need to work with different
> compiler and machine.


If the memory represents pointers or floating point values, then perhaps
null or 0.0 isn't represented by zeros. But why should it matter for
integers?

(And if integer zero doesn't have a representation of all-bits-zero, I don't
think using assignment is going to help either.)

--
Bartc

 
Reply With Quote
 
jerry
Guest
Posts: n/a
 
      09-30-2011
On Sep 30, 2:47*pm, Ian Collins <(E-Mail Removed)> wrote:
> On 10/ 1/11 09:30 AM, jerry wrote:
>
>
>
> > Consider the following codes:

>
> > static int POOL_SIZE 256
> > static int BUFF_SIZE 1024

>
> > static int *buff_pool[POOL_SIZE]={NULL};

>
> > void init_buff(int idx)
> > {
> > * *int i;
> > * *buff_pool[idx]=(int*)malloc(BUFF_SIZE * sizeof(int));
> > * *for (i=0; i<BUFF_SIZE;i+=)
> > * * * *buff_pool[idx][i] = 1;
> > }

>
> > There is an alternative way is to use memset instead of for loop, but
> > it is said that there is no guarantee that the machine representation
> > of zero is all-bits-zero. *My codes need to work with different
> > compiler and machine.

>
> > I wonder if there is a better and fast way to initialize the dynamic
> > array without running the whole for loop?

>
> How about:
>
> enum { POOL_SIZE = 256, BUFF_SIZE = 1024 };
>
> static int *buff_pool[POOL_SIZE]={NULL};
>
> typedef struct X { int buf[BUFF_SIZE]; } X;
>
> static X x;
>
> void init_buff(int idx)
> {
> * *buff_pool[idx]=malloc(BUFF_SIZE * sizeof(buff_pool[idx]));
>
> * **(X*)buff_pool[idx] = x;
>
> }
>
> int main(void)
> {
> * *for( int i = 0; i < BUFF_SIZE; x.buf[i++] = 1);
>
> * *// stuff.}
>
> --
> Ian Collins- Hide quoted text -
>
> - Show quoted text -


Thanks for your reply.

It will need an extra memory block to hold the initial value. It also
need individual initial block for different type, such as long long,
double, etc. It will increase the memory usage. Considering the
memory usage, is there a another way?

Thanks
 
Reply With Quote
 
jerry
Guest
Posts: n/a
 
      09-30-2011
On Sep 30, 3:18*pm, "BartC" <(E-Mail Removed)> wrote:
> "jerry" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > There is an alternative way is to use memset instead of for loop, but
> > it is said that there is no guarantee that the machine representation
> > of zero is all-bits-zero. *My codes need to work with different
> > compiler and machine.

>
> If the memory represents pointers or floating point values, then perhaps
> null or 0.0 isn't represented by zeros. But why should it matter for
> integers?
>
> (And if integer zero doesn't have a representation of all-bits-zero, I don't
> think using assignment is going to help either.)
>
> --
> Bartc


Thanks for your reply.

Is it the only way that using assignment for floating array?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-30-2011
On 10/ 1/11 11:03 AM, jerry wrote:
> On Sep 30, 2:47 pm, Ian Collins<(E-Mail Removed)> wrote:
>> On 10/ 1/11 09:30 AM, jerry wrote:
>>
>>> I wonder if there is a better and fast way to initialize the dynamic
>>> array without running the whole for loop?

>>
>> How about:
>>
>> enum { POOL_SIZE = 256, BUFF_SIZE = 1024 };
>>
>> static int *buff_pool[POOL_SIZE]={NULL};
>>
>> typedef struct X { int buf[BUFF_SIZE]; } X;
>>
>> static X x;
>>
>> void init_buff(int idx)
>> {
>> buff_pool[idx]=malloc(BUFF_SIZE * sizeof(buff_pool[idx]));
>>
>> *(X*)buff_pool[idx] = x;
>>
>> }
>>
>> int main(void)
>> {
>> for( int i = 0; i< BUFF_SIZE; x.buf[i++] = 1);
>>
>> // stuff.}


Please don't quote signatures and google nonsense.

> Thanks for your reply.
>
> It will need an extra memory block to hold the initial value. It also
> need individual initial block for different type, such as long long,
> double, etc. It will increase the memory usage. Considering the
> memory usage, is there a another way?


Life, and programming, is full of compromises. Here the compromise is
speed/space.

Is the cost of an initialisation loop a measurable problem to you?

--
Ian Collins
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-30-2011
On Fri, 30 Sep 2011 13:30:16 -0700 (PDT), jerry <(E-Mail Removed)>
wrote:

>Consider the following codes:
>
>static int POOL_SIZE 256
>static int BUFF_SIZE 1024
>
>static int *buff_pool[POOL_SIZE]={NULL};


This is a VLA supported only by C99 compilers.

>
>void init_buff(int idx)
>{
> int i;
> buff_pool[idx]=(int*)malloc(BUFF_SIZE * sizeof(int));


Lose the cast. All it does is camouflage an error if you fail to
include stdlib.h.

> for (i=0; i<BUFF_SIZE;i+=)


I assume you meant i++ (or perhaps i+=1).

> buff_pool[idx][i] = 1;
>}
>
>There is an alternative way is to use memset instead of for loop, but
>it is said that there is no guarantee that the machine representation
>of zero is all-bits-zero. My codes need to work with different
>compiler and machine.


Since you don't attempt to assign 0 to any element of the array, what
difference does it make how zero is represented?

memset stores values in bytes. If you are attempting to initialize an
array of char (any flavor) or any other type whose size is 1, it
should work. If the type has a size greater than one, memset is only
useful if each byte of an element should have the same value (which
for practical purposes limits you to integer types with value 0 but
then you could just as easily use calloc).

>
>I wonder if there is a better and fast way to initialize the dynamic
>array without running the whole for loop?


I don't know if it is any better but you could reduce the number of
loop iterations by using memcpy and memmove code along the lines of:

buff_pool[idx][0] = your value;
for (i = 1; i < BUFF_SIZE/2; i,, *= 1)
memcpy(buff_pool[idx],
buff_pool[idx] + i,
i * sizeof *buff_pool[idx]);
memmove(buff_pool[idx],
buff_pool[idx] + BUFF_SIZE/2,
(BUFF_SIZE+1)/2 * sizeof *buff_pool[idx]);

This will work for both non-zero values and non-integer types.

--
Remove del for email
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      09-30-2011
"jerry" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sep 30, 3:18 pm, "BartC" <(E-Mail Removed)> wrote:
>> "jerry" <(E-Mail Removed)> wrote in message


>> > There is an alternative way is to use memset instead of for loop, but
>> > it is said that there is no guarantee that the machine representation
>> > of zero is all-bits-zero. My codes need to work with different
>> > compiler and machine.

>>
>> If the memory represents pointers or floating point values, then perhaps
>> null or 0.0 isn't represented by zeros. But why should it matter for
>> integers?


> Is it the only way that using assignment for floating array?


To initialise a block of floating point numbers to 0.0 where the
representation is unknown?

Assignment is the sure way to do it, but that doesn't mean you have to
assign 0.0 to every element.

For example, assignment can be used to initialise one row, then memcpy() can
be used to copy those 0.0s to the other rows.

And that row can be part of the data, or a special row - or part row - set
aside for that purpose. There are probably other ways of avoiding too many
assignments (perhaps using memset() for platforms that you know for sure use
all-bits-zero, and assignment where that is not the case, using conditional
compilation)

--
Bartc

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-01-2011
On 9/30/2011 5:18 PM, BartC wrote:
>[...]
> (And if integer zero doesn't have a representation of all-bits-zero, I
> don't think using assignment is going to help either.)


First, assigning zero -- or forty-two, or negative ninety-nine --
to an `int' will work just fine, regardless of the representation of
any of those quantities. You can even safely assign a `signed char'
with value eighty-six to a `long double' variable and be confident that
the latter will then have the value eighty-six, even though the two
representations are almost certainly different.

Second, any integer type with all-bits-zero has the numerical value
zero. It may happen that the type has more than one way to represent
the value zero, but all-bits-zero will be one of them. It may or may
not be the "normal" way to represent a zero, but a zero it will be.

(Note that this is only for *integer* types; there is no similar
guarantee for floating-point, complex, or pointer types.)

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-01-2011
On 9/30/2011 6:17 PM, jerry wrote:
> [... filling an array with a constant ...]
> Is it the only way that using assignment for floating array?


It's certainly not the "only" way. For example, you could
fill element [0] (of an array of any kind at all) manually, then
use memcpy() to copy [0] to [1], then memcpy() again to copy
[0] and [1] to [2] and [3], then again to copy [0] through [3]
to [4] through [7], ... Filling an N-element array this way
uses approximately lg(N) memcpy() calls, each filling twice as
many elements as the one before. (Watch out for the boundary
conditions on the final call, though.)

However, I urge you to consider Ian Collin's question: "Is
the cost of an initialisation loop a measurable problem to you?"
The word "measurable" is of great importance: What have you done
to obtain evidence that array-filling is a performance problem?
What have your measurements shown?

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Parsing Binary Structures; Is there a better way / What is your way? Paul Rubin Python 5 08-06-2009 08:06 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
JSP and dynamic XML: Isn't there a better way :-( milkyway Java 1 12-02-2004 02:02 PM
JSP and dynamic XML: Isn't there a better way :-( milkyway XML 1 12-02-2004 02:02 PM



Advertisments