0

So My mission is to keep accepting numbers from users until the user input a negative number. My algorithm should be: 1)start in the size of 2. 2)double the size every time it reaches the end, free the older one. 3) stop when user hits negative number.

**I know I haven't used free and didn't check if the allocation is successful, I'm trying to keep the code as clean as possible for you guys. Please help.

#include <stdio.h>
#include <stdlib.h>
int *reallocate(int* numbers,int i,int arr[]);
int main() {
    int numbers[2];
    int *nums = numbers;
    int i = 0;
    int size = 2;
    while (i<size)
    {
        scanf("%d", (nums+i));
        if (*(nums + i) <0)
            break;
        i++;
        if (i == size) {
        nums=reallocate(nums,i,numbers);
        size = size * 2;
        }
    }
    puts("Stop");
    return 0;
}
int *reallocate(int* numbers,int i, int arr[]) {
    int newsize = 0;
    newsize =i * 2 * sizeof(int);
    numbers = (int *)realloc(arr,newsize);
    return numbers;
}
6
  • what is live dynamic memory allocation??? Commented Dec 27, 2015 at 19:37
  • You can't call realloc on an array you allocated statically to begin with. Commented Dec 27, 2015 at 19:39
  • Mehh I had a feeling. Any solution offerd ? :) Commented Dec 27, 2015 at 19:40
  • Well, by saying live allocation, I mean that it's "on demand". As long as the user keeps entering numbers it keeps growing. Commented Dec 27, 2015 at 19:46
  • @IsanRivkin you should allocate memory for array dynamically, and then reallocate it on demand. Commented Dec 27, 2015 at 19:50

1 Answer 1

1

You should use malloc'ed arrays only with realloc
Here is code:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *nums;
    size_t size = 2;
    nums = malloc(size * sizeof(int));
    size_t i = 0;

    while (i < size && nums != NULL) {
        scanf("%d", (nums+i));
        if (nums[i] < 0)
            break;
        i++;
        if (i == size) {
            size *= 2;
            nums = realloc(nums, size * sizeof(int));
        }
    }

    if (nums == NULL) {
        puts("Error");
        return 1;
    } else {
        free(nums);
        puts("Stop");
        return 0;
    }
}
Sign up to request clarification or add additional context in comments.

15 Comments

It works great thanks ! I got the wrong idea of making a static array and pointing at it, thanks! *Small correction, you forgot to add sizeof(int) in the realloc, just for the people who read here, but it works perfect!
Why are you casting malloc? By the way your code has memory leaks.
@Michi Can you please show me where? And about casting malloc, do you have any ideas how to do this in C, not C++ (I mean no vector)?
@stek29 I really don't understand your Question. Any way there is no need to cast malloc in C, because of its return type which is VOID*. If you ask that, then you probably don't know that. Maybe because you are used with C++? or maybe because you are using a wrong compiler
@stek29 Where malloc takes place, free should be present too. You should know that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.