C++ Can somebody explain me this thing about arrays?

Discussion in 'Programming/Html' started by The Chubu, Feb 3, 2012.

  1. The Chubu

    The Chubu Ancient Guru

    Messages:
    2,537
    Likes Received:
    0
    GPU:
    MSi GTX560 TwinFrozrII OC
    Hi, well, i was reading a C++ tutorial, and i was reading a part bout stating members in classes.

    Now, it had a piece of code using a static member for counting how many objects with a determined class were allocated. The class had a static member n and a constructor CDummy() {n++}. So each time that an object is being allocated with that class and no parameters are passed, the constructor would increment n by one i guess.

    Now, the thing is that the guy made an example creating first an object named a, then an array of objects named b[5].

    So after making the program return the value of n, it would trow a 6. First i mistakenly thought that it should trow a 7 cuz arrays count from 0. So array 0, 1, 2, 3, 4, 5, plus an a would make it 7. Then i returned to the arrays section and discovered that you specify the total amount of elements instead of the number of the top of the array, is this ok right?

    The array initialized as b[5] would have b[0], b[1], b[2], b[3] and b[4], five elements in total?

    Now, to my surprise, i wrote a few lines, before discovering that fact, and made a b[1] array of said object, with a few variables (x, y) and tried to assign values to them plus outputing the ammount of objects allocated. Look at this
    Code:
    // static members in classes
    #include <iostream>
    using namespace std;
    
    class CDummy {
      public:
        int x;
        int y;
        static int n;
        CDummy () { x= 0; y = 0; n++; };
        ~CDummy () { n--; };
    };
    
    int CDummy::n=0;
    
    int main () {
    
      CDummy a;
      cout << "a.n:" << a.n << endl;
      cout << "a.x:" << a.x << endl;
      cout << "a.y:" << a.y << endl;
      CDummy b[1];
      b[0].x = 1;
      b[0].y = 2;
      cout << "b[0].n:" << b[0].n << endl;
      cout << "b[0].x:" << b[0].x << endl;
      cout << "b[0].y:" << b[0].y << endl;
      b[1].x = 10;
      b[1].y = 5;
      cout << "b[1].n:" << b[1].n << endl;
      cout << "b[1].x:" << b[1].x << endl;
      cout << "b[1].y:" << b[1].y << endl;
      CDummy * c = new CDummy;
      cout << "a.n:" << a.n << endl;
      delete c;
      cout << "a.n:" << CDummy::n << endl;
      return 0;
    }
    First, works perfectly. But my concern is that exactly in b[1].x = 10; and b[1].y = 5; i am assigning values to variables that are actually outside the array? Stuff that shoulnt be there? Im messing up memory?
     
  2. Janq

    Janq New Member

    Messages:
    8
    Likes Received:
    0
    Yes, you are basically writing off the end of the array and trashing the stack. It may "work", but only by luck ;)
     
  3. dave11

    dave11 Master Guru

    Messages:
    317
    Likes Received:
    2
    GPU:
    AMD RX6800M
    Yes you are dealing with undefined behaviour as your accessing memory outside of the array.

    Always make sure you handle memory correctly in C/C++ it wont stop you from doing something stupid.

    Anyway heres what VS2010 has to say at the end of execution:

    Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted.

    Dave
     
  4. The Chubu

    The Chubu Ancient Guru

    Messages:
    2,537
    Likes Received:
    0
    GPU:
    MSi GTX560 TwinFrozrII OC
    Huh, i'd thought that it would warn me before allowing me to do it. Anyway, im using Code::Blocks + MinGW atm.

    Thanks for your answers! :) Now i musn't forget that array[5] goes up to array[4].
    I feel dirty :p
     

  5. gamerk2

    gamerk2 Ancient Guru

    Messages:
    2,108
    Likes Received:
    1
    GPU:
    NVIDIA 570 GTX 1.2 GB
    Yeah, one of the carry-overs C++ kept from C: No automatic bounds checking for arrays. Hence the confusion.

    You think thats bad, wait till you try and convert a 5 character CString value into a 5 character array [forgetting that CStrings have a '/0' appended to the end, and you just allocated 6 characters to a 5 character array...]

    For farther reading [and a must read for any C/C++ devs]:
    http://www.flounder.com/memory_damage.htm

    Basically explains what you just did, and the inner workings of how MSVC deals with it.
     
  6. The Chubu

    The Chubu Ancient Guru

    Messages:
    2,537
    Likes Received:
    0
    GPU:
    MSi GTX560 TwinFrozrII OC
    Ah, yes. The tutorial dealt with that, but i already forgot it :p I wanted to read all of it first, then try to "port" a program that i made in Ada in a uni course to C++ (making use of dynamic memory allocation and hopefully classes), checking back with the tutorial when needed.
    Nice, thanks! Already bookmarked it :) (now i need some coffee and Skyrim :D )
     
  7. gamerk2

    gamerk2 Ancient Guru

    Messages:
    2,108
    Likes Received:
    1
    GPU:
    NVIDIA 570 GTX 1.2 GB
    I feel for you; I'm currently being force fed Ada for one of the programs I'm working on. Horrid language...
     
  8. The Chubu

    The Chubu Ancient Guru

    Messages:
    2,537
    Likes Received:
    0
    GPU:
    MSi GTX560 TwinFrozrII OC
    Nah, i dun have issues with it. Its kinda fool proof.
     
  9. gamerk2

    gamerk2 Ancient Guru

    Messages:
    2,108
    Likes Received:
    1
    GPU:
    NVIDIA 570 GTX 1.2 GB
    It prevents programmers from doing what we want to do. We spend more time debugging the language and figuring out how to do simple things then we do actually programming.

    From my experiance, Ada is less stable then C based languages, for no other reason then all the hacks that are needed to either get Ada to do what you want, or the increase performance in some way to make up for its horrid design choices.

    Of all the languages I've ever used, Ada was the least productive one.
     
  10. The Chubu

    The Chubu Ancient Guru

    Messages:
    2,537
    Likes Received:
    0
    GPU:
    MSi GTX560 TwinFrozrII OC
    Hard to make mistakes when you strive to get it working in the first place :D No, srsly, with my limited experience i can see that is way too restrictive. But with i have yet to do something big enough to get frustrated at it for such reasons. I only know data structures, pointers, and stuff at that level. I still kinda dont quite understand how could i make the jump eventually from there to bigger programs, feels like i know very, very little, even if i spent year and a half learning new stuff non stop (taking a break this last half of a year cuz i missed a few requirements for the OOP/Java course). I guess that i'll have to learn a lot of new stuff in the comming years...

    Cant comment bout that, i'll see if i have to make something bigger in Ada in the future...
     

  11. gamerk2

    gamerk2 Ancient Guru

    Messages:
    2,108
    Likes Received:
    1
    GPU:
    NVIDIA 570 GTX 1.2 GB
    ^^ For me? I'm working on a program thats hovering around the 1 million SLOC mark right about now. Written in Ada.

    I have yet to find a developer that likes the language. Thats probably why its used less then languages like Assembly and LOGO.
     

Share This Page