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

[Hackathon 2024][Neodyme][C] Avoid passing by value #121

Open
Dryss10 opened this issue May 30, 2024 · 0 comments
Open

[Hackathon 2024][Neodyme][C] Avoid passing by value #121

Dryss10 opened this issue May 30, 2024 · 0 comments
Assignees
Labels
Hackathon 2024 New issues tagged during the hackathon 2024 spotter

Comments

@Dryss10
Copy link

Dryss10 commented May 30, 2024

Rule title

Avoid passing by value.

Language and platform

C

Rule description

Instead of copying strings, arrays, or large structs, consider copying a pointer to them. As long as you're done using the pointer before you modify the string, array, or struct you're okay.

ANSI C now requires that structs are pass-by-value like everything else, thus if you have extraordinarily large structs, or are making millions of function calls on medium-sized ones, you might consider passing the struct's address instead, after modifying the called function so that it doesn't perturb the contents of the struct.

Bad code:

// function which use a copy of the array
static void func(double *arr)
{
    for (int i = 0; i < 100000000; i++) {
        arr[i] += 1;
    }
}
int main(int argc, char *argv[])
{
    // Allocate the memory for the array
    double *arr = malloc(sizeof(double) * 100000000);

    // Initialize the array to 0 
    memset(arr, 0, 100000000);
    // Use of the function
    func(arr);
    free(arr);
    return 0;
}

Good code:

// function which use a copy of the array
static void func(double **arr)
{
    for (int i = 0; i < 100000000; i++) {
        (*arr)[i] += 1;
    }
}

int main(int argc, char *argv[])
{
    // Allocate the memory for the array
    double *arr = malloc(sizeof(double) * 100000000);

    // Initialize the array to 0 
    memset(arr, 0, 100000000);
    // Use of the function
    func(&arr);
    free(arr);
    return 0;
}

Rule short description

Avoid overloading memory unnecessarily.

Rule justification

No concrete documentation.
We carried out tests on the code above and found that the difference in performance between the two programs was almost undetectable.
We would need to develop tests on the cpu and its consumption, or on the memory used by these two programs.

Bad code:

without_pointer

Good code:

with_pointer

Severity / Remediation Cost

Severity: Minor (depends of size of the data)

Remediation: Easy

Remediation consists in using pointers instead of value.

Implementation principle

In a function prototype check the arguments and if the arguments type are arrays or structs, check that it is passed with a pointer instead of its value.

@Dryss10 Dryss10 added Hackathon 2024 New issues tagged during the hackathon 2024 spotter labels May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hackathon 2024 New issues tagged during the hackathon 2024 spotter
Projects
None yet
Development

No branches or pull requests

2 participants