qlibc icon indicating copy to clipboard operation
qlibc copied to clipboard

possible bug in qvector_resize

Open darmar-lt opened this issue 8 years ago • 10 comments

Hi, I think I found a small bug in qvector container in function qvector_resize(). If the argument 'newmax' is smaller than the current size of container 'num', then the 'num' should be changed to 'newmax' too. I think the code should be added after 768 line:

if (vector->num > newmax) {
    vector->num = newmax;
}

darmar-lt avatar Jan 25 '17 07:01 darmar-lt

No, the num represents the total number of objects stored in the vector. When the resize happen to be smaller size than the stored number of objects, it allows to keep the existing objects but will not allow to insert more objects there until num gets smaller than the max.

wolkykim avatar Jan 25 '17 10:01 wolkykim

It seems I do not understand how realloc works. In a documentation of realloc, I read: "The content of the memory block is preserved up to the lesser of the new and old sizes". I interpret this, that the existing objects outside of reallocated space are not preserved.

darmar-lt avatar Jan 25 '17 12:01 darmar-lt

Hey, this is qlibc vector implementaion. Nothing to refer how realloc works.

On Jan 25, 2017 4:50 AM, "darmar-lt" [email protected] wrote:

It seems I do not understand how realloc works. In a documentation of realloc, I read: "The content of the memory block is preserved up to the lesser of the new and old sizes". I interpret this, that the existing objects outside of reallocated space are not preserved.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/wolkykim/qlibc/issues/58#issuecomment-275100206, or mute the thread https://github.com/notifications/unsubscribe-auth/AGioMZQ7xWHIUM1pFYBgORNDwOj_CjKJks5rV0UfgaJpZM4LtLi8 .

wolkykim avatar Jan 25 '17 12:01 wolkykim

I have made a simple test case. Maybe it will help to understand the problem:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "qlibc.h"

int main(void) {

    size_t i;
    size_t siz = sizeof(i);
    qvector_t *vec = qvector(10, siz, QVECTOR_RESIZE_LINEAR);

    for (i=0; i < 5; i++) {
        vec->addlast(vec, &i);
    }

    printf("Number of elements = %zu\n", vec->size(vec));
    for (i=0; i < vec->size(vec); i++) {
        printf("vec[%zu]=%zu\n", i, *(size_t*) vec->getat(vec, i, false));
    }

    vec->resize(vec, 3);
    printf("New number of elements = %zu\n", vec->size(vec));

    for (i=0; i < vec->size(vec); i++) {
        printf("vec[%zu]=%zu\n", i, *(size_t*) vec->getat(vec, i, false));
    }
    vec->free(vec);
    return 0;
}

On my PC, I have output (note, the last two lines are with wrong values):

Number of elements = 5
vec[0]=0
vec[1]=1
vec[2]=2
vec[3]=3
vec[4]=4
New number of elements = 5
vec[0]=0
vec[1]=1
vec[2]=2
vec[3]=65
vec[4]=0

darmar-lt avatar Jan 25 '17 14:01 darmar-lt

Oh you're right. My bad. I was thinking about other implementation. Yes, it does realloc thus the num need to be set correctly when it shrinks. Would you want to send a PR for the fix?

On Wed, Jan 25, 2017 at 6:33 AM, darmar-lt [email protected] wrote:

I have made a simple test case. Maybe it will help to understand the problem:

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <stdint.h> #include <string.h> #include "qlibc.h"

int main(void) {

size_t i;
size_t siz = sizeof(i);
qvector_t *vec = qvector(10, siz, QVECTOR_RESIZE_LINEAR);

for (i=0; i < 5; i++) {
    vec->addlast(vec, &i);
}

printf("Number of elements = %zu\n", vec->size(vec));
for (i=0; i < vec->size(vec); i++) {
    printf("vec[%zu]=%zu\n", i, *(size_t*) vec->getat(vec, i, false));
}

vec->resize(vec, 3);
printf("New number of elements = %zu\n", vec->size(vec));

for (i=0; i < vec->size(vec); i++) {
    printf("vec[%zu]=%zu\n", i, *(size_t*) vec->getat(vec, i, false));
}
vec->free(vec);
return 0;

}

On my PC, I have output (note, the last two lines are with wrong values):

Number of elements = 5 vec[0]=0 vec[1]=1 vec[2]=2 vec[3]=3 vec[4]=4 New number of elements = 5 vec[0]=0 vec[1]=1 vec[2]=2 vec[3]=65 vec[4]=0

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/wolkykim/qlibc/issues/58#issuecomment-275122799, or mute the thread https://github.com/notifications/unsubscribe-auth/AGioMag91UjBNxasf1AJHR1BUW32voKXks5rV109gaJpZM4LtLi8 .

wolkykim avatar Jan 25 '17 15:01 wolkykim

Better not.

darmar-lt avatar Jan 25 '17 15:01 darmar-lt

Why not? We need add thag unit test too. You really don't want to handle it yourself?

On Jan 25, 2017 7:44 AM, "darmar-lt" [email protected] wrote:

Better not.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/wolkykim/qlibc/issues/58#issuecomment-275143088, or mute the thread https://github.com/notifications/unsubscribe-auth/AGioMZmK5bMWZZhKdPMGklZzKkFq84cwks5rV23ogaJpZM4LtLi8 .

wolkykim avatar Jan 25 '17 15:01 wolkykim

Simply I am new on GitHub. But OK. I will try to prepare the pull request. However, it may take time until I understand the required steps.

By the way, I tried to compile qlibc under Windows using CMake. Without success. Is it possible to compile qlibc on Windows?

darmar-lt avatar Jan 25 '17 16:01 darmar-lt

quick steps here: https://help.github.com/articles/creating-a-pull-request-from-a-fork/

About windows, cygwin env should be working but I've never tried it myself.

On Wed, Jan 25, 2017 at 8:17 AM, darmar-lt [email protected] wrote:

Simply I am new on GitHub. But OK. I will try to prepare the pull request. However, it may take time until I understand the required steps.

By the way, I tried to compile qlibc under Windows using CMake. Without success. Is it possible to compile qlibc on Windows?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/wolkykim/qlibc/issues/58#issuecomment-275153230, or mute the thread https://github.com/notifications/unsubscribe-auth/AGioMamokulnyCJPoD39xgXNRZlKa90oks5rV3WxgaJpZM4LtLi8 .

wolkykim avatar Jan 25 '17 16:01 wolkykim

@darmar-lt let us know if you need any help with the pull request :)

levidurfee avatar Jan 25 '17 19:01 levidurfee

not hearing back so long, closing

wolkykim avatar Sep 11 '22 08:09 wolkykim