Thread: why program crashes?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    235

    why program crashes?

    I have problem with this program:
    [C++] bitpacking working version - Pastebin.com
    this is the code I need to cope with in this thread it is temporal version.

    The original version is here:
    [C++] bitpacking.cpp - Pastebin.com
    this is just for comparison.

    Purpose: I am rewriting the original code in C++ to the new version which will be in C. The code does bitpacking using benchmark.

    It crashes on line 2975:
    Code:
    void __fastpack1(const uint32_t *  __restrict__ in, uint32_t *  __restrict__  out) {
        *out |= (*in)   & 1 ; // here

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Mmm - 6000+ lines of the most awful code I've seen in a long while.

    > It crashes on line 2975:
    Run it in the debugger and figure out which of your pointers is invalid.
    - are they both valid (ie memory aligned) to access uint32_t's ?
    - are they both pointing to different places in memory, to honor the restrict qualifier?

    When the debugger has trapped, then do a stack trace to find out the caller chain back to main.

    Is it really any quicker than say 10 lines of code to do the same thing?
    For one thing, a short piece of code in a loop would benefit greatly from being cache resident all the time, than several 1000 lines of straight-line code hammering the external bus for both instructions and data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Not to mention that several 1000 lines are far more prone to errors, much less readable and more time consuming for the programmer.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    I have problem to understand this command:
    Code:
    compressed.resize(N*bit/32, 0);
    so when I have rewritten it to C style I did this:
    Code:
    free(compressed);
    free(recovered);
    t = N*bit/32;
    compressed = (uint32_t *) malloc(t); // in
    recovered = (uint32_t *) malloc(t); // out
    Hence the size is not uint32_t * N but t. But what does really do vector.resize() ? Does it realloc memory? I am not used to C++ I just try to rewrite the C++ version of the code to C version.

  5. #5
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    I think what resize() does is copy the old data to the larger buffer and frees the older one. Of course it also copies the new buffer pointer back to the relevant member of compressed.
    "Highbrow philosophical truth: Everybody is an ape in monkeytown" --Oscar Wilde

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    It doesn't just copy a buffer. It creates copies of each element in the vector, which includes calling the copy constructor. I suspect in C++11 and later, it uses the move constructor, if available.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by the_jackass View Post
    I think what resize() does is copy the old data to the larger buffer and frees the older one. Of course it also copies the new buffer pointer back to the relevant member of compressed.
    It resizes the size of the vector (remember that a vector is essentially an array and an array has a fixed number of elements; in case of a vector, it can change the number of elements it contains at any time).

    Quote Originally Posted by Elkvis View Post
    It doesn't just copy a buffer. It creates copies of each element in the vector, which includes calling the copy constructor. I suspect in C++11 and later, it uses the move constructor, if available.
    It uses the move constructor/move assignment only if they do not throw. If they do, the copy constructor is used because resize has strong exception safety guarantee.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashes
    By mallison359 in forum C Programming
    Replies: 14
    Last Post: 10-31-2014, 06:49 AM
  2. Program crashes at very end
    By Kudose in forum C++ Programming
    Replies: 15
    Last Post: 07-23-2009, 04:06 AM
  3. My program crashes :|
    By Testify in forum C++ Programming
    Replies: 12
    Last Post: 06-27-2007, 11:48 AM
  4. My I/O Program crashes, I need some help
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 01:16 AM
  5. program crashes
    By Strut in forum Linux Programming
    Replies: 3
    Last Post: 02-10-2002, 10:49 AM

Tags for this Thread