Click here to Skip to main content
Click here to Skip to main content

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


Sweden Sweden
I am not using Microsoft.NET and Crystal report anymore.

Comments and Discussions

GeneralMy vote of 1 Pinmembersroppel3-Dec-10 9:39 
Generalrequest for your email Pinmemberbahman shafiei29-Sep-10 10:29 
GeneralRe: request for your email PinmemberShahabFatemi1-Oct-10 4:38 
GeneralMy vote of 2 PinmemberAlejandro Xalabarder4-May-10 14:19 
GeneralMy vote of 1 Pinmemberroman30017817-May-09 19:33 
General[Message Deleted] Pinmemberweaponx2007a17-May-09 8:05 
GeneralRe: My vote of 1 PinmemberShahabFatemi23-May-09 0:50 
GeneralMy vote of 1 Pinmembermadicursar16-May-09 10:39 
GeneralRe: My vote of 1 PinmemberShahabFatemi22-May-09 2:08 
GeneralAlways check for NULL when using realloc() PinmemberJörgen Sigvardsson15-May-09 6:49 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 13 May 2009
Article Copyright 2009 by ShahabFatemi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid