• 11
• 9
• 12
• 9
• 11

Problems with pointers

This topic is 4275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello, I'm new to this forum, and I'm just starting in C++. I've had some experience (sort of) programming in Game Maker, so I've got a good grasp on basic things like variables, arrays, loops and stuff like that. Recently, however, I've been having some problems with pointers (hence the topic title); I've been trying to use them to create a dynamically sized array, and it seems to be working pretty well, except my program freezes at seemingly random times. I'm pretty sure it's a memory leak, but I'm not too good at finding those things. Here's the source code for my program; any help would be appreciated. If it helps, I'm using Dev-C++ 4.9.9.2.
#include <iostream>

using namespace std;

int main()
{
long int input;
long int i;

while(true)
{
cin >> input;
long int *pointer = new long int[input];

for(i=0;i<=input;i+=1)
{
pointer=i;
}

cout << pointer[input] << "\n";

cin.get();
delete [] pointer;
cout << "Memory cleared.\n";

cin.get();
system("cls");
}
cin.get();
}
P.S.: If anyone knows how I could put my code in a box on this forum, to separate it from the rest of my post, I'd greatly appreciate it if you could tell me. EDIT: Don't bash me for the lack of indentation, the forum decided to get rid of mine when I posted :P EDIT2: After some testing, I've found out that it wasn't really random freezing, it was only freezing after I typed numbers while pressing enter really fast, or if I typed numbers over 100,000,000. Might not be a leak after all, just me being stupid and overestimating my console app. :O
Edit by Fruny: re-fixed the formatting. [Edited by - Fruny on August 10, 2006 12:51:47 AM]

Share on other sites
No memory leak, but you're writing one past the end of the array, into potentially prohibited territory.

When you allocate an array as in new int[size], it allocates "size" elements. That includes the zeroth element, so the last usable element is actually in slot "size-1".

Share on other sites
Your program does have bugs. You are accessing outside the bounds of your array.

Remember, an array called Array, with length X, has members Array[0], Array[1], Array[2]...Array[X - 2], Array[X - 1]. Array[X] does not exist!

You are accessing pointer[input], which does not exist. The last element of pointer is pointer[input - 1].

You attempt to access pointer[input] in your cout statement.

Your program is likely crashing when this results in an invalid memory read. Note that there are times when it will work anyway, simply because the memory you are trying to access luckily isn't invalid.

Out of curiousity, why are you using long ints, and not just ints?

Share on other sites
Yea, your loop should look like this

for(i=0; i < input ;i++)
{
pointer = i;
}

if say, input is 5, then it's going to try and put items in the 0,1,2,3,4,5th elements of the array, which is actually 6 elements. But you only have 5 elements.

So you allways need to do 1 less then the sepcified array size.

if it's 10, then i < 10

Share on other sites
If you enter text instead of a number for input, the read will fail (check cin.fail()) and the text will be left in the input buffer, which will cause problems with the subsequent cin.get()s.

If the read for input fail, then input is left uninitialized, meaning that it can have a very large value, which may cause new to fail and throw an exception (→ crash) or return zero if you're using an old C++ compiler (e.g. VC6), which will cause a crash when doing the assignments.

If the value for input is large, the for loop may take quite a while.

You have an off-by-one error in the termination condition of your loop: an array of N elements has indices going from 0 to N-1, so the test should be i < input and not i <= input. As currently written, your code accesses memory past the end of the array, which is a Bad Thing™. Simply using i != input is a good habit to take as you will eventually want to learn about iterators, not all of which can be ordered (e.g. list iterators).

Trying to access pointer[input] is also a Bad Thing™ for exactly the same reason.

If you need to use dynamic arrays, use the standard C++ std::vector class template.

Share on other sites
Thanks for pointing out the "reading past the end of the array" problem, that's a bad Game Maker habit I picked up :(. I'm a bit tired right now, so I'll work that out later. Thanks for the help!

Quote:
 Original post by Gauvir_MuccaOut of curiousity, why are you using long ints, and not just ints?

I used long ints because I thought the problem was I was using too big of numbers, when it was really that I was screwing up my array.