c99

Smart pointers/safe memory management for C?

て烟熏妆下的殇ゞ 提交于 2019-11-27 00:17:02
问题 I, and I think many others, have had great success using smart pointers to wrap up unsafe memory operations in C++, using things like RAII, et cetera. However, wrapping memory management is easier to implement when you have destructors, classes, operator overloading, et cetera. For someone writing in raw C99, where could you point (no pun intended) to help with safe memory management? Thanks. 回答1: The question is a bit old, but I figured I would take the time to link to my smart pointer

Which functions in the C standard library commonly encourage bad practice? [closed]

别等时光非礼了梦想. 提交于 2019-11-26 23:51:20
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . This is inspired by this question and the comments on one particular answer in that I learnt that strncpy is not a very safe string handling function in C and that it pads zeros, until it reaches n , something I was unaware of. Specifically, to quote R.. strncpy does not null

What is the default C mode for the current gcc (especially on Ubuntu)?

走远了吗. 提交于 2019-11-26 21:58:20
When I ask to see the current version of cc I get this. $ cc --version cc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 Copyright (C) 2012 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ What I would like to know is which of c89, c90, c99 or c11 is being used. This is explained in depth in the gcc manual, available (if it's installed) by typing info gcc or online here . The relevant section of the 4.7.2 manual is here . By default, gcc does not conform to any of the

Dynamic array allocation on stack in C

房东的猫 提交于 2019-11-26 21:39:48
问题 I just did a experiment yesterday, and find something confusing: #include <stdio.h> int main() { int j; scanf("%d",&j); const int i = j; int arr[i]; return 0; } The number j is read from keyboard and it’s used to allocate the array arr on the stack. The compiler does not even know the size of the array at compile time (initializes j to 0?), but there is no compilation error. How is it possible? 回答1: Variable length arrays were added to C99. It's described in the C99 rationale: 6.7.5.2 Array

C Complex Numbers in C++?

吃可爱长大的小学妹 提交于 2019-11-26 21:34:30
问题 The following code compiles and runs just fine in C (at least according to 'gcc -std=gnu99'), but it fails to compile under C++, giving "line 5: error: cannot convert 'double' to 'double complex ' in initialization". Does anybody know why? #include "/usr/include/complex.h" #include <stdio.h> int main(int argc, char * argv[]) { double complex a = 3; // ERROR ON THIS LINE printf("%lf\n", creal(a)); return 0; } I realize there is another way of doing complex numbers in C++, but I have to use C

Adding two floating-point numbers

天涯浪子 提交于 2019-11-26 21:33:56
问题 I would like to compute the sum, rounded up, of two IEEE 754 binary64 numbers. To that end I wrote the C99 program below: #include <stdio.h> #include <fenv.h> #pragma STDC FENV_ACCESS ON int main(int c, char *v[]){ fesetround(FE_UPWARD); printf("%a\n", 0x1.0p0 + 0x1.0p-80); } However, if I compile and run my program with various compilers: $ gcc -v … gcc version 4.2.1 (Apple Inc. build 5664) $ gcc -Wall -std=c99 add.c && ./a.out add.c:3: warning: ignoring #pragma STDC FENV_ACCESS 0x1p+0 $

struct bitfield max size (C99, C++)

做~自己de王妃 提交于 2019-11-26 20:57:03
问题 What is maximal bit width for bit struct field? struct i { long long i:127;} Can I define a bit field inside struct, with size of bitfield up to 128 bit, or 256 bit, or larger? There are some extra-wide vector types, like sse2 (128-bit), avx1/avx2 (256-bit), avx-512 (512-bit for next Xeon Phis) registers; and also extensions like __int128 in gcc. 回答1: C99 §6.7.2.1, paragraph 3: The expression that specifies the width of a bit-field shall be an integer constant expression that has nonnegative

What is the official status of C99 support in VS2013?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 20:43:18
I see that VS2013 added support for a large number of major core language features of C99. Now it supports compound literals, designated initializers, variadic macros, interleaved declarations and statements just to name a few. This indicates that VS developers made serious steps towards C99 support in Visual Studio. Yet, some of these features are not part of C++ language, which appears to be a notable deviation from the previously announced development strategy (e.g. "VS C compiler will only support those C99 features that are also a part of C++"). So, is there any official or semi-official

What's the difference between “int” and “int_fast16_t”?

夙愿已清 提交于 2019-11-26 20:19:19
问题 As I understand it, the C specification says that type int is supposed to be the most efficient type on target platform that contains at least 16 bits. Isn't that exactly what the C99 definition of int_fast16_t is too? Maybe they put it in there just for consistency, since the other int_fastXX_t are needed? Update To summarize discussion below: My question was wrong in many ways. The C standard does not specify bitness for int . It gives a range [-32767,32767] that it must contain. I realize

How to declare an inline function in C99 multi-file project?

怎甘沉沦 提交于 2019-11-26 19:58:41
问题 I want to define an inline function in a project, compiled with c99. How can I do it? When I declare the function in a header file and give the detail in a .c file, the definition isn't recognized by other files. When I put the explicit function in a header file, I have a problem because all .o files who use it have a copy of the definition, so the linker gives me a "multiple definition" error. What I am trying to do is something like: header.h inline void func() { do things... } lib1.c