Click here to Skip to main content
15,923,120 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Disable popup menu in Macromedia Flash ActiveX Pin
johnnyXP12-Sep-05 2:22
johnnyXP12-Sep-05 2:22 
QuestionPrinter Driver Pin
Ghasrfakhri11-Sep-05 9:27
Ghasrfakhri11-Sep-05 9:27 
Questionhow can I Create an Office plugin? Pin
Ghasrfakhri11-Sep-05 9:24
Ghasrfakhri11-Sep-05 9:24 
AnswerRe: how can I Create an Office plugin? Pin
ThiefXXX11-Sep-05 11:34
ThiefXXX11-Sep-05 11:34 
QuestionWebbrowser Pin
RaymondM11-Sep-05 9:15
RaymondM11-Sep-05 9:15 
QuestionC++ question Pin
Sveta8011-Sep-05 7:44
Sveta8011-Sep-05 7:44 
AnswerRe: C++ question Pin
Nemanja Trifunovic11-Sep-05 7:59
Nemanja Trifunovic11-Sep-05 7:59 
AnswerRe: C++ question Pin
PJ Arends11-Sep-05 9:52
professionalPJ Arends11-Sep-05 9:52 
As has been said previously using std::vector is the best and easiest way of doing this. But you also said you are a beginner starting to learn C++ and perhaps you want to learn how to do it yourself. What you have to do in that case allocate a certain amount of memory, the amount does not matter, and when you use up all that memory you will have to reallocate another larger block, move all your data to the new block, delete the old one, have your original pointer point to the new one and continue on.
#include <iostream>
 
using namespace std;
 
void main()
{
    int *pointer = NULL; // pointer to the array
    int size = 0;        // the number of elements in the array
    int nextindex = 0;   // the next available empty index
    int growby = 10;     // the number of elements to add to our array
                         // the bigger this number, the less reallocations
                         // we have to make.
 
    int number = 0;      
    cin >> number;
    while (number > 0)   // zero or negative number ends loop
    {
        if (nextindex >= size)
        {
            // array full, make it bigger
            cout << "Allocating more memory" << endl;
            int *newpointer = new int[size + growby];
            memset (newpointer, 0, (size + growby) * sizeof(newpointer[0]));
            memmove (newpointer, pointer, size * sizeof(pointer[0]));
            delete[] pointer;
            pointer = newpointer;
            size += growby;
        }
 
        pointer[nextindex++] = number;
 
        cin >> number;
    }
 
    for (int count = 0; count < nextindex; ++count)
    {
        cout << pointer[count] << endl;
    }
 
    delete[] pointer;  // cleanup
}
This is the basic technique used by all dynamic array classes such as std::vector, CArray, etc.




"You're obviously a superstar." - Christian Graus about me - 12 Feb '03

"Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04

"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05

Within you lies the power for good - Use it!
GeneralRe: C++ question Pin
Sveta8011-Sep-05 10:36
Sveta8011-Sep-05 10:36 
GeneralRe: C++ question Pin
Marc Clifton11-Sep-05 12:09
mvaMarc Clifton11-Sep-05 12:09 
GeneralRe: C++ question Pin
PJ Arends11-Sep-05 12:35
professionalPJ Arends11-Sep-05 12:35 
GeneralRe: C++ question Pin
Marc Clifton11-Sep-05 13:48
mvaMarc Clifton11-Sep-05 13:48 
GeneralRe: C++ question Pin
PJ Arends11-Sep-05 19:28
professionalPJ Arends11-Sep-05 19:28 
GeneralRe: C++ question Pin
Christian Graus11-Sep-05 12:46
protectorChristian Graus11-Sep-05 12:46 
GeneralRe: C++ question Pin
Marc Clifton11-Sep-05 13:49
mvaMarc Clifton11-Sep-05 13:49 
GeneralRe: C++ question Pin
Christian Graus11-Sep-05 13:53
protectorChristian Graus11-Sep-05 13:53 
GeneralRe: C++ question Pin
Nemanja Trifunovic12-Sep-05 2:26
Nemanja Trifunovic12-Sep-05 2:26 
GeneralRe: C++ question Pin
Nemanja Trifunovic12-Sep-05 2:30
Nemanja Trifunovic12-Sep-05 2:30 
GeneralC++ question Pin
Sveta8011-Sep-05 7:32
Sveta8011-Sep-05 7:32 
GeneralRe: C++ question Pin
Trollslayer11-Sep-05 7:42
mentorTrollslayer11-Sep-05 7:42 
GeneralRe: C++ question Pin
El Corazon11-Sep-05 7:55
El Corazon11-Sep-05 7:55 
GeneralRe: C++ question Pin
Trollslayer11-Sep-05 13:53
mentorTrollslayer11-Sep-05 13:53 
GeneralRe: C++ question Pin
Blake Miller12-Sep-05 4:38
Blake Miller12-Sep-05 4:38 
GeneralRe: C++ question Pin
Marc Clifton11-Sep-05 8:03
mvaMarc Clifton11-Sep-05 8:03 
GeneralRe: C++ question Pin
DavidNohejl11-Sep-05 8:09
DavidNohejl11-Sep-05 8:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.