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;
}
Isan Rivkin
  • 177
  • 1
  • 15

1 Answers1

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;
    }
}
stek29
  • 395
  • 4
  • 14
  • 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! – Isan Rivkin Dec 27 '15 at 19:51
  • Why are you casting `malloc`? By the way your code has memory leaks. – Michi Dec 27 '15 at 20:04
  • @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 Dec 27 '15 at 20:50
  • @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 – Michi Dec 27 '15 at 20:53
  • @stek29 Where `malloc` takes place, `free` should be present too. You should know that. – Michi Dec 27 '15 at 20:54
  • @Michi you mean `free` before `return`? – stek29 Dec 27 '15 at 20:58
  • @Michi I'm not used to C++ at all. I've not understand you, but now I did, I google'd and I'm surprised. Now I'll not cast `void *`. – stek29 Dec 27 '15 at 21:08
  • Try to understand [This](http://pastebin.com/raw/RU69HK3r) and then [This](http://pastebin.com/raw/VcuLgkk9) – Michi Dec 27 '15 at 21:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99073/discussion-between-michi-and-stek29). – Michi Dec 27 '15 at 21:09
  • return type of sizeof is `long unsigned int` this means that `size` and `i` shouldn't be `int` – Michi Dec 27 '15 at 21:10
  • @stek29 Try to understand [this](http://stackoverflow.com/questions/33655093/array-with-undefined-length-in-c/33656480#33656480). – Michi Dec 27 '15 at 21:22
  • @Michi Aren't we in chat? – stek29 Dec 27 '15 at 21:26
  • @Michi should't we check that `nums` isn't `NULL` after `realloc`? – stek29 Dec 27 '15 at 22:09
  • @stek29 Please get a book. I really don't have time for this. – Michi Dec 27 '15 at 22:14
  • @Michi Ok, I'll. Thanks anyway – stek29 Dec 27 '15 at 22:19