Why do I need pointer

This is a discussion on Why do I need pointer within the C++ Programming forums, part of the General Programming Boards category; Hi. I have a vector of pointers. When I try to return v[i] from the following function and then try ...

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Why do I need pointer

    Hi. I have a vector of pointers. When I try to return v[i] from the following function and then try to output the values to the console, the program crashes.
    Code:
    Huff* findMinimum(vector<Huff*>& v)
    {
         int i, minCo=v[0]->count;
         char minCh=v[0]->ch;
         for(i=1; i<v.size(); i++)
         {
             if(v[i]->count < minCo)
             {
                 minCo=v[i]->count;
                 minCh=v[i]->ch;
             }
         }
         return v[i];
    }
    However, once I do this:
    Code:
    Huff* findMinimum(vector<Huff*>& v)
    {
         int i, minCo=v[0]->count;
         char minCh=v[0]->ch;
         Huff* h3;
         for(i=1; i<v.size(); i++)
         {
             if(v[i]->count < minCo)
             {
                 minCo=v[i]->count;
                 minCh=v[i]->ch;
                 h3=v[i];
             }
         }
         return h3;
    }
    The program will output the values fine? Isn't v[i] a pointer? I'm baffled. Thanks, Steve
    Code:
    #include<iostream>
    #include<string>
    #include<vector>
    #include<fstream>
    #include<stack>
    using namespace std;
    
    struct Huff;
    
    Huff* findMinimum(vector<Huff*>& v);
    
    struct Huff
    {
        Huff();
        int count;
        char ch;
        Huff* left;
        Huff* right;
        Huff* parent;
    };
    
    int main(int argc, char* argv[])
    {
    	if(argc<2)
    	{
    	    cout<<"Not enough arguments were provided."<<endl;
    		return -1;
    	}
        ifstream myFile(argv[1], ios::in);
    	if(!myFile.is_open())
    	{
    	    cout<<"Unknown file specified."<<endl;
    		return -1;
    	}
    	char cz;
        int i;
        vector<Huff*> values;
        while(myFile.get(cz))
        {
            if(cz < 'a' || cz > 'z')
                continue;
            for(i=0; i<values.size(); i++)
            {
                if(values[i]->ch == cz)
                {
                    values[i]->count++;
                    break;
                }
            }
            if(values.size()==i)
            {
                Huff* h = new Huff;
                h->ch=cz;
                h->count=1;
                values.push_back(h);
            }
         }
         for(i=0; i<values.size(); i++)
             cout<<values[i]->ch<<values[i]->count<<endl;
         Huff* h2;
         h2=findMinimum(values);
         cout<<h2->ch<<endl;
         cout<<h2->count<<endl;
         return 0;
    }
    
    Huff::Huff() : ch(0), count(0) { }
    
    Huff* findMinimum(vector<Huff*>& v)
    {
         int i, minCo=v[0]->count;
         char minCh=v[0]->ch;
         Huff* h3;
         for(i=1; i<v.size(); i++)
         {
             if(v[i]->count < minCo)
             {
                 minCo=v[i]->count;
                 minCh=v[i]->ch;
                 h3=v[i];
             }
         }
         return h3;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Isn't v[i] a pointer?
    It's treated as one, yes. But because the loop terminated normally (when i == v.size()), v[i] is an out of bounds index. You can only access indices 0 to v.size() - 1.
    My best code is written with the delete key.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Notice that you do not break out of the loop when the 'if' condition evaluates to true (or for that matter, the last time it does, which is the logical equivalent of your second piece of code). So, i keeps getting incremented until it no longer satisifies the loop condition (when i == v.size()), so you end up returning v[v.size()] which is, of course, out of bounds.

    *edit*
    Foiled! Grrr...
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Thumbs up Thanks

    Glad you could see that! Wish I could have. Thanks again. Steve

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21