Thread: C - realloc() I Keep getting Segmentation fault (core dumped)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Hodor View Post
    I think there is a typo here:


    Should really be using __builtin_clzll otherwise the calculation will be incorrect. Also, I believe, that sizeof(size_t) should be multiplied to get the number of bits in size_t. (also added john.c's suggestion)

    Code:
        new_capacity = 1ULL << ( sizeof ( size_t ) * CHAR_BIT - __builtin_clzll( needed - 1 ));

    Although it's clear I think it's important to mention that it's not portable code
    You are right: __builtin_clzll() and it is not portable... and I forgot sizeof returns the number of bytes, so I need to multiply sizeof( size_t ) by 8 as well...

    There is a small confusion with the use of CHAR_BIT... this is the number of bits of char type. A byte is defined as 8 bits long (since 1960's I believe).

    Thanks, anyway, for the correction!
    Last edited by flp1969; 01-15-2020 at 02:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (Core Dumped)
    By Alma in forum C Programming
    Replies: 2
    Last Post: 09-29-2017, 02:14 PM
  2. Segmentation fault (core dumped) in #c?
    By hs101 in forum C Programming
    Replies: 9
    Last Post: 12-06-2012, 02:38 PM
  3. Segmentation Fault (core dumped)
    By Kevin Jerome in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2012, 12:58 AM
  4. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  5. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM

Tags for this Thread