Unexplained C++ default int values

前端 未结 6 1581
悲哀的现实
悲哀的现实 2021-01-12 04:55

I\'ve been refactoring some code and I noticed some wonky behavior involving an uninitialized int array:

int arr[ARRAY_SIZE];

I set a break

6条回答
  •  广开言路
    2021-01-12 05:24

    You need to explicitly set the values for the array - they do not "default" to anything. Try:

    memset(mouseBufferX,0,sizeof(mouseBufferX));
    
    //or 
    
    int mouseBufferX[mouseBufferSize] = {0};
    
    //and, in C++ this *might* work too (fuzzy memory!):
    
    int mouseBufferX[mouseBufferSize] = {};
    

提交回复
热议问题