Like Tree5Likes

Class vector member initilization.

This is a discussion on Class vector member initilization. within the C++ Programming forums, part of the General Programming Boards category; Hey! Working with a new framework for Open GL (SFML love it so far), and I am creating a texture ...

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407

    Class vector member initilization.

    Hey!

    Working with a new framework for Open GL (SFML love it so far), and I am creating a texture manager to manage the textures for any project I do in the future.

    The logical way to do so was to create two vectors one to store the images one to store the TextureID handle for OpenGL. Now the Image datatype in SFML has no method to check if it has been loaded (ikr) so I am faced with a couple solutions.

    1. Don't set the size of the vectors until the AddTexture(string FileName) function and have an int represent its size that is initialized as 0 in the constructor. This int will ++ immediately in the AddTexture function then the AddTexture function will resize all vectors to the size of the int.

    2. I could initialize both vector's size to 1 at construction and then use a boolean to track if I have saved one object yet or not. If I have saved one object then I need to grow both methods before the function starts and if not then I will just save into the last data position of the vectors which would be 0.

    The biggest danger of option 1 is that you can use a GetTexture(int index) function that if called before AddTexture(string FileName) would produce undefined behavior, but if I put a error check in the GetTexture then I could avoid this. (which it should have anyways)

    Are there any other problems having vectors in a class with an uninitialized size during construction?
    Last edited by Lesshardtofind; 01-02-2013 at 01:42 AM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    I don't know how the others, but I have no idea what you are asking for. Don't expect anyone to know what your AddTexture() does.
    Maybe post some pseudo code?
    I never put signature, but I decided to make an exception.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Sounds like you want a dynamic array whose size you will manually manage. Don't.
    Use std::vector's push_back method instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I just wanted to know if there was a danger in having a vector without a size initialized. Such as:

    Code:
    class Aclass{
      private:
        std::vector <int> TheseNums;
      public:
        Aclass();
        int GetNum(int index);
    };
    
    Aclass::Aclass(){
    }
    
    int Aclass::GetNum(int index){
      if(index >= 0 && index < TheseNums.size())
        return TheseNums[index];
      else
        return -1001001;  // random error value
    }
    I'm sure my GetNum error check isn't the best ... maybe a boolean function with a pointer to an int as an argument would be better.

    As for the TextureManager I already finished it with a different method so would just like to know about that question.
    Last edited by Lesshardtofind; 01-02-2013 at 04:45 AM.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    A vector's size is 0 if nothing is in the vector. It's fine.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Thanks I dunno where I come up with these pre-conceived notions that everything must be initialized! ;o)

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    I would say that the vector's size is 0 in this case.

    //too slow
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by Lesshardtofind View Post
    I just wanted to know if there was a danger in having a vector without a size initialized. Such as:
    Also, if you use the .get() member, it will throw if you do an out-of-bounds access, so you never have to worry about the vector's size from a security perspective. You do have to worry about not doing out-of-bounds access in the internals of your code, of course, since this would mean a bug.

    Quote Originally Posted by Lesshardtofind View Post
    Thanks I dunno where I come up with these pre-conceived notions that everything must be initialized! ;o)
    Generally, you should assume that the constructor of an object (unless it is a two-stage construction) will make the object ready for use. That is, after the constructor is finished, you can use the object as normal without consequences (no needing to keep track of size, etc).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Also, if you use the .get() member, it will throw if you do an out-of-bounds access, so you never have to worry about the vector's size from a security perspective
    That's just wrong. at() isn't a silver bullet, you need to control your loops or whatever.

    Throwing is not always acceptable either, but to you this is some strange, weird requirement.
    laserlight and iMalc like this.

  10. #10
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    That is, after the constructor is finished, you can use the object as normal without consequences (no needing to keep track of size, etc).
    Right other than checking for out of bounds access so there are no bugs within the game. That was my point after I created the class I could use it as needed. Without having to do anything to it from the external side because it already managed itself. And not knowing how a vector handled itself as a size 0 I was worried about how it would act.

    Code:
    TextureManager GameTextures;
    GameTextures.AddTexture("thispic.png");
    GameTextures.AddTexture("thatpic.png");
    GameTextures.AddTexture("metoo.png");  
    ..
    ... 
    ... // later on in a render function
    glBindTexture(GL_TEXTURE_2D, GameTextures.GetTexture(2));

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by whiteflags View Post
    That's just wrong. at() isn't a silver bullet, you need to control your loops or whatever.
    This is what I meant by,
    You do have to worry about not doing out-of-bounds access in the internals of your code, of course, since this would mean a bug.
    Quote Originally Posted by whiteflags View Post
    Throwing is not always acceptable either, but to you this is some strange, weird requirement.
    Then you swallow the exception. It's safer than doing manual checking.
    You should not dismiss a potentially great tool just because it has a few drawbacks. I agree that one should learn where to use it and where not, but you make it sound like it's always bad. And remember - this is not a professional program we're discussing. This is a hobby project. There is a difference.

    Quote Originally Posted by Lesshardtofind View Post
    Right other than checking for out of bounds access so there are no bugs within the game.
    Vector can do that for you. No need to do it manually.
    Last edited by Elysia; 01-02-2013 at 05:13 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Vector can do that for you. No need to do it manually.
    So if the get function is handed a number to high what does the .get() function return that prevents me from binding the texture and displaying it? (aka a undefined texture) without me having my own error code?

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Quote Originally Posted by Elysia View Post
    This is what I meant by,
    Then you swallow the exception. It's safer than doing manual checking.
    You should not dismiss a potentially great tool just because it has a few drawbacks.
    Shut up. People have made this point before but throwing is an interruption of code, period. Code which may be running a service. So that reasoning doesn't always work. Some people do define security in a way that means "this doesn't fail".

    I agree that one should learn where to use it and where not, but you make it sound like it's always bad.
    No I don't.

    And remember - this is not a professional program we're discussing. This is a hobby project. There is a difference.
    I hope you're dedicated to convincing me of this. I won't believe in shoddy work in 100 years.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by Lesshardtofind View Post
    So if the get function is handed a number to high what does the .get() function return that prevents me from binding the texture and displaying it? (aka a undefined texture) without me having my own error code?
    It throws an exception.

    Quote Originally Posted by whiteflags View Post
    Shut up. People have made this point before but throwing is an interruption of code, period. Code which may be running a service. So that reasoning doesn't always work. Some people do define security in a way that means "this doesn't fail".
    That's a stupid definition, because that may just be one consequence of failed security.
    Show me why you can't simply swallow the exception in a service.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Lesshardtofind
    So if the get function is handed a number to high what does the .get() function return that prevents me from binding the texture and displaying it? (aka a undefined texture) without me having my own error code?
    As whiteflags silently pointed out, Elysia made a typo error: the member function of vector in question is at, not get. The std::out_of_range exception will be thrown. However, properly written code would not have this exception thrown in the first place, i.e., it is a fail-safe, not a silver bullet such that "you never have to worry about the vector's size from a security perspective".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-18-2012, 10:17 AM
  2. class vector member template
    By Tropod in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2012, 05:28 PM
  3. Replies: 3
    Last Post: 03-21-2008, 12:19 AM
  4. Replies: 2
    Last Post: 11-04-2007, 11:55 AM
  5. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM

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