Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:55:40+00:00 2026-06-14T00:55:40+00:00

I am implementing a flexible array in C. All ideas are based upon this

  • 0

I am implementing a flexible array in C. All ideas are based upon this small Paper.
My error is with the indexing operation. Been hacking away for too long to admit. Asking for knowledgable minds and eyes.

Overview:
The data structure consists of 1 flat index block which holds pointers to the data blocks. Each data block has size 2^(k/2). Where k is the leading set bit. So when I search for element “i”, k is log_2(i+1).

index = { 
    [0], -> [0]
    [2],  -> [0,1]
    [2],  -> [0,1]
    [3],  -> [0,1,2,3]
    [4],  -> [0,1,2,3]
    ....}

The size of each data block is determined by the “super block” it is clustered into. Where a super block is made up of data blocks with the same size. So. index 1,2 are in the same super block (super block 1). While 0 (super block 0), and index 3 (superblock 2) are not.

You end up with each superblock having 2^(floor(k/2)) data blocks, and each of those data blocks has size 2^(ceil(k/2)).

The Problem
When r = a power of 2 the index skips over what is should be.
Like when I search for 3 it should be index [2][0], instead its index[3][0].
Why does this happen? any way to avoid it? Is there an “off by 1” error I’m not seeing??

The Code
Here is the single main function testcase, it’s clear and simple, and fails when trying to get element at index 3;

The simplified code for the locating index i is this:

/* Edited from the actual test case for extra clarity */
/* all vars int */
r= i+1
k = first_set_bit(r) - 1;   // ex: r = 5, 5 = "00000101"b so k is (3-1) = 2

b = first_subset_of_r(floor(k/2)); // floor(k/2) bits of r immediately after first set; 
e = last_subset_of_r(ceil(k/2); // last ceil(k/2) bits of r 
p = (1 << k) -1 ; // 2^k -1
// Index supposed to be found with. . .
return index[p+b][e];

Here is some real output, of first printing the array’s contents by index and data block, then the output of 4 trys into the index

The first part dumps the 2d array, where the number before the bar is the index of the index block, and the part after is the elements contained in the array that the index block points to. all arrays are zero indexed.

[clemensm@gaia:23]> ./a.out 
Index block |  element number (Not it's index!)
0 | 0   
1 | 1   2   
2 | 3   4   
3 | 5   6   
4 | 7   8   9   10  
5 | 11  12  13  14  
6 | 15  16  17  18  
7 | 19  20  21  22  
8 | 23  24  25  26  
9 | 27  28  29  30  
10 | 31  32  33  34  35  36  37  38  
11 | 39  40  41  42  43  44  45  46  
12 | 47  48  49  50  51  52  53  54  
13 | 55  56  57  58  59  60  61  62  
14 | 63  64  65  66  67  68  69  70  
15 | 71  72  73  74  75  76  77  78  
16 | 79  80  81  82  83  84  85  86  
17 | 87  88  89  90  91  92  93  94  
18 | 95  96  97  98  99  100 101 102 
19 | 103 104 105 106 107 108 109 110 
Finished element dump

Trying to get 0
R: [1]b
k/2=[0], Ceil(k,2)=[0]
K: [0] is the leading 1 bit
B: [0]
E: [0]
P: [0] data blocks prior to our superblock
p+b,e : [0,0] 

Trying to get 1
R: [10]b
k/2=[0], Ceil(k,2)=[1]
K: [1] is the leading 1 bit
B: [0]
E: [0]
P: [1] data blocks prior to our superblock
p+b,e : [1,0] 

Trying to get 2
R: [11]b
k/2=[0], Ceil(k,2)=[1]
K: [1] is the leading 1 bit
B: [0]
E: [1]
P: [1] data blocks prior to our superblock
p+b,e : [1,1] 

Trying to get 3
R: [100]b
k/2=[1], Ceil(k,2)=[1]
K: [2] is the leading 1 bit
B: [0]
E: [0]
P: [3] data blocks prior to our superblock
p+b,e : [3,0] 
a.out: test_array.c:81: main: Assertion `get_index(3)==3' failed.
Abort (core dumped)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:55:41+00:00Added an answer on June 14, 2026 at 12:55 am

    Just for the search engines and clearity, the paper name: Resizable Arrays in Optimal Time and Space
    As far as I see it from the paper, you can make a very basic experience here. Even if it is a nice looking paper, there can be mistakes.

    The locate algorithm is clearly wrong. Line 3 claiming to calculate the number of data blocks prior to SB_k can not work this way. You see this already in your example above.

    I suggest to find out the formula by yourself and continue reading afterwards.

    My analysis suggests this formula:

    p = 2^(k/2 + 1) - 2 + (k mod 2) * 2^(k/2) # "/" indicates integer division
    

    Sample code:

    int max = 20;
    for (int k = 0; k < max; k++)
        System.out.println("k=" + k + " - data blocks: " + Math.pow(2, k/2) + " - data blocks size: " + Math.pow(2, Math.ceil(k/2.0)));
    System.out.println();
    
    for (int i = 0; i < max; i++) {
        String r = Integer.toBinaryString(i + 1);
        int k = r.length() - 1;
        String b_bin = r.substring(1, 1 + k/2);
        int b = Integer.parseInt("0" + b_bin, 2);
        String e_bin = r.substring((int)Math.ceil(k / 2.0));
        int e = Integer.parseInt("0" + e_bin, 2);
        int p = (1 << (k/2 + 1)) - 2 + (k & 1) * (1 << (k/2));
        int db = p + b;
        System.out.println("i=" + i + ", r=" + r + ", k=" + k + ", b_bin=" + b_bin + ", b=" + b + ", e_bin=" + e_bin + ", e=" + e + ", p=" + p + ", db=" + db);
    }
    

    Sample results:

    k=0 - data blocks: 1.0 - data blocks size: 1.0
    k=1 - data blocks: 1.0 - data blocks size: 2.0
    k=2 - data blocks: 2.0 - data blocks size: 2.0
    k=3 - data blocks: 2.0 - data blocks size: 4.0
    k=4 - data blocks: 4.0 - data blocks size: 4.0
    k=5 - data blocks: 4.0 - data blocks size: 8.0
    k=6 - data blocks: 8.0 - data blocks size: 8.0
    k=7 - data blocks: 8.0 - data blocks size: 16.0
    k=8 - data blocks: 16.0 - data blocks size: 16.0
    k=9 - data blocks: 16.0 - data blocks size: 32.0
    k=10 - data blocks: 32.0 - data blocks size: 32.0
    k=11 - data blocks: 32.0 - data blocks size: 64.0
    k=12 - data blocks: 64.0 - data blocks size: 64.0
    k=13 - data blocks: 64.0 - data blocks size: 128.0
    k=14 - data blocks: 128.0 - data blocks size: 128.0
    k=15 - data blocks: 128.0 - data blocks size: 256.0
    k=16 - data blocks: 256.0 - data blocks size: 256.0
    k=17 - data blocks: 256.0 - data blocks size: 512.0
    k=18 - data blocks: 512.0 - data blocks size: 512.0
    k=19 - data blocks: 512.0 - data blocks size: 1024.0
    
    i=0, r=1, k=0, b_bin=, b=0, e_bin=1, e=1, p=0, db=0
    i=1, r=10, k=1, b_bin=, b=0, e_bin=0, e=0, p=1, db=1
    i=2, r=11, k=1, b_bin=, b=0, e_bin=1, e=1, p=1, db=1
    i=3, r=100, k=2, b_bin=0, b=0, e_bin=00, e=0, p=2, db=2
    i=4, r=101, k=2, b_bin=0, b=0, e_bin=01, e=1, p=2, db=2
    i=5, r=110, k=2, b_bin=1, b=1, e_bin=10, e=2, p=2, db=3
    i=6, r=111, k=2, b_bin=1, b=1, e_bin=11, e=3, p=2, db=3
    i=7, r=1000, k=3, b_bin=0, b=0, e_bin=00, e=0, p=4, db=4
    i=8, r=1001, k=3, b_bin=0, b=0, e_bin=01, e=1, p=4, db=4
    i=9, r=1010, k=3, b_bin=0, b=0, e_bin=10, e=2, p=4, db=4
    i=10, r=1011, k=3, b_bin=0, b=0, e_bin=11, e=3, p=4, db=4
    i=11, r=1100, k=3, b_bin=1, b=1, e_bin=00, e=0, p=4, db=5
    i=12, r=1101, k=3, b_bin=1, b=1, e_bin=01, e=1, p=4, db=5
    i=13, r=1110, k=3, b_bin=1, b=1, e_bin=10, e=2, p=4, db=5
    i=14, r=1111, k=3, b_bin=1, b=1, e_bin=11, e=3, p=4, db=5
    i=15, r=10000, k=4, b_bin=00, b=0, e_bin=000, e=0, p=6, db=6
    i=16, r=10001, k=4, b_bin=00, b=0, e_bin=001, e=1, p=6, db=6
    i=17, r=10010, k=4, b_bin=00, b=0, e_bin=010, e=2, p=6, db=6
    i=18, r=10011, k=4, b_bin=00, b=0, e_bin=011, e=3, p=6, db=6
    i=19, r=10100, k=4, b_bin=01, b=1, e_bin=100, e=4, p=6, db=7
    

    edit: More details about the way to the formula

    How to get the formula (I suggest to read every step and think if you can continue by yourself, if not continue to tread):
    The paper says: "When superblock SB_k is fully allocated, it consists of 2^floor(k/2) data blocks"

    If we want the number of data blocks prior to SB_k, we have to sum over all of them:

    p = sum from i=0 to k-1 over 2^floor(i/2)
    

    We could use this now, this is a clear for loop statement. But lets make it a single calculation.
    If you think about the sum, it sums up every 2^i two times because of the floor. This is true for all even k, because the sum max is uneven then, hence we have floor((k-2)/2) = floor((k-1)/2).
    For odd k, we have one single number, we have to care about this separately.

    So, we have now:

    (2 * sum from i=0 to floor((k-2)/2) over 2^i) + (k mod 2)*2^k/2
    

    (make some examples for k=6,7,8 if you need some more details)

    Now, we can clearly get rid of the sum, because we know that sum from i=0 to n over 2^i = 2^(n+1) - 1 (this is a basic math/cs proof. You can clearly see the correctness from the binary representation). We use this formula to get:

    2 * (2^(floor((k-2)/2) + 1) - 1) + (k mod 2)*2^k/2
    

    Now, we can multiply by the 2, modify the exponent and we are finished:

    2^(floor(k/2) + 1) - 2 + (k mod 2)*2^k/2
    

    I hope, this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

implementing publishActivity in PHP using the REST API using this code: $activity = array(
Implementing the basic algorithm using last array as a pivot in Java, is it
When implementing move constructors and move assignment operators, one often writes code like this:
Whilst implementing my first MVVM application in WPF, I've been wondering about the pros
Implementing Ajax requests in my rails 3 app has been one of the most
Implementing ArrayAccess Interface in PHP , We can access Object Properties as Array Keys
I'm implementing the CSS3 flexible box layout module as defined by the W3C, which
I know this has been asked before , but I don't think these solutions
implementing this on MATLAB for my project. I have a circle divided up to
Implementing a custom Dependency Property on a Framework Element object causes my Visual Studio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.