Thread: make a vector constant?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    make a vector constant?

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    void test(vector <int*> v)
    {
    	for (int i = 0; i < v.size(); i++)
    	{
    		if (v[0] == v[i])
    		{
    			cout << v[i] << endl;
    		}
    		else
    		{
    			cout << "ugly address man.\n";
    		}
    	}
    }
    
    int main()
    {
    	int j = 2;
    
    	vector <int*> v; // size of the vector starts at 0
    
    	for (int i = 0; i < 2; i++)
    	{
    		if (i != 1)
    		{
    			v.push_back(&i); // v now has 1 element
    			test(v);
    		}
    		else
    		{
    			v.push_back(&j); // v now has 1 element
    			//v[1] = v[0];
    			test(v);
    		}
    	}
    }
    you see line 36 //v[1] = v[0]; change the element 1 to look like element 0 when uncommented? i want to know how to make the element 1 constant so it can't be changed. and do this for every element in the vector, please. i googled how but i didn't find anything.
    Last edited by jeremy duncan; 02-10-2021 at 01:49 PM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    i see here is something like what i want but its not possible using vector push back.

    c++ - Why can&#39;t I push_back to a vector of const elements? - Stack Overflow

    is there some other way to make an array dynamically grow, like vector push back, and also make the array elements const so they can't be changed?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    apparently:

    "The size of an array is static in C++. You cannot dynamically resize it. That's what std::vector is for:"

    and

    "With STL containers, the value type is required to be assignable. (not const)"

    so the only way to ensure the contents are secure is to write to a file? arrays won't resize, and vectors can be made to copy other values insecurely into the element.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're very focussed on the implementation, i.e., you have a vector<int*> and you want to constrain it in some way.

    Another way of looking at things is that you have something that you're trying to model (represent as an abstraction), and internally it happens to make use of a vector<int*>. You then determine what operations you want to include in this thing that you model, and you then write a class that has a vector<int*> private member. So, naturally this class will not provide an operation that will result in assignment to an existing element of the internal vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    this is what i came up with:

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <string>
    using namespace std;
    
    void ReadTheTest()
    {
    	string myText;
    	string first;
    	int counter = 0;
    
    	ifstream MyReadFile("test.txt");
    
    	while (getline(MyReadFile, myText)) 
    	{
    		if (counter == 0)
    		{
    			first = myText;
    		}
    		else
    		{
    			if (first != myText)
    			{
    				cout << "bad address man.\n";
    			}
    		}
    		counter++;
    	}
    
    	MyReadFile.close();
    }
    
    void test(vector <int*> v)
    {
    	for (int i = 0; i < v.size(); i++)
    	{
    		ofstream myfile;
    		myfile.open("test.txt", ios_base::app);
    		myfile << v[i] << "\n";
    		myfile.close();
    	}
    }
    
    int main()
    {
    	int j = 2;
    
    	vector <int*> v; // size of the vector starts at 0
    
    	for (int i = 0; i < 2; i++)
    	{
    		if (i != 1)
    		{
    			v.push_back(&i); // v now has 1 element
    			ofstream myfile;
    			myfile.open("test.txt", ios_base::app);
    			myfile << v[i] << "\n";
    			myfile.close();
    		}
    		else
    		{
    			v.push_back(&j); // v now has 1 element
    			test(v);
    			v[1] = v[0];
    			
    		}
    
    		test(v);		
    	}
    
    	ReadTheTest();
    }
    in main, in the else, line 65, the element 0 wrote over element 1, which hides the fact that in line 63 the value j wrote to the element 1 too.

    so for security when element 1 is going to be used it runs the test first. then if the vector element is wrote over its able to be identified.
    Last edited by jeremy duncan; 02-10-2021 at 03:42 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's a rather inefficient solution, and is based on detection rather than prevention. I recommend that you consider writing a class that I suggested in my previous post.

    I note that none of these are really concerned with security per se: if you have an attacker who can change your code to insert such an assignment, then they can of course change your code to remove your test function call, or change your code to change the class I suggested, etc. Rather, the techniques here are for guiding (or even forcing) client code into doing what you expect.
    Last edited by laserlight; 02-10-2021 at 03:48 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    write the j address in the file, this sends a error in the ReadTheTest function, then get a count of how many addresses are bad. should just be one. that function then sends a blip somewhere showing if its just one or no blip to show somethings wrong.

    when does j get written in? some random time decided by the online server testing the SW.

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <string>
    using namespace std;
    
    void ReadTheTest()
    {
    	string myText;
    	string first;
    	int counter = 0;
    
    	ifstream MyReadFile("test.txt");
    
    	while (getline(MyReadFile, myText))
    	{
    		if (counter == 0)
    		{
    			first = myText;
    		}
    		else
    		{
    			if (first != myText)
    			{
    				cout << "bad address man.\n";
    			}
    		}
    		counter++;
    	}
    
    	MyReadFile.close();
    }
    
    void test(vector <int*> v)
    {
    	for (int i = 0; i < v.size(); i++)
    	{
    		ofstream myfile;
    		myfile.open("test.txt", ios_base::app);
    		myfile << v[i] << "\n";
    		myfile.close();
    	}
    }
    
    int main()
    {
    	int x;
    	cout << "Type a number: "; // the online server randomly types in a number
    	cin >> x;
    
    	int j = 2;
    
    	vector <int*> v; // size of the vector starts at 0
    
    	for (int i = 0; i < 2; i++)
    	{
    		if ((i != 1) || (x != 1))
    		{
    			v.push_back(&i); // v now has 1 element
    			ofstream myfile;
    			myfile.open("test.txt", ios_base::app);
    			myfile << v[i] << "\n";
    			myfile.close();
    		}
    		else if (x == 1) // maybe this is also set by the online server
    		{
    			v.push_back(&j); // v now has 1 element
    			test(v);
    			v[1] = v[0];
    
    		}
    
    		test(v);
    	}
    
    	ReadTheTest();
    }
    Last edited by jeremy duncan; 02-10-2021 at 06:57 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compile this program:
    Code:
    #include <iostream>
    #include <vector>
    
    class AddressVerifier
    {
    public:
        void push_back(int* address)
        {
            addresses.push_back(address);
        }
    
        const int* operator[](std::size_t i) const
        {
            return addresses[i];
        }
    
        std::size_t size() const
        {
            return addresses.size();
        }
    private:
        std::vector<int*> addresses;
    };
    
    using namespace std;
    
    void test(AddressVerifier& v)
    {
        for (size_t i = 0; i < v.size(); i++)
        {
            if (v[0] == v[i])
            {
                cout << v[i] << endl;
            }
            else
            {
                cout << "ugly address man.\n";
            }
        }
    }
    
    int main()
    {
        int j = 2;
    
        AddressVerifier v;
    
        for (int i = 0; i < 2; i++)
        {
            if (i != 1)
            {
                v.push_back(&i); // v now has 1 element
                test(v);
            }
            else
            {
                v.push_back(&j); // v now has 1 element
                //v[1] = v[0];
                test(v);
            }
        }
    }
    You can see that it follows very closely your program in post #1, except for the replacement of the direct use of the vector with the AddressVerifier class.

    Uncomment the line that you were talking about in post #1 and observe the compile error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    thats beautiful. ty!

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    here's how i delete the vector in the code in post 7. does this look right, the last 2 lines in main:

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <string>
    using namespace std;
    
    void ReadTheTest()
    {
    	string myText;
    	string first;
    	int counter = 0;
    
    	ifstream MyReadFile("test.txt");
    
    	while (getline(MyReadFile, myText))
    	{
    		if (counter == 0)
    		{
    			first = myText;
    		}
    		else
    		{
    			if (first != myText)
    			{
    				cout << "bad address man.\n";
    			}
    		}
    		counter++;
    	}
    
    	MyReadFile.close();
    }
    
    void test(vector <int*> v)
    {
    	for (int i = 0; i < v.size(); i++)
    	{
    		ofstream myfile;
    		myfile.open("test.txt", ios_base::app);
    		myfile << v [ i ] << "\n";
    		myfile.close();
    	}
    }
    
    int main()
    {
    	int x;
    	cout << "Type a number: "; // the online server randomly types in a number
    	cin >> x;
    
    	int j = 2;
    
    	vector <int*> v; // size of the vector starts at 0
    
    	for (int i = 0; i < 2; i++)
    	{
    		if ((i != 1) || (x != 1))
    		{
    			v.push_back(&i); // v now has 1 element
    			ofstream myfile;
    			myfile.open("test.txt", ios_base::app);
    			myfile << v [ i ] << "\n";
    			myfile.close();
    		}
    		else if (x == 1) // maybe this is also set by the online server
    		{
    			v.push_back(&j); // v now has 1 element
    			test(v);
    			v[1] = v[0];
    
    		}
    
    		test(v);
    	}
    
    	ReadTheTest();
    
    	v.clear();
    	v.shrink_to_fit();
    }
    to do the same in the code you made in post 8, just make a setter that does these last 2 lines in main in the class then call that setter when your done with the vector.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be pointless since the main function ends immediately afterwards.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, you cannot delete what you (or a library that you used) did not new.

    But again, it is pointless to manually destroy the vector when it would have been automatically destroyed immediately after the code that you use to destroy it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    i saw that but i wasn't sure. ty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character arrays - constant values in a constant expression
    By hewlettuser in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2017, 04:00 AM
  2. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  3. Replies: 45
    Last Post: 04-02-2008, 09:10 AM
  4. Why make a constant a constant again?
    By Overworked_PhD in forum C Programming
    Replies: 3
    Last Post: 11-03-2007, 06:57 PM
  5. Newbie: how do you make a char a constant?
    By Bob++ in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2006, 06:17 PM

Tags for this Thread