Dynamically allocated array using command line arguments

Posted on 2003-03-11
Medium Priority
Last Modified: 2010-08-05
I have to implement a Union/Find tree in C++. The size of my parent array is to be generic, and I am supposed to pass in the size of the array on the command line, (argv[]). I have my Union/Find tree in a seperate class, and I am thinking this somehow needs to be done in the constructor.
My question is : How do I dynamically allocate an array of the size specified by the user in the command line?

Thanks in advance for any help
Question by:CPLUSPLUSRookie
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

christopher_smith earned 320 total points
ID: 8113160
maybe like this...

#include <stdlib.h>

void main(int argc, char *argv[ ])
     int intSize = 0;
     int *intDynArray;
     if (argc > 1)

          intSize = atoi(argv[1]);
                //check it is greater than 0 first...
          intDynArray = new int[intSize];

          delete [] intDynArray;



Expert Comment

ID: 9447207
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Simple Linear Regression
Starting up a Project

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question