Re: Why does this crash?

Discussion in 'C++' started by Alf P. Steinbach, Jul 18, 2003.

  1. On Fri, 18 Jul 2003 07:37:11 GMT, Curt <> wrote:

    > if ( *((int*)data) == 0 )



    High-level reason: it crashes because it's a too low-level
    C design instead of C++.

    Low-level reason: it's possible that it crashes due to bad
    memory alignment (that 'data', regarded as a memory address,
    isn't a multiple of, say, 8). It's also possible that it
    crashes because 'data' is an invalid pointer, e.g. pointing
    to something that's been deallocated, or an arbitrary pointer
    value. The latter reason is the most likely one.
    Alf P. Steinbach, Jul 18, 2003
    #1
    1. Advertising

  2. "Alf P. Steinbach" <> wrote in message
    news:...
    > On Fri, 18 Jul 2003 07:37:11 GMT, Curt <> wrote:
    >
    > > if ( *((int*)data) == 0 )

    >
    >
    > High-level reason: it crashes because it's a too low-level
    > C design instead of C++.
    >
    > Low-level reason: it's possible that it crashes due to bad
    > memory alignment (that 'data', regarded as a memory address,
    > isn't a multiple of, say, 8). It's also possible that it
    > crashes because 'data' is an invalid pointer, e.g. pointing
    > to something that's been deallocated, or an arbitrary pointer
    > value. The latter reason is the most likely one.
    >


    I thought C++ guaranteed that new char[] returned a pointer suitably aligned
    for any data type. Of course the OP's compiler may not be following that
    requirement, or he may have overloaded operator new.

    john
    John Harrison, Jul 18, 2003
    #2
    1. Advertising

  3. Alf P. Steinbach

    vijay Guest

    This code compiles perfectly on my comp, windows C++ compler

    int main(int argc, char* argv[])
    {
    int ov_size =10;
    char *data = new char[ov_size ];
    data = "hi all";
    if ( *((int*)data) == 0 )
    {

    }
    else
    {
    std::cout<<data<<std::endl;
    }
    return 0;
    }

    I guess probably u could have missed a one = in == sign in the if
    ie following code crashes , an = sign missing, this is for obvious reasons,
    int main(int argc, char* argv[])
    {
    int ov_size =10;
    char *data = new char[ov_size ];
    data = "hi all";
    if ( *((int*)data) = 0 ) //observe an equal = is missing
    {

    }
    else
    {
    std::cout<<data<<std::endl;
    }
    return 0;
    }

    Vijay
    vijay, Jul 18, 2003
    #3
  4. vijay wrote:
    >
    > This code compiles perfectly on my comp, windows C++ compler


    .... nevertheless it contains at least one bug :)
    your program leaks memory.

    Thats the simpler one. I leave it to you to find the other one.

    >
    > int main(int argc, char* argv[])
    > {
    > int ov_size =10;
    > char *data = new char[ov_size ];
    > data = "hi all";
    > if ( *((int*)data) == 0 )
    > {
    >
    > }
    > else
    > {
    > std::cout<<data<<std::endl;
    > }
    > return 0;
    > }
    >
    > I guess probably u could have missed a one = in == sign in the if
    > ie following code crashes , an = sign missing, this is for obvious reasons,


    It may crash. Your code has undefined behaviour due to writing
    to a constant character string. And I wouldn't say it is very
    obvious in the presented code.

    > int main(int argc, char* argv[])
    > {
    > int ov_size =10;
    > char *data = new char[ov_size ];
    > data = "hi all";
    > if ( *((int*)data) = 0 ) //observe an equal = is missing
    > {
    >
    > }
    > else
    > {
    > std::cout<<data<<std::endl;
    > }
    > return 0;
    > }
    >


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jul 18, 2003
    #4
  5. Alf P. Steinbach

    Curt Guest

    (Alf P. Steinbach) wrote in news:3f17a769.74548921
    @News.CIS.DFN.DE:

    > On Fri, 18 Jul 2003 07:37:11 GMT, Curt <> wrote:
    >
    >> if ( *((int*)data) == 0 )

    >
    >
    > High-level reason: it crashes because it's a too low-level
    > C design instead of C++.


    Granted, but thats the beauty of c++, it lets you get down and dirty to
    bare-metal when you need to.

    >
    > Low-level reason: it's possible that it crashes due to bad
    > memory alignment (that 'data', regarded as a memory address,
    > isn't a multiple of, say, 8). It's also possible that it
    > crashes because 'data' is an invalid pointer, e.g. pointing
    > to something that's been deallocated, or an arbitrary pointer
    > value. The latter reason is the most likely one.
    >


    Well its not an invalid/arbitrary pointer, thats a simple bug to find/fix.
    But now that you mention it new[] IS overloaded in this program, we are
    using a third party memory manager (Hoard). Its supposed to be
    stable/tested on Sparc hardware, but I'm gonna try linking this program
    without it and seeing if that does the trick.

    -Curt
    Curt, Jul 18, 2003
    #5
  6. Alf P. Steinbach

    Curt Guest

    "vijay" <> wrote in
    news:bf8de6$5mn$:

    > This code compiles perfectly on my comp, windows C++ compler
    >
    > []
    >


    Yes, thats the frustrating part, its perfectly normal code under most
    circumstances, and compiles/runs fine all day long, so its got to be
    something else. Was that compiled under x86 architecture, or Sparc?

    >
    > I guess probably u could have missed a one = in == sign in the if
    > ie following code crashes , an = sign missing, this is for obvious


    Granted, its one of the things I checked for. I don't make that mistake
    much anymore.. but I do make it ;)

    -Curt
    Curt, Jul 18, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Developwebsites

    why does it crash?

    Developwebsites, Oct 10, 2003, in forum: C++
    Replies:
    6
    Views:
    351
    Jerry Coffin
    Oct 11, 2003
  2. William Payne
    Replies:
    2
    Views:
    375
    William Payne
    Aug 2, 2004
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    856
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
  5. Phi!
    Replies:
    1
    Views:
    171
Loading...

Share This Page