Skip to content

Use ValueFlow::getSizeOf() in CheckOther#6257

Merged
chrchr-github merged 5 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_getsizeof
Apr 10, 2024
Merged

Use ValueFlow::getSizeOf() in CheckOther#6257
chrchr-github merged 5 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_getsizeof

Conversation

@chrchr-github
Copy link
Copy Markdown
Collaborator

No description provided.

@chrchr-github chrchr-github marked this pull request as ready for review April 8, 2024 19:43
@chrchr-github chrchr-github merged commit bca43c2 into cppcheck-opensource:main Apr 10, 2024
@chrchr-github chrchr-github deleted the chr_getsizeof branch April 10, 2024 15:55
@autoantwort
Copy link
Copy Markdown
Contributor

I don't know if this is intended, but before this commit for the following code:

struct SomeClass{
	std::string name;
	NonClass test;
}

void test(const std::vector<SomeClass>& t){
	for(const auto entry : t){
		std::cout << entry.name << std::endl;
	}
}

the following warning was emitted.

example_loop.cpp:7:17: performance: Range variable 'entry' should be declared as const reference. [iterateByValue]

After this commit you don't get the warning anymore. You only get it if you remove or comment out the line 3 NonClass test;.
It seems to me that the size of the whole class becomes zero if the function can not determine the size of one of it members in comparison to earlier where it seems that such a member was simply ignored.

@chrchr-github
Copy link
Copy Markdown
Collaborator Author

I don't know if this is intended, but before this commit for the following code:

This is tracked in https://trac.cppcheck.net/ticket/13696

chrchr-github pushed a commit that referenced this pull request Jun 16, 2025
… empty structs have a size of 1 (#7560)

Fixes https://trac.cppcheck.net/ticket/13696 +
#6257 (comment)
One could argue that the size of a member of an unknown type should be 1
instead of 0 because the member will be at least 1 byte large (if not
empty and annotated with `[[no_unique_address]]`)

---------

Co-authored-by: Leander Schulten <Leander.Schulten@tetys.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants