c99

Is it safe to assume sizeof(double) >= sizeof(void*)?

狂风中的少年 提交于 2019-12-06 14:50:49
Is it safe to assume that sizeof(double) will always be greater than or equal to sizeof(void*) ? To put this in some context, is the following portable? int x = 100; double tmp; union { double dbl; void* ptr; } conv; conv.ptr = (void*)&x; tmp = conv.dbl; conv.dbl = tmp; printf("%d\n", *((int*)conv.ptr)); It does work on the few machines that I've tested it on, but I can see this going horribly wrong if sizeof(void*) > sizeof(double) . On current systems yes. double is 64 bits on all current and future systems because they're aligned with IEEE arithmetic's double-precision. It's unlikely but

Is negating INT_MIN undefined behaviour?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 09:31:55
问题 Let's say I have a variable i that comes from external sources: int i = get_i(); Assuming i is INT_MIN and two's complement representation, is -i undefined? 回答1: It depends on the platform. C supports three representations for negative numbers (see section 6.2.6.2 of the C99 standard): Two's complement. One's complement. Sign and magnitude. With one's complement and sign and magnitude, -INT_MIN is defined (and equal to INT_MAX ). With two's complement, it depends on whether the value with

atexit considered harmful?

江枫思渺然 提交于 2019-12-06 08:02:13
问题 Are there inherent dangers in using atexit in large projects such as libraries? If so, what is it about the technical nature behind atexit that may lead to problems in larger projects? 回答1: The main reason I would avoid using atexit in libraries is that any use of it involves global state. A good library should avoid having global state. However, there are also other technical reasons: Implementations are only required to support a small number (32, I think) of atexit handlers. After that, it

scanf not to exceed buffer overrun

江枫思渺然 提交于 2019-12-06 04:59:57
I have a buffer and I don't want the user to enter more characters than the buffer can hold (to avoid a buffer overrun). I am using scanf and have done like this: char buffer[30] = {'\0'}; scanf("%30s", buffer); However, I know I am protected if the user enters more than 30. However, if the user enters more than 30, will the buffer be null terminated? scanf() with a "%s" conversion specifier adds a terminating null character to the buffer. But , you're asking for 30 characters, which really means 31 and only have space for 30. You should use a maximum field width of 29. char buffer[30] = {'\0'

ARC warning: Implicit declaration of function 'DLog' is invalid in C99

情到浓时终转凉″ 提交于 2019-12-06 04:31:25
I use DLog macro in an ARC project, and I got the warning: Implicit declaration of function 'DLog' is invalid in C99 You can find DLog from http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog How to fix this warning? I actually had this warning when calling Dlog without a capital L. I changed it into DLog and everything worked fine. But you seem to have it correct. Make sure your Prefix.pch file is included in 'Build Settings' -> 'Prefix Header' 来源: https://stackoverflow.com/questions/9799838/arc-warning-implicit-declaration-of-function-dlog-is-invalid-in-c99

Is this use of the Effective Type rule strictly conforming?

℡╲_俬逩灬. 提交于 2019-12-06 04:29:51
The Effective Type rule in C99 and C11 provides that storage with no declared type may be written with any type and, that storing a value of a non-character type will set the Effective Type of the storage accordingly. Setting aside the fact that INT_MAX might be less than 123456789, would the following code's use of the Effective Type rule be strictly conforming? #include <stdlib.h> #include <stdio.h> /* Performs some calculations using using int, then float, then int. If both results are desired, do_test(intbuff, floatbuff, 1); For int only, do_test(intbuff, intbuff, 1); For float only, do

_Bool data type of C99

余生长醉 提交于 2019-12-06 04:23:11
The C99 standard of the C programming language defines the _Bool data type as a macro for another data type (as the language isn't able to deal with a type safe boolean). Is the _Bool a macro for unsigned char , unsigned int or some other data type? _Bool is a separate integere type that according to the C Standard. _Bool is a keyword of the C language. 2 An object declared as type _Bool is large enough to store the values 0 and 1. _Bool is unsigned integer type. The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer

What is the correct definition of size_t? [duplicate]

冷暖自知 提交于 2019-12-06 03:15:41
问题 This question already has answers here : What is size_t in C? (12 answers) Closed 4 years ago . First of all, what do I mean, by 'correct definition`? For example, K&R in "C Programming Language" 2nd ed. , in section 2.2 Data Types and Sizes , make very clear statements about integers: There are short , int and long for integer types. They are needed to repesent values of different boundaries. int is a "naturally" sized number for a specific hardware, so also probably the most fastest. Sizes

Does MISRA C 2012 say not to use bool

孤者浪人 提交于 2019-12-06 02:49:28
问题 I am in the early stages of framing stuff out on a new project. I defined a function with a return type of "bool" I got this output from PC-Lint Including file sockets.h (hdr) bool sock_close(uint8_t socket_id); ^ "LINT: sockets.h (52, 1) Note 970: Use of modifier or type '_Bool' outside of a typedef [MISRA 2012 Directive 4.6, advisory]" I went ahead and defined this in another header to shut lint up: typedef bool bool_t; Then I started wondering why I had to do that and why it changed

Strict aliasing rules for allocated objects

为君一笑 提交于 2019-12-06 01:38:43
C99 6.5/6 The effective type of an object for an access to its stored value is the declared type of the object, if any. 75) If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, then the type of the lvalue becomes the effective type of the object for that access and for subsequent accesses that do not modify the stored value. If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type of the modified object for that access and for