Question

First of all I want to say that I did search around, but I didn't understand the solutions proposed to the existing questions.

Here is my question.

Array *create()
{
        static Array *arr1;
        void *arraypointer;

        if ((arraypointer = (Array *) malloc(sizeof(Array))) == NULL) {
                printf("Array not created because no memory is available. \n");
        } else {
                arraypointer = arr1;
                printf("Array created successfully. \n");
        }

        return arr1;
}

I am assuming that that is good. Now, I want to add something to the array, so obviously I need to increase the size in memory. At the moment, I have this.

void add(Array S[], Item x)
{
        static Array *arr1;
        void *arraypointer;

        arraypointer = (Array *) malloc(sizeof(Array) + 1);

        if (is_element_of(x, S) == true) {
                printf
                    ("Item already exists in array and therefore it can't be added. \n");
        } else {
                strcpy(S->arr1[S->s].sinput, x.sinput);
                S->arr1[S->s].iinput = x.iinput;
                S->s++;
                printf("Item added successfully. \n");
        }

}

I feel this is not good, although I am not sure how I have to do it. The warning I get is that arr1 and arraypointer are not used in the Add method.

What should I do?

Thanks

P.S. I would appreciate if you keep it simple, as I am still trying to wrap my head around this malloc thing.

Était-ce utile?

La solution

this is different from yours but It feel like the following

typedef int Item;

typedef struct da {
    size_t s;//now size of array
    Item *arr;//dynamic array
} Array;

Array *create(void){
    Array *ap;

    if((ap = malloc(sizeof(Array)))== NULL){
        fprintf(stderr, "Array not created because no memory is available. \n");
    } else {
        fprintf(stderr, "Array created successfully. \n");
        ap->s = 0;
        ap->arr = NULL;
    }
    return ap;
}

bool is_equal(Item x, Item y){
    return x == y;
}

bool is_element_of(Item x, Array *S){
    size_t i;
    for(i = 0; i < S->s ; ++i){
        if(is_equal(x, S->arr[i]))
            return true;
    }
    return false;
}

void add(Array *S, Item x){
    if (is_element_of(x, S) == true){
        fprintf(stderr, "Item already exists in array and therefore it can't be added. \n");
    } else {
        S->arr = realloc(S->arr, (S->s + 1) * sizeof(Item));
        if(S->arr == NULL){
            fprintf(stderr, "Memmory could not allocate.\n");
        } else {
            S->arr[S->s++] = x;
            fprintf(stderr, "Item added successfully. \n");
        }
    }
}

Autres conseils

Once you allocated memory with malloc and resize it afterwards, you'll have to use realloc. Otherwise, if you use malloc again you'll get a whole new array. Furthermore, if you forget to call free, you'll get a memory leak, b/c the "old" array is not freed. With realloc the content of your current array is preserved.

Also have a look at this related question: Differences between using realloc vs. free -> malloc functions

I am assuming that that is good.

No, it's not, sorry. Look at your code carefully: assuming malloc is successfull, first, it assigns a newly allocated memory region to arraypointer (which doesn't really have a reason to be void *, you should make it Array *), and then it assigns arr1 to arraypointer. After that, you just lost reference to the previously allocated block. Thus, your program contains a memory leak.

I don't see the point of using arr1, and I can't understand why you made it static. Why not something like this?

Array * create()
{
    Array *arraypointer;

    if ((arraypointer = malloc(sizeof(*arraypointer))) == NULL) {
        printf("Array not created because no memory is available. \n");
    }  else {
        printf("Array created successfully. \n");
    }
    return arraypointer;
}

The warning that you get inside add() is because you're not really using arr1 nor arraypointer: you're just using S. Nowhere in the function's code you use these variables. I'm guessing you will want to use realloc here, but it is hard to tell, since you didn't show us the structure definition for Array.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top