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
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.

Introduction

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.

Background

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.

Attention

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.

License

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

About the Author

ShahabFatemi

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

Comments and Discussions

 
GeneralMy vote of 1 Pinmembersroppel3-Dec-10 8:39 
Generalrequest for your email Pinmemberbahman shafiei29-Sep-10 9:29 
GeneralRe: request for your email PinmemberShahabFatemi1-Oct-10 3:38 
GeneralMy vote of 2 PinmemberAlejandro Xalabarder4-May-10 13:19 
GeneralMy vote of 1 Pinmemberroman30017817-May-09 18:33 
General[Message Deleted] Pinmemberweaponx2007a17-May-09 7:05 
GeneralRe: My vote of 1 PinmemberShahabFatemi22-May-09 23:50 
GeneralMy vote of 1 Pinmembermadicursar16-May-09 9:39 
GeneralRe: My vote of 1 PinmemberShahabFatemi22-May-09 1:08 
GeneralAlways check for NULL when using realloc() PinmemberJörgen Sigvardsson15-May-09 5:49 
GeneralMy vote of 1 PinmemberNenad Caklovic13-May-09 22:20 
GeneralRe: My vote of 1 PinmemberShahabFatemi13-May-09 23:19 
GeneralMy vote of 1 PinmemberStrempel13-May-09 8:33 
GeneralRe: My vote of 1 PinmemberShahabFatemi13-May-09 10:39 
GeneralRe: My vote of 1 PinmemberShahabFatemi13-May-09 10:41 
GeneralRe: My vote of 1 PinmemberManish K. Agarwal13-May-09 22:14 
GeneralRe: My vote of 1 PinmemberMike Diack14-May-09 3:41 
GeneralRe: My vote of 1 PinmemberManish K. Agarwal14-May-09 19:27 

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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 13 May 2009
Article Copyright 2009 by ShahabFatemi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid