source: cpp/common/realloc-free0size.cpp @ 1327

Last change on this file since 1327 was 1314, checked in by Maciej Komosinski, 6 months ago

Added realloc() implementation that behaves like free() when size==0 - consistently on all platforms, in contrast to standard realloc()

File size: 694 bytes
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 2023-2024  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "realloc-free0size.h"
6#include <common/log.h>
7
8// https://stackoverflow.com/questions/16759849/using-realloc-x-0-instead-of-free-and-using-malloc-with-length-of-a-string
9void *realloc_free0size(void *ptr, size_t size)
10{
11        // special case begin
12        if (ptr && size == 0)
13        {
14                free(ptr);
15                return NULL;
16        }
17        // special case end
18
19        void *tmp = realloc(ptr, size);
20        if (size != 0 && tmp == NULL)
21        {
22                logPrintf("", "realloc_free0size", LOG_CRITICAL, "realloc(%zd) failed.", size);
23        }
24        return tmp;
25}
Note: See TracBrowser for help on using the repository browser.