Click here to Skip to main content
12,295,457 members (55,970 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

29.2K views
137 downloads
4 bookmarked
Posted

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.

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)

Share

About the Author

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

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