Results 1 to 20 of 20

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

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

    Question Little confusion Regarding general Initilizaion..!!!

    Hello,

    I have some confusion with Initilization of int.

    Qt Code:
    1. #include <iostream>
    2.  
    3. using namespace std;
    4. int main()
    5. {
    6. int *p = new int[4];
    7. for( int i = 0; i < 6568; i++ )
    8. {
    9. p[i] = i;
    10. cout << i << " ->> " << p[i] << endl;
    11. }
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Above, i have initilize int *p = new int[4];
    when i run this program on windows it get crashed after printing

    Output :
    0 ->> 0
    .
    .
    .
    .
    6567 ->> 6567
    why is it so ????


    And instead of Initilizing like this
    int *p = new int[4];

    if i initilize
    int *p = new int[100];

    then on running this example it get crahsed on 182 ?
    why is it so ???

    Output :
    0 ->> 0
    .
    .
    .
    .
    182 ->> 182
    can any body explain me why this happens ?
    normally we expect that int[100] will take more values int[4], but its completely different here..!!

    can any body tell me what does these below initilization says..!!

    int *p = new int();
    int *p = new int(10);
    int *p = new int[10];

    Thanks & Best Regards
    Kunal Nandi

  2. #2
    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..!!!

    Quote Originally Posted by kunalnandi View Post
    int *p = new int();
    creates a pointer to int with default value 0.

    Quote Originally Posted by kunalnandi View Post
    int *p = new int(10);
    creates a pointer to int with default 10.

    Quote Originally Posted by kunalnandi View Post
    int *p = new int[10];
    creates a pointer to a first element of array with size == 11

  3. #3
    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..!!!

    so, if you need to pass of all an array elements you need to use next code
    Qt Code:
    1. const int arraySize = 10;
    2. int *p = new int[arraySize];
    3. for (int i = 0; i < arraySize + 1; ++i) {
    4. *(p3 + sizeof(p) + i) = i;
    5. qDebug() << (*(p + sizeof(p) + i));
    6. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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..!!!

    Thanks spirit,

    one more question..!!

    int *p = new int[4];
    if i use this in my example then application get crashed after printing 6567

    int *p = new int[100];
    if i use this in my example then application get crashed after printing 182


    Why is it so ??

    Thanks & Best Regards
    Kunal Nandi
    Last edited by kunalnandi; 19th September 2008 at 10:58.

  5. #5
    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..!!!

    6567 it is a number of iteration, but your array has only 4 or 100 elements and then index out of range in first case after i == 4 and in second case i == 100.

  6. #6
    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

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 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.

  8. #8
    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 spirit View Post
    creates a pointer to a first element of array with size == 11
    Ouch, this statement is online for days and nobody disagrees loudly?

    int *p = new int[10];

    This reserves memory for exactly 10 integers. 0 ... 9, i.e. size == 10.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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.

  10. #10
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 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. )

  11. #11
    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.

  12. #12
    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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...

  14. #14
    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

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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.

  16. #16
    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.

  17. #17
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 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.)

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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.

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

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

    What does, when used correctly?
    Obviously, anything you use as a layer between you and the cpu has a chance of introducing a bug and thus a crash: By 'worst' I did not mean buggy. :-)
    But abstractions like (std:: or Q)vector, map etc just like classes you build upon them help because they allow you to work and think are nearer to the real world than the datastructures C has to offer.
    (I realize that C++ has basically the same and this is rather an issue of good libraries. Only it's more easy and thus likely too use string, array etc 'classes' in C++ than C.)

    Obviously naked C arrays have there place, too.
    (Also in most applications I work with the trouble of checking "does the input fit here", "is this stack based array big enough" etc just is a pain. I see too many places where crashes would occur if the input was only too long. Sure, one can program to prevent that (you can in assembler, too...). But if people don't do it, not much of a help that they could have done it.
    Therefore, in my opinion it is good if a language - or library - allows one not to have to deal with the dangerous world of memory management. (I say allow because now and then you need/want to have to deal with it. But not always and imho definitely not for string operations.)

    And yes, one can write pretty code in C (one can in any language). But having to work with a large C (and partly C++) code base on a daily basis: believe me when I tell you: you can write very ugly code in it, too.
    (The lack of a proper string entity leads to numerous hard to read (often incorrect) for-loops, working on char*, incrementing pointers etc that is way harder to read, debug etc than a e.g. nice simplifyWhitespace(), boost::strip() or whatever. Once again, I agree that one could use (or create) a string-struct in C, and write nice reusable and well named functions for it. But could is theory, and ugly code is my reality ;-) )

    To sum up: it is not a problem with C, or C++, or naked C style arrays. It is a problem of the people using it...

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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
    What does, when used correctly?
    As mentioned, VC6 implementation of STL does.

    But abstractions like (std:: or Q)vector, map etc just like classes you build upon them help because they allow you to work and think are nearer to the real world than the datastructures C has to offer.
    (I realize that C++ has basically the same and this is rather an issue of good libraries. Only it's more easy and thus likely too use string, array etc 'classes' in C++ than C.)
    That's completely another case but then we come back to a question "why use STL if you have Qt?". The latter provides an even more abstract and complete interface with better speed, memory footprint and capabilities.

    (The lack of a proper string entity (...)
    Unfortunately for some time std::string is not a "proper string entity" as it is based on char* as well and in times of globalization this is unacceptable.

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.