NSInteger versus int [duplicate] - nsinteger

This question already has answers here:
When to use NSInteger vs. int
(8 answers)
Closed 8 years ago.
What would be the reason to use an NSInteger vs an int in iPhone programming? Thanks.

An NSInteger is just a typedef with the following definition:
#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
As the typedef is defined differently basing on the platform, you can keep using NSInteger independently from the platform.

Related

preprocessor define with #pragma directive [duplicate]

This question already has answers here:
Pragma in define macro
(4 answers)
Closed 4 months ago.
I would like to do a #define which contains a #pragma directive but I got the following error.
Any idea?
#define FunctionPar_Begin typedef struct fpar { #pragma pack(4)
error C2121: '#': invalid character: possibly the result of a macro expansion
The one you probably want:
#define FunctionPar_Begin typedef struct fpar { _Pragma("pack(4)")
MSVC also allows for:
#define FunctionPar_Begin typedef struct fpar { __pragma(pack(4))
Clang/gcc also allow:
#define FunctionPar_Begin typedef struct fpar { __attribute__((packed, aligned(4)))

_ADDRESSOF(v) macro definition in VS2019 C++ [duplicate]

This question already has answers here:
Implementation of addressof
(4 answers)
Why and when is cast to char volatile& needed?
(2 answers)
Closed last year.
In <vadefs.h> you'll find the following definition for the macro _ADDRESSOF(v) in VS2019:
#ifdef __cplusplus
#define _ADDRESSOF(v) (&const_cast<char&>(reinterpret_cast<const volatile char&>(v)))
#else
#define _ADDRESSOF(v) (&(v))
#endif
I'd like to understand the first definition above, when __cplusplus is defined. Why does it work?

How to declare a pointer to a nested C++ class in C [duplicate]

This question already has answers here:
Developing C wrapper API for Object-Oriented C++ code
(6 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I have a nested class in C++ that I want C code to be able to use. Because it is nested I cannot forward declare it to C, so instead we have code like this in a shared header file:
#ifdef __cplusplus
class Mgr {
public:
class Obj {
public:
int x;
};
};
typedef Mgr::Obj * PObj;
#else
typedef void* PObj;
#endif
This results in C and C++ each seeing a different definitions of PObj which I fear violates the one definition rule. However it is "just" a pointer, so I'm not sure if this can go wrong or not.
C does nothing with the pointer besides pass it along to C++ functions which are wrappers for methods. For instance we'll have this in the combined header:
#ifdef __cplusplus
extern "C" {
#endif
int obj_GetX(PObj pObj);
#ifdef __cplusplus
}
#endif /* __cplusplus */
With this as the implementation in the C++ file:
int obj_GetX(PObj pObj) {
return pObj->x;
}
So:
Is this really a problem?
If so, what is the best way to share this pointer between C and C++
It's not a problem per-se, but you might violate the strict aliasing rule if and when you cast that void * back to Mgr::Obj * in your C++ code.
There's no good solution to this - it is (to me) a glaring omission in the standard. The best you can do is to compile any code which does such casts with the -fno-strict-aliasing flag. I do this extensively and have never had a problem performing casts in this manner.

What is the difference between #ifdef VALUE vs #if defined (VALUE) [duplicate]

This question already has answers here:
Is #if defined MACRO equivalent to #ifdef MACRO?
(5 answers)
Closed 4 years ago.
#include<stdio.h>
#define MAX 0
int main()
{
#ifdef MAX
printf("MAX defined");
#endif
#if defined (MAX)
printf("MAX is defined");
#endif
return 0;
}
Both the #ifdef and #if defined give the same effect then what is the difference between them? I have not seen the disassembly code of these directives if you have seen then kindly try to explain that as well.
The difference is historical. Originally there was only #ifdef. The newer syntax is more flexible and allows combining tests with logical conditions, but in the simple form you can use them interchangeably.

syntax error while using __int8 in c++

I have a header file which has function definitions for some of the APIs of a device to which I'm trying to communicate. I created a c++ project in eclipse juno and copied the header files into that project. When I open the header file I see a strange syntax error. The header file goes like this:
#ifndef AnaGate_h
#define AnaGate_h
// platform-independent fixed-size integer types
#ifdef WIN32
typedef signed __int8 AnaInt8;
typedef signed __int16 AnaInt16;
typedef signed __int32 AnaInt32;
typedef signed __int64 AnaInt64;
typedef unsigned __int8 AnaUInt8;
typedef unsigned __int16 AnaUInt16;
typedef unsigned __int32 AnaUInt32;
typedef unsigned __int64 AnaUInt64;
#else
// C99 standard header, may not be included with all (especially older) compilers.
#include <stdint.h>
typedef int8_t AnaInt8;
typedef int16_t AnaInt16;
typedef int32_t AnaInt32;
typedef int64_t AnaInt64;
typedef uint8_t AnaUInt8;
typedef uint16_t AnaUInt16;
typedef uint32_t AnaUInt32;
typedef uint64_t AnaUInt64;
#endif
The syntax error is for lines 5-12. ( starting from typedef signed __int8 AnaInt8). I'm not able to understand why it is syntax error. Any help is greatly appriciated