×
Namespaces

Variants
Actions

Archived:Open C incorrectly implements offsetof

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Content not applicable to current releases of platform

Article Metadata
Tested with
SDK: S60 3rd Edition MR
Compatibility
Platform(s): S60 3rd Edition
Symbian
S60 3rd Edition (initial release)
Article
Created: mcicciotti (23 Nov 2010)
Last edited: hamishwillee (30 May 2013)

Contents

Overview

Open C defines the offsetof macro incorrectly for some build environments. You have been bitten by this bug if you get errors like this:

file.cpp: At global scope:
file.cpp:6: error: expected primary-expression before ',' token
file.cpp:6: error: expected `)' before ',' token
file.cpp:6: error: `field' was not declared in this scope
file.cpp:6: error: expected `,' or `;' before ')' token

on lines that use offsetof.


Description

Open C's <sys/cdefs.h> misuses the __offsetof__ GCC built-in:

#define __offsetof(type, field)					\
       (__offsetof__ (type,field))

__offsetof__ isn't the same as __builtin_offsetof:


How to reproduce

Compile the following C++ source code with Open C (SYSTEMINCLUDE \epoc32\include\stdapis) and a toolchain with a GCC version 3.x:

#include <cstddef>
#if !defined(__GNUC__) || __GNUC__ >= 4
#error this test is meant for GCC 3.x
#endif
struct type { int field };
static const int x = offsetof(type, field);

Compilation should fail with:

file.cpp: At global scope:
file.cpp:6: error: expected primary-expression before ',' token
file.cpp:6: error: expected `)' before ',' token
file.cpp:6: error: `field' was not declared in this scope
file.cpp:6: error: expected `,' or `;' before ')' token


Solution

Manually change the following lines in <sys/cdefs.h> (\epoc32\include\stdapis\sys\cdefs.h) from:

#define __offsetof(type, field)					\
       (__offsetof__ (type,field))

to:

#define	__offsetof(type, field)	__offsetof__(reinterpret_cast <size_t>	\
(&reinterpret_cast <const volatile char &> \
(static_cast<type *> (0)->field)))


Reference

This page was last modified on 30 May 2013, at 07:08.
94 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×