<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Feature request: add sizeof extension for structs with flexible array member"
href="https://bugs.llvm.org/show_bug.cgi?id=37243">37243</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Feature request: add sizeof extension for structs with flexible array member
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>Other
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Frontend
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>vicencb@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>Hi
It would be nice to have an extension that reports the size of a struct with a
flexible array member.
Calculating the size of such an struct is a bit cumbersome if alignment is to
be taken into account.
Also, this feature nicely fits with other compiler built-ins like 'sizeof'.
Currently, writing some code like this is required:
#include <stdalign.h>
#include <stddef.h>
typedef struct strc {
unsigned len;
char val[];
} strc;
static inline size_t strc_size(size_t length) {
static const size_t alignment = alignof(strc);
static const size_t offsetofmember = offsetof(strc, val);
static const size_t sizeofmember0 = sizeof(((strc
*)0)->val[0]);
static const unsigned offsetofmember_is_aligned = offsetofmember ==
((offsetofmember + alignment - 1) / alignment) * alignment;
static const unsigned sizeofmember0_is_aligned = sizeofmember0 ==
((sizeofmember0 + alignment - 1) / alignment) * alignment;
static const unsigned always_aligned = offsetofmember_is_aligned
&& sizeofmember0_is_aligned;
size_t packed_size = offsetofmember + length * sizeofmember0;
size_t padded_size = ((packed_size + alignment - 1) / alignment ) *
alignment;
return always_aligned ? packed_size : padded_size;
}
// That function is generic enough that can be moved into a macro
// for any struct with a flexible array member:
#define sizeof_struct(type, member) size_t type##_size(size_t length) {...}
This extension could be provided on top of the 'sizeof' operator as an extra
optional argument:
sizeof(strc, 7) == strc_size(7)
sizeof(strc) == strc_size(0)
sizeof strc == strc_size(0)
Regards,
Vicente.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>