I'm coding my own version of Tetris. While creating blockRotation method I encountered weird problem with memcpy. During second call for blockRotate, my bool[4*4] array is not saved corectly, despite that is generated properly. What's wrong?
This is my error log:
arr_shift[0]: true
arr_shift[1]: false
arr_shift[2]: false
arr_shift[3]: false
arr_shift[4]: true
arr_shift[5]: true
arr_shift[6]: false
arr_shift[7]: false
arr_shift[8]: true
arr_shift[9]: false
arr_shift[10]: false
arr_shift[11]: false
arr_shift[12]: false
arr_shift[13]: false
arr_shift[14]: false
arr_shift[15]: false
arr_rot[0]: false
arr_rot[1]: false
arr_rot[2]: true
arr_rot[3]: false
arr_rot[4]: false
arr_rot[5]: false
arr_rot[6]: true
arr_rot[7]: true
arr_rot[8]: false
arr_rot[9]: false
arr_rot[10]: true
arr_rot[11]: false
arr_rot[12]: false
arr_rot[13]: false
arr_rot[14]: false
arr_rot[15]: false
shape[0]: true
shape[1]: false
shape[2]: false
shape[3]: false
shape[4]: true
shape[5]: true
shape[6]: false
shape[7]: false
shape[8]: true
shape[9]: false
shape[10]: false
shape[11]: false
shape[12]: false
shape[13]: false
shape[14]: false
shape[15]: false
arr_shift[0]: false
arr_shift[1]: false
arr_shift[2]: false
arr_shift[3]: false
arr_shift[4]: false
arr_shift[5]: false
arr_shift[6]: false
arr_shift[7]: false
arr_shift[8]: false
arr_shift[9]: false
arr_shift[10]: false
arr_shift[11]: false
arr_shift[12]: false
arr_shift[13]: false
arr_shift[14]: false
arr_shift[15]: false
arr_rot[0]: false
arr_rot[1]: true <---
arr_rot[2]: true <---
arr_rot[3]: true <---
arr_rot[4]: false
arr_rot[5]: false
arr_rot[6]: true <---
arr_rot[7]: false
arr_rot[8]: false
arr_rot[9]: false
arr_rot[10]: false
arr_rot[11]: false
arr_rot[12]: false
arr_rot[13]: false
arr_rot[14]: false
arr_rot[15]: false
shape[0]: false
shape[1]: false <---
shape[2]: false <---
shape[3]: false <---
shape[4]: false
shape[5]: false
shape[6]: false <---
shape[7]: false
shape[8]: false
shape[9]: false
shape[10]: false
shape[11]: false
shape[12]: false
shape[13]: false
shape[14]: false
shape[15]: false
And this is my code:
memset(shape, 0, 16*sizeof(bool));
if(toShift == true) {
memcpy(shape, arr_shift, 16*sizeof(bool));
}
else {
memcpy(shape, arr_rot, 16*sizeof(bool));
}
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
fprintf(stderr, "shape[%i]: %s\n", i*4+j, shape[i*4+j]? "true":"false" );
}
}
fprintf(stderr, "\n\n");
Declararation of shape is in Block class header, and is as follows:
bool shape[16];
You haven't shown us where arr_rot is declared or what value toShift is. However, most likely either toShift is true or you declared where arr_rot points to on the stack in another function.
Related
Is there a way to get clang-format to produce the following formatting:
void foo()
{
if(cond)
{ return; }
}
void f(int x)
{ return 2*x; }
I.e. I want always to see break before {, so I have
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
however, I want short statements to be on a single line, i.e.
{ return; }
// vs
{
return;
}
So far, I am noticing that even with
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
those blocks are expanded into 3 lines.
If I set
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
I still get the same thing
I would like a configuration something like "AllowShortBlocksWhateverTheyAre"
I'm encountering a coding style problem with clang-format. It seems to me that clang-format cannot break initializer list and its function body when it doesn't exceed the column limit.
So basically, this is the code I want:
deque()
: _start(), _finish(), _map(), _map_size()
{
_initialize_map(0);
}
But clang format gives me this:
deque() : _start(), _finish(), _map(), _map_size() { _initialize_map(0); }
There are some format that I'm satisfied with the same config. For example:
explicit deque(size_type count, const value_type &value)
: _start(), _finish(), _map(), _map_size() // Initializers list on one new single line
{
// Break function body
_initialize_map(count);
_fill_construct(value);
}
Here is my full .clang-format:
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: true
AlignConsecutiveBitFields:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveDeclarations:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: All
AlwaysBreakAfterReturnType: All
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: Never
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: true
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Allman
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 0
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: NextLine
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequiresClause: true
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertBraces: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyIndentedWhitespace: 0
PointerAlignment: Right
PPIndentWidth: -1
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
RequiresClausePosition: OwnLine
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
AfterRequiresInClause: false
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Latest
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...
Any comment is appreciated. Thank you!
EDITED:
I know there is a configuration, PackConstructorInitializer: BinPack, that looks like to match with what I want. But somehow, it doesn't work.
Replace
PackConstructorInitializers: NextLine
with
PackConstructorInitializers: BinPack
I have the following BraceWrapping options:
BraceWrapping:
AfterEnum: false
AfterStruct: false
SplitEmptyFunction: false
AfterControlStatement: "Never"
AfterFunction: false
AfterNamespace: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
However, clang-format always inserts a new line after an enum:
enum class event_flags : std::uint8_t
{
running = 1 << 0,
executed = 1 << 1,
};
I want it to be like this:
enum class event_flags : std::uint8_t {
running = 1 << 0,
executed = 1 << 1,
};
what am I doing wrong here?
The one option I could find that seems to fix this formatting for you is outside the BraceWrapping section and that is setting
AllowShortEnumsOnASingleLine: true
even though the description of that option doesn't mention it:
true:
enum { A, B } myEnum;
false:
enum {
A,
B
} myEnum;
I have this option in settings:
Clang_format_fallback Style : Visual studio
Clang_format_style : file
But when I write
int main(){
}
braces are on the same line, but I want to put them on the new line like this:
int main()
{
}
How to achieve that?
edit: settings like
"javascript.format.placeOpenBraceOnNewLineForFunctions": true,
do not help for c++ (only for js)
EDIT2: .clang-format:
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
#AfterExternBlock: false # Unknown to clang-format-5.0
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBraces: Custom
Cpp11BracedListStyle: false
which of these settings are responsible for my issue?
I can't find a configuration for the linebreak behaviour after access modifiers.
(But without setting MaxEmptyLinesToKeep to 0)
(To remove the linebreak after an access modifier change)
I have my current clang configuration like this:
Language: Cpp
CompactNamespaces: false
FixNamespaceComments: false
Cpp11BracedListStyle: true
NamespaceIndentation: All
PointerAlignment: Right
SortIncludes: false
SpacesInParentheses: false
SpaceInEmptyParentheses: false
SpaceAfterTemplateKeyword: false
SortUsingDeclarations: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
IndentWidth: 4
TabWidth: 4
ColumnLimit: 0
AccessModifierOffset: -1
UseTab: true
MaxEmptyLinesToKeep: 2
BreakBeforeBraces: Allman
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Right
KeepEmptyLinesAtTheStartOfBlocks: false
AllowShortFunctionsOnASingleLine: None
BraceWrapping:
AfterFunction: true
AfterClass: true
AfterStruct: true
AfterControlStatement: true
AfterEnum: true
AfterUnion: true
AfterExternBlock: true
AfterNamespace: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
SplitEmptyFunction: true
SplitEmptyRecord: true
IndentBraces: false
BreakBeforeBraces: Custom
Example:
At the moment clang produces:
class Test
{
public:
int test();
private:
int m_test;
}
But i want it to remove the linebreak so that it becomes:
class Test
{
public:
int test();
private:
int m_test;
}