Results 1 to 20 of 20

Thread: Little confusion Regarding general Initilizaion..!!!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Little confusion Regarding general Initilizaion..!!!

    you mean to say tht p[0].. to p[4] will accept values and after tht index out of range..

    is it like this
    p[0] = 0
    p[1] = 1
    p[2] = 2
    p[3] = 3
    p[4] = 4
    p[5] will give index out of range ??? is it so ??

    i am not getting this..!! i get crash after i = 6567. why i don't know

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Little confusion Regarding general Initilizaion..!!!

    This is call undefined behaviour. You are accessing (overwriting actually) memory which is not yours. Anything could happen. (You are destroying your stack amongst other things...)

    That is why one can't explain (ok you could if you examine your cpu architecture, compiler etc most carefully you could explain it) at which iteration it will crash. That is part of being undefined.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Little confusion Regarding general Initilizaion..!!!

    Quote Originally Posted by caduel View Post
    That is why one can't explain (ok you could if you examine your cpu architecture, compiler etc most carefully you could explain it) at which iteration it will crash. That is part of being undefined.
    Actually it's possible to determine when the application will crash. Memory protection is mostly based on memory pages, so the likely place for the system to notice a violation is when you try to write beyond a page you have access to. As pages are most often 4kB big, the likely spot to have a crash is at most 4096B after the last valid index (unless you are "lucky" and the next page is reserved for you as well). Of course this is all academic talk - accessing memory that is not yours is forbidden, even one byte after the last valid index. This might get your frame stack corrupted which leads directly to many dangerous situations.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Little confusion Regarding general Initilizaion..!!!

    Yes, tracking down issues (crashes, if you're lucky) due to out of bounds violations (stack corruptions, overwritten local variables etc) is part of the "fun" when working with C/C++. Especially when the code was written by a dear colleague that has ideally left the company since

    ( In C++ you have more ways to step around this kind of problem as you have std::vector etc and do not have to work with what C calls arrays so often. )

  5. #5
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Little confusion Regarding general Initilizaion..!!!

    Quote Originally Posted by caduel View Post
    ( In C++ you have more ways to step around this kind of problem as you have std::vector etc and do not have to work with what C calls arrays so often. )
    What do you mean with 'std::vector'? You certainly mean QVector.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Little confusion Regarding general Initilizaion..!!!

    STL vector, this is General programming.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Little confusion Regarding general Initilizaion..!!!

    STL vector sucks It's much better than STL string (compared to QString), but sucks anyway...

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Little confusion Regarding general Initilizaion..!!!

    I totally agree. STL is ugly like MFC

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Little confusion Regarding general Initilizaion..!!!

    It's not that it's "ugly". According to me it has two problems - it lacks functionality and it has many different implementations with different quality, complexity, etc. And it tends to be heavy sometimes... Qt classes are much more lightweight.

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Little confusion Regarding general Initilizaion..!!!

    if compare Qt and STL, then using Qt classes more convenient and as you say, more light.

  11. #11
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Little confusion Regarding general Initilizaion..!!!

    When compared with a C style "array", even the worst STL is a blessing.
    But I agree that Qt classes often offer better usability and more useful methods.
    (But, as there are times when one can use C++ and STL but not introduce the dependency to Qt, I use STL, too.)

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Little confusion Regarding general Initilizaion..!!!

    Quote Originally Posted by caduel View Post
    When compared with a C style "array", even the worst STL is a blessing.
    C style arrays don't cause crashes when used correctly. Bad STL implementations (like the one present in VC6) do.

Similar Threads

  1. Little confusion Regarding general Initilizaion.. :confused:
    By kunalnandi in forum General Programming
    Replies: 2
    Last Post: 19th September 2008, 10:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.