1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C++ is variable set.

Discussion in 'Software Development' started by Boylett, Jul 5, 2007.

Thread Status:
Not open for further replies.
  1. Boylett

    Boylett Thread Starter

    Dec 23, 2006
    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?

  2. JohnWill

    JohnWill Retired Moderator

    Oct 19, 2002
    You've just declared a pointer to a bool, where does it get initialized?
  3. Shadow2531


    Apr 30, 2001
    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.
  4. Boylett

    Boylett Thread Starter

    Dec 23, 2006
    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.
  5. Shadow2531


    Apr 30, 2001
    Have a look at a bunch of examples:

    #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) {
        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) {
        cout << count << endl;
        vector<bool> zam;
        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]) {
        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) {
        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.
  6. WARnux


    Jun 29, 2007
    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.
  7. Sponsor

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 733,556 other people just like you!

Similar Threads - variable
  1. des000
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/592170

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice