problem from tutorial

Pages: 1234
Mouse hover over c++ or after compile it shows language under page header.
WinwordExonar, in ideone.com if u hover on the language u've selected it will show u the compiler being used.

@ Peter87, why would cin >> i; fail???
That is awesome. Thank you.

Well what the OP and I are using the the GNU GCC Compiler ( MinGW Which is the GCC Compiler for Windows ) that is the default for Code::Blocks 10.05 Windows Version. I have personally been using it since its version came out without a problem. But now I am wondering how much different it is from the standard GCC that you see on that site. Keep in mind, your dealing with Linux GCC compilers on that website where as we are using Windows GCC compilers. That website is showing the version that Linux is using. Go to the GCC website and the MinGW website and you'll see they are not the same version.

( Although MinGW is based off of the Cygwin GCC )

On top of that, that http://ideone.com website is using an older copy of GCC.
Last edited on
@kOdL3R
It relies on the user who could type anything in for i such as the string "kOdL3R". Th e stream itself could also become corrupted I suppose.
kOdL3R: cin >> i; can fail if the user enters a number that is bigger than INT_MAX.

Try this code:
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
#include <iostream> 
#include <climits> 
#include <new> 
using namespace std; 
 
int main () 
{ 
	int i,n; 
	int * p; 
	cout << "How many numbers would you like to type? "; 
	while (!(cin >> i))
	{
		 cout << "Error: invalid number! Try again: ";
		 cin.clear();
		 cin.ignore(INT_MAX, '\n');
	}
		
	p = new (nothrow) int[i]; 
	if (p == 0) 
		cout << "Error: memory could not be allocated"; 
	else 
	{ 
		for (n=0; n<i; n++) 
		{ 
		cout << "Enter number: "; 
		cin >> p[n]; 
		} 
		cout << "You have entered: "; 
		for (n=0; n<i; n++) 
			cout << p[n] << ", "; 
		delete[] p; 
	}
	return 0; 
}
@WinwordExonar

The original question:
kOdL3r wrote:
as per the tutorial the program should produce the output "Error: memory could not be allocated" if i give an input too big....but instead it's producing "Enter number:" for infinite times..... :(
i'm using "codeblocks 10.05" with "gcc". can anyone plz explain what's wrong?


"input Too big" is a very relative term, too big for the tutorial author may have been considerably less memory then you and I have available. There is NO error in the original code.
Last edited on
Peter, INT_MAX = 32767. But we can type in larger numbers and there is no Error. Not for us. Its why I am saying this has to do with Pointers.. not p being an integer. The code does NOT work if you do not explain to the compiler that its to be a POINTER... not an INTEGER.

That's the whole problem here.
Last edited on
WinwordExonar, Stop trolling. I doubt INT_MAX is 32767 on your system but it doesn't matter. INT_MAX is the biggest value that can be stored in an int variable. If the user enters a value bigger than INT_MAX cin >> i; will fail and you can't be sure what value the i variable has.
I don't think he its trolling, he is confused/mistaken about pointers though.
I'm not trolling sir. In fact my solution worked for the OP. Yours has not. Your the one trolling here.

This whole thread can be condensed into a simple paragraph.

The OP and I are using the same WINDOWS MinGW GCC compiler from Code::Blocks 10.05. Its why I knew the answer would work for him. You guys are trying to use a Linux GCC compiler from a website that is showing the compiler to be outdated to begin with. Your not using the SAME compiler we are. I asked for you to explain WHY your answer "is the right one" but you still have not with the correct parameters that we are using. Your using a different set of tools then we are.
@WinwordExonar

If you're not a troll I suggest you read up on pointers in C++.
The original code was OK (though if (p == NULL) or if (p == nullptr) // in C++11 or (my favourite) if (!p)would be "more" correct)
Otherwise please ignore me (which a troll probably won't - I know...).
There shouldn't be any difference between compilers if the program was working correctly. That your "solution" only works on one version of Code::Blocks/gcc should be a sign the code is not correct. You are just unlucky it didn't crash. Did you try to run my code? Did it work?
Last edited on
I get that it works on some compilers. I get that. But in the compiler we are using you have to add the *. Read this thread and you'll see that it works for both the OP and myself because we both use the same compiler. Or better yet, you get the compiler + IDE and try it yourself. You'll see what we are talking about.

I have read that tutorial and it itself shows the * in front of it in some lines. Again, this whole thread is about the compiler. I will however try your suggestion about the ! ( exclamation mark ) ... thank you for that.

EDIT : No that does not work either. Its like saying if(p != 0) which would not work at all unless the answer was 0 in our compiler.
Last edited on
The OP's "issue" was that the "too big" value he was inputting to expect the "Error: memory could not be allocated" actually wasn't too big. Changing p to *p did NOT fix any problem, in fact it did nothing. You really need to read up on pointers.
Last edited on
Naraku, are you using our compiler we are using ? Code::Blocks with the MinGW GNU GCC compiler ?? IF your not, get it and you'll see what we are talking about.
Last edited on
Dude, it has NOTHING to do with the compiler.
Bro, all I am asking you to do is get the compiler. As a fellow programmer, get it and see it for yourself. What your explaining in your compiler and what we are seeing in ours is different.
Last edited on
I cannot believe I actually downloaded and installed Code::blocks just to test this. And low and behold it works exactly the same as in VC++2010 and on ideone.com.
now type in a HUGE number.. bigger then you would expect. It will not error out. When you add the * it works as its supposed too.

And please understand.. i'm not trying to make your life difficult here, but we are trying to explain the problem as best as we can.
Last edited on
This thread is amusing, to say the least.
Pages: 1234