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?

 
 
Keith Thompson
Guest
Posts: n/a
 
      10-01-2011
jerry <(E-Mail Removed)> writes:
> Consider the following codes:
>
> static int POOL_SIZE 256
> static int BUFF_SIZE 1024


Missing semicolons.

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


That initializes each element of buf_pool to a null pointer.
The first is explicit, the others are implicit (elements not
specified in an initializer are initialized to zero converted to
the appropriate type).

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


Drop the cast.

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


i+= what?

It would be *really* helpful if you'd post the actual code you compiled,
copy-and-pasted, not re-typed. Otherwise it can be hard to tell the
difference between transcription errors and the actual problem you're
asking about.

> 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.


You're setting the elements to 1, not 0. Or was that a typo?

For integer types, there is a guarantee that all-bits-zero is a
representation of 0 (though not necessarily the only such
representation). N1256 6.2.6.2p5:

For any integer type, the object representation where all the
bits are zero shall be a representation of the value zero in
that type.

(This wasn't in the original C99 standard; it was added in one of the
Technical Corrigenda, presumably because all existing implementations
already behaved this way.)

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


--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-01-2011
On 10/ 1/11 12:00 PM, Barry Schwarz wrote:
> 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.


No, it's an error. You can't declare a VLA at file scope.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-01-2011
Keith Thompson <(E-Mail Removed)> writes:

> jerry <(E-Mail Removed)> writes:
>> Consider the following codes:
>>
>> static int POOL_SIZE 256
>> static int BUFF_SIZE 1024

>
> Missing semicolons.


(and = signs)

<snip>
--
Ben.
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      10-01-2011
On Sep 30, 10:30*pm, jerry <(E-Mail Removed)> wrote:
>
> 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;
>
> }
>
> I wonder if there is a better and fast way to initialize the dynamic
> array without running the whole for loop?
>

Not really. On some machines you could set a long or long long to
0x0000000100000001, then treat buff_pool as a long *, and you might
initialise the ints faster. But it's very machine specific, and it
makes the code hard to read. It's very much a last resort.


 
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