Allocating Memory exercise

I'm working on an assignment for my class to write a program which will:
1. Prompt the user for the number of characters in a string (actually a cstring, we haven't learned strings yet)
2. Allocate a string of sufficient length (one more than # of characters)
3. Prompt the user for the string using getline
4. Display the string back to the user
5. Release the memory and for allocation failures

I *think* I have allocated the memory correctly based off what I've seen in videos and tutorials.

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
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

int getSize()
{
	int size;
	cout << "Enter number of characters: ";
	cin >> size;
	
	return size;
}

void getText(char text[], int pSize)
{
	cout << "Enter text: ";
	cin.ignore();
	cin.getline (text, pSize);
}

int main()
{
	int *pSize = new (nothrow) int;
	pSize = new int;
	
	*pSize = getSize();
	
	if (*pSize <= 0)
	{
		cout << "Allocation failure!";
		return 0;
	}
		
	cout << "The size is: " << *pSize;
	
	char text[*pSize];
	text = getText(text, *pSize);
	
	cout << "Text: " << text << endl;

	return 0;
}


The compiler returns this error:
 
[Error] incompatible types in assignment of 'void' to 'char[(((sizetype)(((ssizetype)(*pSize))+ -1)) + 1)]'


I have no idea what that error is supposed to be telling me or how to fix it. Or if I'm even on the right track for that matter for figuring out how to do this. (By the way, my knowledge of pointers is bad at best)
The syntax for dynamic memory allocation is:
type* variableName = new type(constructor arguments here); if you're allocating a single type element, or

type* variablename = new type[how many]; for an array.

To free (deallocate) the memory afterwards, you call
delete variableName; if you just allocated one, or

delete[] variableName; if you allocated an array.

So, to allocate memory for a C string (which is just a char array), you would do
char* myStringName = new char[howManyCharsPlusOneForTheNullTerminator];
Okay, so now my code is looking like:

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
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

int getSize()
{
	int size;
	cout << "Enter number of characters: ";
	cin >> size;
	
	return size;
}

void getText(char text[], int pSize)
{
	cout << "Enter text: ";
	cin.ignore();
	cin.getline (*text, *pSize);
}

int main()
{
	int* pSize = new (nothrow) int;
	pSize = new int;
	
	*pSize = getSize();
	
	if (*pSize <= 0)
	{
		cout << "Allocation failure!";
		return 0;
	}
		
	cout << "The size is: " << *pSize;
	
	char* text = new (nothrow) char[*pSize + 1];
	text = getText(text, pSize);
	
//	cout << "Text: " << text << endl;

	return 0;
}


PS: Im not quite sure exactly why I'm using nothrow except my book is using it. From what I understand is that if an invalid entry is returned, the it sets the value to NULL?

My errors are:
1
2
3
[Error] invalid conversion from 'int*' to 'int' [-fpermissive]
[Error] invalid conversion from 'int*' to 'int' [-fpermissive]
[Error] void value not ignored as it ought to be
You don't need to use dynamic memory for the size variable.
You can just do something like int size = getSize();.

Also, getText doesn't return anything, so text = getText(text, pSize); makes no sense.

For your cin.getline call, the first argument should be just text, not *text (since it needs a pointer).

The nothrow thing just means that if memory allocation fails, it returns a null pointer instead of throwing an exception.
Ok, it looks like I got everything to work :)

I was unable to adjust the size of the memory (I think that's what I'm doing at least) by having size + 1 in the "char* text = new (nothrow) char[size];". I had to do size + 1 within the function call "getText(text, (size + 1));". But either way, I got my code to compile and print out the results I needed to get to get credit for the program :)

Thank you very much for the help!
Topic archived. No new replies allowed.