Hi, i am making a program that contains an integer array of an unknown size.The user is prompted to enter numbers into the array,and press 'x' to exit.

What i am going to do then,is to pass the array from the main into a class which contains another array,but before i do that i wanted to allocate the required space for this array in my class.

Here is what i have done so far(i have made a small test program with a function instead of included class files)

#include <iostream>
#define MAX_SIZE 50

using namespace std;

void Malloc(int _array[]);


  int array[MAX_SIZE] = {};
  char exit;

  // enter numbers into an array
  for(int i = 0; i < MAX_SIZE; i++)
      // if user enters x program will stop input to array.
      if(exit == 'x')

      cin >> array[i];

  for(int i = 0; array[i] != '\0'; i++)
      cout << array[i];



void Malloc(int _array) {

    int size;
    int classArray[];

    // Find the size of the array passed in as an argument.
    for(size = 0; _array[size] != '\0'; size++);

    classArray = (char*)malloc(size+1);

    for(int i = 0; _array[i] != '\0'; i++)
        classArray[i] = _array[i];


Does anyone have any suggestions as to what im doing wrong?Any help is appreciated.

7 Years
Discussion Span
Last Post by VernonDozier

Lines 15 - 24 - This loop is a black hole. Once you get to line 23 the first time, if you get to it, you'll never get out. Well, you'll get out when you go through it MAX_SIZE times, so that's a slight exaggeration, but you won't get out of it any earlier.

Look at line 18. Your intent is clear:stop asking for input when the user enters 'x'. You do this by testing a variable and if it equals 'x', execute the "break" command. Good design, but you are testing the wrong variable. Again, you want to check the user's input. You're getting the user's input from cin via the >> operator here (line 23).

cin >> array[i];

Now look at your test (line 18):

if(exit == 'x')

The exit variable has nothing to do with user input here. It could equal anything (you don't initialize it) and it will never change inside this for-loop. You need to have a test that is based on the actual user input variable. In this case, that's array. You've defined this as integer array, so if 'x' is acceptable input, you should not read the user input into an integer variable. Read input in as a string, then check to see if it is "x". If so, break out of the loop. If not, convert the string to an integer and store it in your array.

Edited by VernonDozier: n/a


ah thank you,i am going to change it to enter -1 for the break.I am still unsure as to how to memory allocate by passing the array in to the function i have made,iit possible what i am trying to do?


I've never seen this syntax before:

char array[MAX_SIZE] = {};

Does that initialize all of the elements to zero? If so, I've just learned something new. I've never seen '\0' used to compare an integer. '\0' is just 0, right? That means that you can't enter the integer 0 and have it interpreted as 0. That's fine if you know that the array should never contain a 0 element. So when you input the numbers, you might as well skip having the person enter an 'x' and just have them enter a 0 instead. If 0 IS a legitimate array element, then you'll need a different method.

Delete the brackets in line 31 in the function call. Function calls passing arrays don't include empty brackets.

Line 38 is illegal. You can either have an array declared with brackets with a KNOWN size or you can declare it as a pointer, but you'll get a compile error the way you have it.

Line45 - you have classArray as an integer array on line 38, but you're typecasting to a char* on line 45. What type is this array storing?


Yes i think it does,i just made it up myself :D

That main question i need help with is this:

How can i create an array of integers of an unknown size,and pass it into an array in my class.But before doing so,allocate that size of memory to the class array?


Yes i think it does,i just made it up myself :D

I'd confirm this if I were you. Until you've verified that it will work on all compilers, assume that it won't. Maybe it does. I've never seen this. I've always set up a loop and gone through the elements one by one or used memset or used calloc.

That main question i need help with is this:

How can i create an array of integers of an unknown size,and pass it into an array in my class.But before doing so,allocate that size of memory to the class array?

You don't create an array of an unknown size. You can

  1. Reserve memory for a maximum size, more than you'll ever need.
  2. Use your "best guess" for the size, then resize and/or do deep copies if/when you're wrong about the size.
  3. Use a vector that does all of this for you.

Regarding copying it to an array in your class, by the time the user enters all the input in your example, the size IS known, so use it. Decide whether you need a deep or shallow copy. If it's a shallow copy, just reassign the pointer in your class array to the pointer that holds the data that the user just entered. If it's a deep copy, reserve that exact (now known) array size with the "new" command (or with malloc if you prefer) and do a deep copy for-loop as you do for the deep copy.

But right now your code has too many syntax errors to compile and you're naming your function "Malloc", but it really has nothing to do with malloc, so it's confusing. malloc takes the number of bytes requested to reserve as memoryas a parameter. It does not take an array and it does not do anything with the memory, and it definitely doe not make a deep copy of the array contents.

From the link below:

The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.