Help using array with dynamic size

I'm new to arrays with dynamic sizes. That being said, I'm working with a simple code which seems to work just fine, my only concern is that once I display the 'char array', not only displays the user's inputs but some extra data, symbols and what not.

Can someone explain why, if to my understanding the first user's input already sets the size of the array, and how can this be fixed?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <iomanip>
using namespace std;
 
int main()
 
{ 



int size,x;
char a;
cout << "Please enter the number of pin numbers: "<<endl;
cin >> size;

char *array=new char[size];
array[0]='\n';
if(array)    // Make sure it allocated memory
{
	x=0;
	for(int i=0;i<5;i++)
        {
	cout<<"Enter"<<endl;
	cin>>a;
	array[x]=a;
	cout<<array<<endl;
	x++;
	}
	
	
   delete [] array;
}
'cout' has no way to know how big the array is. You're just giving it a chunk of data and saying "print this".

It determines where to stop printing by looking for a special "null terminator". Which basically is the literal value of zero. This marks the end of a C-style string (a char array).

This also means your array has to be 1 larger than the desired size, because you need to have that extra 1 char to hold the null terminator.

1
2
3
4
5
6
7
8
9
10
11
12
13
char foo[5];

foo[0] = 'a';
foo[1] = 'b';
foo[2] = 'c';
foo[3] = 'd';
 // no null terminator

cout << foo;  // prints "abcd" possibly followed by a bunch of other garbage

foo[4] = 0;  // the null terminator

cout << foo;  // prints "abcd" cleanly.  End of string is now marked 



Of course this would be much easier if you use actual strings instead of char arrays. But I'm going to assume you're doing it the hard way because this is a homework assignment or something.
So in arrays there is no way to input a "null terminator"?
I want to use a dynamic size array because of the fact that its size may very whenever I call this function (not a function now but planning on making it one)
There is a way to put the null terminator in the array. It is '\0', a character with the numeric value of 0, as Disch showed on line 11 of his example.
@Disch

I'm actually surprised this is not done automatically.

E: Yes it is done automatically

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
#include <new>
using namespace std;

int main()
{
    int size,x;
    char a;
    cout << "Please enter the number of pin numbers: "<<endl;
    cin >> size;
    
    char *array=new (nothrow) char[size];
    if(array)//<---If you are gonna use this, then use the nothrow method when creating the array
    {
        array[0]='\n';
        for(int i=1;i<size;i++)
        {
            cout<<"Enter\n";
            cin>>a;
            array[i]=a;
            cout<<array<<endl;
        }
        
        
        delete [] array;
    }
}
Last edited on
got it! that null terminator had me stuck. THANK YOU BOTH very much
@Smac89
using your code I'm still getting does random characters when the array is displayed. How would you fix that?
Last edited on
That's weird, I guess do exactly what was suggested by Disch so on line 17 above the for-loop, specify array[size] = 0

E:
Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>
#include <new>

using std::cin;
using std::cout;
using std::endl;
using std::nothrow;

int main()
{
    int size;
    char a;
    cout << "Please enter the number of pin numbers: "<<endl;
    cin >> size;
    
    char *array=new (nothrow) char[size+1];
    if(array)//<---If you are gonna use this, then use the nothrow method when creating the array
    {
        array[0]='\n';
        array[size+1] = 0;
        for(int i=1;i<size + 1;i++)
        {
            cout<<"\nEnter\n";
            cin>>a;
            array[i]=a;
            cout<<array<<endl;
        }
        
        
        delete [] array;
    }
}
$ ./temp
Please enter the number of pin numbers: 
6

Enter
r

r

Enter
u

ru

Enter
s

rus

Enter
s

russ

Enter
i

russi

Enter
a

russia
Last edited on
array[size] is out of bounds.

You should do array[size-1] = 0;


But again... you can always just use strings and then you don't have to deal with this junk.
@Disch

can strings have dynamic sizes too?
would it be the same process done for the char type array?
something like string array= new string[size]??
You don't even need to dynamically allocate for a string; it handles all that for you. Just assign to it and it will handle the rest.
I'm a little confused , I thought every time a string is declared it has to be followed by its size in square brackets, like so string a[50] variable 'a' type string with 50 parameters.

Can anyone provide an example?
An individual string can hold any number of characters.

1
2
3
4
5
6
7
8
9
string a = "This is a string.";
a += " I'm adding to the end of the string.";
a += " More data at the end of the string.";

cout << a;  // prints:
// This is a string. I'm adding to the end of the string. More data at the end of the string."

// you can also easily determine the length of the string:
cout << a.length(); // prints the length of the string. 


Plus you don't have to worry about deleting / cleaning up memory... or null terminators... or exceeding your array size... etc.



string a[50]; This gives you an array of 50 strings. This is perfectly legal to do, but if all you need is one string, it's entirely unnecessary.
Topic archived. No new replies allowed.