Invalid allocation size: 4294967295 bytes.

Hi everyone, I'm new here as you might have noticed. I've tinkered with programming a few times in the past but always stopped for one reason or another before I really learned what I was doing. Well I'm back in it and have been trying to figure out how pointers and dynamic memory work. I wrote up a simple program and finally gained some primitive grasp on it. I started adding to the program though and ended up with what's below.

I give it an input, n, and it gives me the summation of 1 + 2 + ... + n, along with 1^2 + 2^2 + ... + n^2 and 1^3 + 2^3 + ... + n^3. It then asks if I want to input another number or not. 'Y' or 'y' cause it ask for another input while 'N' or 'n' close the program.

The good news is that it works exactly how I want. The bad news is that it only works for positive numbers and zero. Any negative number gives me the error in my title: "Invalid allocation size: 4294967295 bytes." I imagine it is some issue concerning my use of dynamic memory but I don't really know. Any suggestions concerning my code would be useful, even if it doesn't solve my problem. I can use all the help I can get.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	int x;
	int y = 0;
	int i;
	char a = 'y';

	while((a != 'n')&&(a != 'N'))
	{
		cout << "Input: ";

		cin >> x;

		int* z = new int[x];

		for (int n = 1; n <= x; n++)
		{
			z[n] = n;
			y += z[n];
		}

		cout << "Summation of x: " << y << "\n";
		y = 0;

		for (int n = 1; n <= x; n++)
		{
			z[n] = n*n;
			y += z[n];
		}

		cout << "Summation of x squared: " << y << "\n";
		y = 0;

		for (int n = 1; n <= x; n++)
		{
			z[n] = n*n*n;
			y += z[n];
		}

		cout << "Summation of x cubed: " << y << "\n";

		cout << "Enter another number Y/N:";
		a = getch();
		while((a != 'Y')&&(a != 'y')&&(a != 'N')&&(a != 'n'))
		{
			a = getch();
		}
		cout << endl;
	}
	return 0;
}


P.S. I just started programming again yesterday and this was done in the last 2 or 3 hours with the help of a lot of research so sorry if it isn't very pretty.
The bad news is that it only works for positive numbers and zero. Any negative number gives me the error in my title: "Invalid allocation size: 4294967295 bytes." I imagine it is some issue concerning my use of dynamic memory but I don't really know.


You hit the nail on the head.

You can't allocate a negative size because that doesn't make any sense. So what's happening is your negative number is being interpreted as an unsigned number. Basically the value 0xFFFFFFFF can be either "-1" (if signed) or "4294967295" (if unsigned).

You're giving new[] a value of -1, and it's interpreting it as 4294967295.
closed account (4Gb4jE8b)
he's right, though it doesn't seem he gave you an actual way of fixing it, and sadly i don't know a way to fix it in this case either.

Though i do have a suggestion, you're going to want to accommodate for the idiot who tries to enter letters (like me!)
You're giving new[] a value of -1, and it's interpreting it as 4294967295.


Alright, so when I declared it: int* z = new int[x]; it actually takes the form of an unsigned integer? If that's the case I'm assuming I can't declare it as a signed integer. Is that just how that works?

Although an idea just occurred to me. Each memory cell is one byte, right? Is there a way to take the users input and convert it to a string of bits/bytes and assign these to the the dynamic memory? Then I could perform binary math on that string. I guess the tricky part would be converting the large string of bytes back into decimals in a way the computer will understand. Maybe as a character string of the digits?

Though i do have a suggestion, you're going to want to accommodate for the idiot who tries to enter letters (like me!)


With regards to you, I thought about that. I actually tried entering a number last night and realized the error. I was kinda hoping it would just convert it to its integer form lol. Is there a way to only accept certain keyboard entries into the console?

Actually, another though, do you think this should allow for only numerical entries?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	int ii = 0;
	char b[32];

	cout << "Input: ";
	while(true)
	{
		b[ii] = getch();

		while (b[ii] <'0' || b[ii] > '9')
		{
			b[ii] = getch();
			if (b[ii] = /*enter*/)
			{
				break;
			}
		}
		cout << b[ii];
	}


I just threw this together in a few minutes. I'm sure there's a better way but should this method work? I'm looking for a proof of method right now.

Thanks for your help guys.
Topic archived. No new replies allowed.