Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect size report after cork_array_ensure_size #157

Open
scossu opened this issue Nov 2, 2019 · 2 comments
Open

Incorrect size report after cork_array_ensure_size #157

scossu opened this issue Nov 2, 2019 · 2 comments

Comments

@scossu
Copy link

scossu commented Nov 2, 2019

To reproduce:

  • Reallocate space in an array with cork_array_ensure_size
  • Assign a value manually to the expanded elements of the array (probably optional but usefult to test the reallocation)
  • Call cork_array_size(array) or array->size (undocumented by the way, but seems to work exactly the same)

Expected result: the number of elements including the ones inserted manually.

Actual result: the number of element prior to expansion.

Example program:

#include <libcork/ds.h>

typedef cork_array(int) Array;

int main()
{
    Array _a;
    Array *a = &_a;

    cork_array_init(a);
    printf("%lu\n", a->size);

    cork_array_append(a, 8);
    printf("%lu\n", a->size);

    cork_array_ensure_size(a, 2);
    cork_array_at(a, 1) = 36;
    printf("array->size: %lu\n", cork_array_size(a));
    printf("cork_array_size: %lu\n", cork_array_size(a));
    printf("At 1: %d\n", cork_array_at(a, 1));

    return(0);
}

Output:

0
1
array->size: 1
cork_array_size: 1
At 1: 36

Is this is the expected behavior?

@scossu scossu changed the title Incorrect size report after cork_array_ensure_size and direct assignment Incorrect size report after cork_array_ensure_size Nov 2, 2019
@scossu
Copy link
Author

scossu commented Nov 2, 2019

I just noticed that I can change a->size in my program and that it affects cork_array_size, still the question remains: while cork_array_append automatically adjusts the size, shall a manual expansion not do so as well?

@dcreager
Copy link
Owner

dcreager commented May 5, 2020

Hi Stefano! Sorry for the delay. This is the intended behavior — cork_array_ensure_size ensures that the capacity of the array is big enough to hold the requested number of elements, but you still have to use something like cork_array_append to add new elements and bump the size field. The documentation sort of mentions it, but I could add some text to make that more explicit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants