简体   繁体   中英

How to correctly use malloc in C?

I'm trying to allocate some memory using malloc in my program (I don't have much experience with malloc as I am just starting to learn how to use it). The program I've created works but I don't think I've used malloc correctly and I want to learn how to use it correctly.

The basic idea of my program is it takes 3 lines of input. The first line being whether you want to sort it by odd or even, the second line being how large your array is and the third line being the integers in the array.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* sort;
int n;
int* ar;
int i;

void test()

{
    int temp;
    int j = 1;

    if (strcmp(sort, "odd") == 0) {

        for (i = 0; i < n;) {

            if (j != n) {

                if (ar[i] % 2 != 0) {

                    if (ar[j] % 2 != 0) {

                        if (ar[j] < ar[i]) {

                            temp = ar[i];
                            ar[i] = ar[j];
                            ar[j] = temp;
                            j++;
                        }
                        else {
                            j++;
                        }
                    }
                    else {

                        j++;
                    }
                }
                else {
                    j++;
                    i++;
                }
            }
            else {

                i++;
                j = i + 1;
            }
        }
    }

    if (strcmp(sort, "even") == 0) {
        for (i = 0; i < n; i++) {

            if (j != n) {

                if (ar[i] % 2 == 0) {

                    if (ar[j] % 2 == 0) {

                        if (ar[j] < ar[i]) {

                            temp = ar[i];
                            ar[i] = ar[j];
                            ar[j] = temp;
                            j++;
                        }
                        else {
                            j++;
                        }
                    }
                    else {

                        j++;
                    }
                }
                else {
                    j++;
                    i++;
                }
            }
            else {

                i++;
                j = i + 1;
            }
        }
    }
}

void main()
{

    ar = malloc(sizeof(int) * 10);
    sort = malloc(sizeof(char) + 5);

    printf("Enter odd or even\n");
    scanf("%s", sort);

    printf("Enter the size of the array \n");
    scanf("%d", &n);

    printf("Enter the elements of the array \n");
    for (i = 0; i < n; i++) {

        scanf("%d", &ar[i]);
    }

    test();

    for (i = 0; i < n; i++) {

        printf("%d ", ar[i]);
    }
}

You need to wait until they tell you the size of the array, and then use malloc() to allocate an array that size.

You don't need to use malloc for sort , you can just declare that as an ordinary array. In general, you use malloc() either when you need to allocate a dynamic number of items, or when the size of the item is dynamic, you don't need it for a single item with a fixed size.

int *ar;
char sort[5];

void main()
{    
    printf("Enter odd or even\n");
    scanf("%s", sort);

    printf("Enter the size of the array \n");
    scanf("%d", &n);
    ar = malloc(n * sizeof(int));

    printf("Enter the elements of the array \n");
    for (i = 0; i < n; i++) {
        scanf("%d", &ar[i]);
    }

    test();

    for (i = 0; i < n; i++) {
        printf("%d ", ar[i]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM