0

I've got a loop that looks for prime numbers in an interval of numbers. What I want to do is place the prime numbers in a dynamical array, hence its starting length is 1, and when I find the second number I want to reallocate memory to make the array able to hold 2 numbers instead of one and so on. The code I have looks like this:

int *arr = new int[1];

int count = 0;
for(int i = 0; i <= x; i++) // (x is a value given by the user)
{
    if (is_prime(i))
    {
        realloc(arr,(sizeof(int)*(count + 1)));
        count++;
        arr[count] = i;
    }
}

I'm sure I'm supposed to use the realloc function to do this but the above code doesn't work and I'm not used to memory management while programming.

NOTE: I am aware of vectors, I want to do this with a dynamical array.

4
  • 3
    Never mix new/delete with malloc/free/realloc Commented Jan 28, 2014 at 21:05
  • 1
    If you must use realloc you should be coding in C and not C++. Commented Jan 28, 2014 at 21:06
  • Just note - (re)allocation is very expensive operation. Rather alloc not for every number, but create some expanding buffer. Commented Jan 28, 2014 at 21:06
  • @Yossarian allocation is not very expensive, but copying is and the clue about reallocation is that you dont have to copy every time but just if its not possible to expand the already allocated memory. Commented Jan 28, 2014 at 21:10

2 Answers 2

3

realloc doesnt modify the pointer being passed to it, but returns the new pointer:

arr = (int*) realloc(arr,(sizeof(int)*(count + 1)));

Notice that you should also check whether an error occured:

if(arr == NULL)
    break; // or try something else
Sign up to request clarification or add additional context in comments.

7 Comments

BTW the OP is using new to allocate the buffer.
@Paranaix indeed! the other two issues whacked me in the face and I didn't spot that one!
So what realloc does is freeing up the previously used memory, and returns a pointer to the new allocated memory? if so, I also get this error saying I cant assignt type void* to int* even thought arr is defined as int*, any idea what that is about?
@Jacco yes in c++ you have to explicitely cast a void*
realloc() may return the old pointer or a new pointer depending on whether it was able to increase the size in place, or needed to copy the contents to a freshly allocated piece of memory in a different location. It may also return NULL if it wasn't able to allocate new memory, at which point you just lost the pointer to the original memory. The usual way to handle this is to store the return value in a temporary variable, and test that before deciding what to do with arr.
|
3

First, your array indexing is wrong, so you're writing beyond your array

change this:

count++;
arr[count] = i;

To this

arr[count++] = i;

Second, and I think equally important, you're mixing new and realloc. Don't! Use malloc/realloc/free, or new/delete. Because of the useful way realloc works, you can replace the initial new with int *arr = NULL;

Third, as Paranaix says, you're not assigning the result from realloc back to arr.

2 Comments

alright, is there a differense between incrementing count first and incrementing inside the array brackets?
@Jacco i++ is post increment, that means that i++ returns the old value and than increments, e.g it could be implemented like this: int operator++(int& var) { int temp = var; var += 1; return temp; }

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.