Skip to content

[BUG]: VLA not supported in MSVC #5

Open
@BatMW

Description

@BatMW

CThreads commit

5deaa64

Description

The function cthreads_error_string does not compile on any compilers that do not support variable-length array (VLA).
VLAs are part of the C99 standard but seems to be only supported by GCC and Clang (cppreference.com).

suggested solutions

  • static size array using #define CTHREADS_ERROR_MSG_SIZE [Some reasonable size]
  • malloc based on size_t length
  • If you want to keep VLA for GCC and Clang and still support other compilers:
#if defined(__GNUC__) || defined(__clang__)
    #define VLA_SUPPORTED 1
#else
    #define VLA_SUPPORTED 0
#endif

//Implement 2 functions based on if VLA is supported or not

Steps to reproduce

Compile CThreads using MSVC.

Example code

> cl /TC /W4 /DCTHREADS_DEBUG test.c cthreads.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.42.34436 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.c
C:\prog\CThreads\cthreads.h(460): error C2057: expected constant expression
C:\prog\CThreads\cthreads.h(460): error C2466: cannot allocate an array of constant size 0
cthreads.c
C:\prog\CThreads\cthreads.h(460): error C2057: expected constant expression
C:\prog\CThreads\cthreads.h(460): error C2466: cannot allocate an array of constant size 0
cthreads.c(465): error C2057: expected constant expression
cthreads.c(465): error C2466: cannot allocate an array of constant size 0
Generating Code...
//Line 460 in cthreads.h
size_t cthreads_error_string(size_t length, char buf[length], int error_code);

Confirmations

  • My environment meets the minimum requirements.
  • I have verified that this is not a duplicate issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingconfirmedThis issue or pull request is confirmed to be done.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions