Hi

Im having a problem when it comes to dynamic memory allocation.I have a program that lets me pick the size of my array,and then input numbers into it.

That all works well,it is when I try to add another int to this array that im having problems with,could anyone shed some light or point me in the right direction..

#include <iostream>

using namespace std;

int main(){
    
    
    int size;
    int* ptr;
    
    cout << "How big is your array going to be?";
    cin >> size;
    
    // Enter numbers into array.
    for(int i = 0; i < size; i++)
    {
        cout << "Enter Number: ";
        cin >> ptr[i];
    }
    
    cout << "Numbers entered into array are: " << endl;
    for(int i = 0; i < size; i++){
        cout << ptr[i] << endl;
    }
    
    // Now i want to add more number to this array.
    int newNum = 3;
    
    *(ptr+4) = newNum;
    
    cout << "New array looks like:"<<endl;
    
     for(int i = 0; i < 4; i++){
        cout << ptr[i] << endl;
    }
    
    
    
    return 0;
}

Recommended Answers

All 6 Replies

int * ptr;
This is a pointer to an int. It isn't an array. It can be used like an array when you assign memory to it using the new[] operator.

int num = 6;
int * ptr = NULL; //null ptr

ptr = &num; //ptr points to num. you can dereference ptr if you want. ptr can not be used like an array with this syntax being used

ptr = new int[10]; //now points to first element in a contiguous section of memory big enough for 10 ints using the default constructor for the int type. ptr can now be used like an array. Now you can not derefernce ptr until you change how it is used back to the previous syntax

delete[] ptr; //releases the memory ptr pointed to. ptr now not pointing at anything. So assign NULL to it unless you are going to quit the program or use it right away again.

ptr = new int; //now ptr acts like an array with capacity of size, whatever size is.

Thanks Lerner I understand hwo to use the new function now :)

But what I am trying to do is add more numbers to the end of my already allocated array.

So for instance if i allocate an Int array[10] and want to add a 11th int,but the memory is full how can i go about this is realloc is not available in c++

#include <iostream>

using namespace std;

int main(){


    int size;

    cout << "How big is your array going to be?";
    cin >> size;
    int* ptr = new int[size];

    // Enter numbers into array.
    for(int i = 0; i < size; i++)
    {
        cout << "Enter Number: ";
        cin >> ptr[i];
    }

    cout << "Numbers entered into array are: " << endl;
    for(int i = 0; i < size; i++){
        cout << ptr[i] << endl;
    }

    // Now i want to add more number to this array.
    int newSize = size + 3;
    int* newPtr = new int[newSize];
    memcpy(newPtr, ptr, size * sizeof(int));
    delete []ptr;
    ptr = newPtr;
    ptr[size] = 99;
    ptr[size + 1] = 100;
    ptr[size + 2] = 199;

    cout << "New array looks like:"<<endl;

     for(int i = 0; i < newSize; i++){
        cout << ptr[i] << endl;
    }

    delete []ptr;

    return 0;
}

Make a new array of larger size and copy the contents from one to another will do the job.

thanks necrolin,why do you have to use size *sizeof(int) for

memcpy(newPtr, ptr, size * sizeof(int));

is this just telling the function how much data it is going to be handling?

EDIT:
Also

#
ptr[size] = 99;
#
ptr[size + 1] = 100;
#
ptr[size + 2] = 199;

When you do size + 1 = 100 is this allocating more memeory to the array?

What I did was this:

a) Create a new array that was larger than the first array.
int newSize = size + 3;
int* newPtr = new int[newSize];

b) Copy the old array into the new array
memcpy(newPtr, ptr, size * sizeof(int));

Basically it's just memcpy(Destination, Original, sizeInBytes)
Since an int is 4 bytes then we need to set it to (# of int * sizeof(int)) to get the right size data to copy.

c) Delete the original ptr and copy the newPtr to ptr
delete []ptr;
ptr = newPtr;

Now ptr is larger by 3 int (because newSize = size + 3). Then I just add 3 random digits to the end. The reason I used size, size + 1, and size + 2 was because I couldn't be sure what size would be originally. You could have chosen to enter 6 digits or 10 digits, right? So, I just did the lazy thing by saying is the first new position in the array, is the next free position, etc. If it were a longer example I would have done this part differently or it would get tedious very quickly.

Edit-Solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.