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

  1. #16
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by flp1969 View Post
    Thank you! This is very useful!
    No worries. Perhaps I'm being overly pedantic because it's unlikely that the OP is compiling on an architecture with 12 bit bytes (it doesn't look like an embedded program which is where you're most likely, these days, to encounter bytes with > 8 bits), but in the future who knows... maybe x86 will decide to have 12 or more bit bytes and the code would have to be rewritten if it was multiplying by 8. FWIW I thought of bytes as 8 bits for a long long time until I was programming a PIC with 12 bit bytes (edit: or maybe it was a 16 bits... can't remember) and I got... bitten (or is that byten or byted?)

    Edit 2: It seems that there are de facto and no-so-defacto standards that say a byte is 8 bits (but that's not C's definition)
    Last edited by Hodor; 01-15-2020 at 03:57 AM.

  2. #17
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Hodor View Post
    FWIW I thought of bytes as 8 bits for a long long time until I was programming a PIC with 12 bit bytes (edit: or maybe it was a 16 bits... can't remember) and I got... bitten (or is that byten or byted?)
    Yep... I've heard about antique systems with 9 bit bytes and 36 bits word size! (PDP-7?)

  3. #18
    Registered User
    Join Date
    Jan 2020
    Posts
    5
    This check (if (needed > *capacity)) should be correct since if I need to fit "needed" amount of elements into the array - the array needs to be able to hold at least that many elements, therefore if "needed" == "*capacity" => do nothing.

    Also, while I DO appreciate the various ways of optimizing this, it's not really necessary and I will probably leave it as "pow(2, ceil(log2(needed)));" since it is more readable. Also if increase_nodes_capacity function is called and the current capacity is 128 but 1000 is needed it would just resize the array to 1024 straight away, compared to this code "while(capacity < needed) capacity *= 2;" that would do it gradually => 128 -> 256 -> 512 -> 1024 and copy the old array into the bigger one every time. See EDIT 2*.
    The optimization should not be needed since this function would only run a few thousand times and the actual resize would happen maybe up to 512 or 1024 elements. For the reference, some other parts of the code are run millions if not billions of times.

    EDIT: I still can't figure out why my program is crashing.
    EDIT 2: Nvm, I figure out what you meant, my bad. I will probably change my code to "
    while(capacity < needed) capacity *= 2;" for readability.
    Last edited by TypicalHog; 01-15-2020 at 07:47 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