microsoft / microsoft/vscode-cpptools
`sizeof` may give incorrect result in editor for structs containing bitfields
Open
Nobody has claimed this yet.
Language Service
more votes needed
Visual Studio
- Dominant language
- TypeScript
- Stars
- 6.2k
- Forks
- 1.7k
- Avg merge
- 14h 46m
- Merged PRs (30d)
- 61
Description
Bug type: Language Service
Describe the bug
- OS and Version: Ubuntu 20.04
- VS Code Version: 1.61.0
- C/C++ Extension Version: v1.7.0-insiders2
- Other extensions you installed (and if the issue persists after disabling them): N/A
- If using SSH remote, specify OS of remote machine: N/A
- A clear and concise description of what the bug is, including information about the workspace (i.e. is the workspace a single project or multiple projects, size of the project, etc).
Steps to reproduce
Code sample 1
#include <iostream>
struct test1 {
unsigned int a : 4;
unsigned int b : 4;
unsigned char c;
unsigned char d;
};
int main() {
std::cout << sizeof(test1) << std::endl;
}
Expected behavior
sizeof(test1)gives 4 when the program runs. (when memory alignment is set to 4 bytes)- However in the editor it shows 8.
Code sample 2
This time I add #pragma pack(1).
#include <iostream>
#pragma pack(1)
struct test1 {
unsigned int a : 4;
unsigned int b : 4;
unsigned char c;
unsigned char d;
};
int main() {
std::cout << sizeof(test1) << std::endl;
}
Expected behavior
sizeof(test1)gives 3 when the program runs. (when memory alignment is set to 1 byte)- However in the editor it shows 6.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue in the C/C++ extension with the two struct samples provided, comparing the editor's sizeof result with the program output. Trace the language-service handling of sizeof for bitfields, alignment, and #pragma pack. Done means the editor reports 4 and 3 for the two examples under the stated packing settings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, typescript
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100