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

refactor cvector_at #75

Merged
merged 2 commits into from
Dec 23, 2024
Merged

refactor cvector_at #75

merged 2 commits into from
Dec 23, 2024

Conversation

GoodenoughPhysicsLab
Copy link
Contributor

@GoodenoughPhysicsLab GoodenoughPhysicsLab commented Dec 22, 2024

After I closed my issue, I suddenly had an idea.

Now, cvector_at can be used as follow:

#include "cvector.h"
#include <stdio.h>
#include <assert.h>

int main(void) {
    cvector(int) v = NULL;
    cvector_push_back(v, 1);

    printf("%d\n", cvector_at(v, 0)); // get value
    cvector_at(v, 0) = 2; // set value
    printf("%d\n", cvector_at(v, 0));
    // printf("%d\n", cvector_at(v, 1)); // assert fail

    return 0;
}

@eteran
Copy link
Owner

eteran commented Dec 22, 2024

Hmm, interesting usage of assert and comma operator there. I'll take a closer look in a little while

@GoodenoughPhysicsLab GoodenoughPhysicsLab force-pushed the master branch 2 times, most recently from 58806ed to c215566 Compare December 23, 2024 01:55
1. `cvector_at` return element itself intead its address
2. `cvector_front` and `cvector_back` will no longer NULL
@GoodenoughPhysicsLab
Copy link
Contributor Author

GoodenoughPhysicsLab commented Dec 23, 2024

I have fixed all the test(including some bug of test e.g. calloc(sizeof(struct data_t), 1)
image

@eteran
Copy link
Owner

eteran commented Dec 23, 2024

Can you explain what you believe was broken about:

struct data_t *data = calloc(sizeof(struct data_t), 1);

vs what you changed it to:

struct data_t *data = calloc(1, sizeof(struct data_t));

Given that the nmem and size parameters are basically just multiplied (and checked for overflow), this should make no difference. I don't really mind the change, I was just curious about the reasoning.

@eteran
Copy link
Owner

eteran commented Dec 23, 2024

Also, it appears that your PR requires adding an #include to the cvector.h file.

@GoodenoughPhysicsLab
Copy link
Contributor Author

GoodenoughPhysicsLab commented Dec 23, 2024

Can you explain what you believe was broken about:

The compiler gives me warning.

void *__cdecl calloc(size_t _NumOfElements,size_t _SizeOfElements);

@eteran eteran merged commit 027300e into eteran:master Dec 23, 2024
5 checks passed
@eteran
Copy link
Owner

eteran commented Dec 23, 2024

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants