C++ is variable set.

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Boylett

Thread Starter
Joined
Dec 23, 2006
Messages
55
Im trying to make a data storage system plugin for a less powerful language. In one of the functions I need to check if a variable is equal to true.

int i = 0;
while(usedvar) i++;

but usedvar is declared as:
bool * usedvar;
so it might not exist, meaing while(usedvar) will crash.
is there a function to see if a variable exists? something like phps isset?

Thanks.
 

JohnWill

Retired Moderator
Joined
Oct 19, 2002
Messages
106,429
You've just declared a pointer to a bool, where does it get initialized?
 
Joined
Apr 30, 2001
Messages
2,636
I think we need more info.

Do you want an array of bools and you want to count till you hit a false?

Or do you want an array of bool pointers and you want to count till a pointer is null?

Or do you want an array of bool pointers and you want to dereference each pointer to see if the corresponding value is true or false and keep counting if it's true?

vector<bool> or vector<bool*> could be used for storage. You can then iterate through the vector and break early depending on the condition you want.

If you're going to use a vector<bool*>, you might want to make sure that it only contains pointers that actually point to something at all times.

How bout an example that everyone can compile along with a description of what you want to do.
 

Boylett

Thread Starter
Joined
Dec 23, 2006
Messages
55
Well if I do:

bool * usedvar;

It declares usedvar with an "unknown" array size, yes? So if I do:

usedvar[10] = true;

usedvar[10] now exists.

but if I do if(usedvar[9]) it will crash, becuase usedvar[9] doesnt exist.

Do you see what I am saying?
So I need to do something like if(isset(usedvar[9])) to make sure it exists.
 
Joined
Apr 30, 2001
Messages
2,636
Have a look at a bunch of examples:

Code:
#include <iostream>
#include <vector>
#include <cstddef>
using namespace std;

int main() {
    vector<bool> usedvar;
    usedvar.push_back(true);  // usedvar[0]
    usedvar.push_back(true);  // usedvar[1]
    usedvar.push_back(true);  // usedvar[2]
    usedvar.push_back(false); // usedvar[3]
    usedvar.push_back(true);  // usedvar[4]
    int count = 0;
    for (vector<bool>::const_iterator i = usedvar.begin(); i != usedvar.end(); ++i, ++count) {
        if (!*i) {
            break;
        }
    }
    cout << count << endl;

    vector<bool> test(5, true);
    test[3] = false;
    count = 0;
    for (vector<bool>::const_iterator i = test.begin(); i != test.end(); ++i, ++count) {
        if (!*i) {
            break;
        }
    }
    cout << count << endl;
    
    vector<bool> zam;
    zam.push_back(false);
    zam.insert(zam.begin(), true);
    zam.insert(zam.begin(), true);
    cout << zam[0] << endl; // true
    cout << zam[1] << endl; // true
    cout << zam[2] << endl; // false
    
    
    bool usedvar2[] = {true, true, true, false, false };
    cout << usedvar2[2] << endl;
    
    bool* usedvar3 = new bool[5];
    usedvar3[0] = true;
    usedvar3[1] = true;
    usedvar3[2] = true;
    usedvar3[3] = false;
    usedvar3[4] = true;
    cout << usedvar3[2] << endl;
    delete [] usedvar3;
    
    bool x = true;
    bool* p = &x;
    cout << *p << endl;
    
    vector<bool> z(11, false);
    z[10] = true;
    int t = 0;
    for (size_t i = 0; i < z.size(); ++i) {
        if (z[i]) {
            ++t;
        }
    }
    cout << t << " trues were found" << endl;
    
    vector<bool> h(11, true);
    h[7] = false;
    int y = 0;
    for (vector<bool>::const_reverse_iterator i = h.rbegin(); i != static_cast<vector<bool>::const_reverse_iterator>(h.rend()); ++i, ++y) {
        if(!*i) {
            break;
        }
    }
    cout << y << endl;
    
    bool j[3];
    j[0] = true;
    j[1] = false;
    j[2] = true;
    
    bool* r = j;
    cout << r[2] << endl;
}
For an array in c++, you have to create it with a certain size and initialize all its elements. In your case, just initialize them all to false and set the ones you want to true or vice versa.

With bool*, the pointer represents a spot in memory where a bool is supposed to be at. In the case of an array of bools, the pointer points to the first element in the array. It does not represent the whole array itself. But, if you know the size of the array, you can use the pointer of the first element as a base to find the spots in memory after it for the rest of the elements in the array. Of course, you have to allocate memory for all the elements. bool* alone doesn't really do anything unless you assign the address of the first element of another array (or a single bool) to it.

With a vector though, you can start off with a size of zero and add elements as you go. You can add them to the end or the beginning. You can even iterate through the vector backwards.

Maybe the vector<bool> z or h example will give you a hint on how to do what you want.

How bout this. Show a working, full (but minimal) example in php, so it can just be converted.
 
Joined
Jun 29, 2007
Messages
78
C++ is a low level language. A simple pointer does not know it's own size. This is why Shadow suggested using vector to simplify things. I made my own wrapper class that does about the same thing as vector and is normally a little faster.

But if you must use a pointer by itself you need to also send the number of elements in the array to the function so that you know when to stop looping.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top