Click here to Skip to main content
12,818,819 members (28,463 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 13 May 2009

How to resize an array of data in C/C++

, 13 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
A resizable stack of data is made in C++ and the allocated size of memory of the stack is changed at runtime.


Changing the size of arrays at runtime in C++ is demanding. In this simple code, we will learn how to change the size of an array of data at runtime. In this order, a structure of data is designed and a pointer is used to point to the allocated part of memory to store the data in the form of a stack, and the size of the allocated memory is changed while we push and pop data into the stack.


I have assumed that you know what a stack is and how it works.

Using the code

First of all, a structure of data is designed as follows:

// A structure of data
struct dataStruct
    int iNumber;    // integer number
    char chChar;    // character
    char* str;    // string

This is just a simple structure. You can change it to whatever you are interested in.

Then, a pointer with the structure data type is defined.

dataStruct * objStruct;

It is crucial to set the pointer to NULL at the beginning. It is done in my code in the class constructor.

objStruct     = NULL;

Next, you can change the size of the allocated memory of your pointer at any time you need.

objStruct = (dataStruct*) realloc (objStruct, 
             iStackCounter * sizeof(dataStruct));

In the line above, iStackCounter stores the number of stored items in the stack. Therefore, the allocated size for the memory should be a multiplication of the number of items and the size of the structure. You can call this line any time you need.

The rest of the code is the basic storing of data in a stack, and finally in the main function, by calling push and pop functions, you can put and fetch data from your stack. While you put a new item into the stack, the size of memory is changed automatically and you do not need to worry about the size of your stack. The allocated size is decreased when you pop data from the stack until the stack becomes empty.


You might see compiler error(s) when you want to run the attached code. First, check the header files. I have used GCC compiler in UBUNTU and the header names might be different than yours.

Hope you find this useful.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
I am not using Microsoft.NET and Crystal report anymore.
I moved from computer science to space science in 2008. Currently I'm a postdoc researcher at UC Berkeley. I apologize if I cannot reply your mails/messages anymore. This is because I am not using Windows and any of the Microsoft products.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
sroppel3-Dec-10 9:39
membersroppel3-Dec-10 9:39 
Generalrequest for your email Pin
bahman shafiei29-Sep-10 10:29
memberbahman shafiei29-Sep-10 10:29 
GeneralRe: request for your email Pin
ShahabFatemi1-Oct-10 4:38
memberShahabFatemi1-Oct-10 4:38 
GeneralMy vote of 2 Pin
Alejandro Xalabarder4-May-10 14:19
memberAlejandro Xalabarder4-May-10 14:19 
GeneralMy vote of 1 Pin
roman30017817-May-09 19:33
memberroman30017817-May-09 19:33 
General[Message Deleted] Pin
weaponx2007a17-May-09 8:05
memberweaponx2007a17-May-09 8:05 
GeneralRe: My vote of 1 Pin
ShahabFatemi23-May-09 0:50
memberShahabFatemi23-May-09 0:50 
GeneralMy vote of 1 Pin
madicursar16-May-09 10:39
membermadicursar16-May-09 10:39 
GeneralRe: My vote of 1 Pin
ShahabFatemi22-May-09 2:08
memberShahabFatemi22-May-09 2:08 
GeneralAlways check for NULL when using realloc() Pin
Jörgen Sigvardsson15-May-09 6:49
memberJörgen Sigvardsson15-May-09 6:49 
GeneralMy vote of 1 Pin
Nenad Caklovic13-May-09 23:20
memberNenad Caklovic13-May-09 23:20 
GeneralRe: My vote of 1 Pin
ShahabFatemi14-May-09 0:19
memberShahabFatemi14-May-09 0:19 
GeneralMy vote of 1 Pin
Strempel13-May-09 9:33
memberStrempel13-May-09 9:33 
GeneralRe: My vote of 1 Pin
ShahabFatemi13-May-09 11:39
memberShahabFatemi13-May-09 11:39 
GeneralRe: My vote of 1 Pin
ShahabFatemi13-May-09 11:41
memberShahabFatemi13-May-09 11:41 
GeneralRe: My vote of 1 Pin
Manish K. Agarwal13-May-09 23:14
memberManish K. Agarwal13-May-09 23:14 
GeneralRe: My vote of 1 Pin
Mike Diack14-May-09 4:41
memberMike Diack14-May-09 4:41 
GeneralRe: My vote of 1 Pin
Manish K. Agarwal14-May-09 20:27
memberManish K. Agarwal14-May-09 20:27 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 13 May 2009
Article Copyright 2009 by ShahabFatemi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid