Jump to content
  • Advertisement
Sign in to follow this  
Lollyn00b

Problems with pointers

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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_Mucca
Out 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.

Share this post


Link to post
Share on other sites
0) For Beginners is at the top of the forum list for a reason.
1) Everything Fruny said.
2) To reiterate, don't do this kind of stuff manually. Even expert programmers don't do this kind of stuff manually without a damn good reason. Use std::vector. It does this stuff for you, at least as well as the vast majority of circumstances require, and in some ways far better than a naive implementation would.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!