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

    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 11: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
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 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.

  9. #9
    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. )

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

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

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

Similar Threads

  1. Little confusion Regarding general Initilizaion.. :confused:
    By kunalnandi in forum General Programming
    Replies: 2
    Last Post: 19th September 2008, 11: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.