English 中文(简体)
为什么不像2D阵列那样储存所有价值?
原标题:Why does memset not store all values similarly in 2D array?
  • 时间:2024-04-27 23:05:45
  •  标签:
  • c++
  • memset

I have a 2D array set as

long long arr[500][500]

我在担任这一主要职务时,对m=-1和m=-3有不同的价值:

int main(){
    memset(arr,m,sizeof(arr));
    for(int i=0;i<10;i++)
        cout<<arr[i][i]<< 
 ;
    return 0;
}

m=-3:每20次印刷——144 680345676153347,每20次,每小时1美元,即1美元。 原因何在?

#include<iostream>
#include<cstring>
using namespace std;
using ll=long long;

ll dp[501][501];

int main(){
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
    //int n;
    //cin>>n;
    memset(dp,-3,sizeof(dp));
    for (int i = 0; i < 20; i++) {
        cout<<dp[i][i]<< 
 ;
    }
    return 0;
}
问题回答

这是因为:memset在记忆中,而不是在分类中打上个人bytes。 我只使用下文中的“integer”一词,提及你具体的64-bit 2s-complement long Long, 类型(1),因为同一行为适用于所有2s-complement integer类型。

Using -1 works because it stores 0xff in every byte and it just happens that the integer value 0xffffffffffffffff is also -1.

但是,-3 Store0xfd和价值0xfdfdfdfdfdnot-3(将0xffffffffd.

如果您转换“0xfd 值为愤怒,请看一下:-144,680,345,676,153,347'。

除非你知道,反复出现的细化模式会产生与惯性相同的价值(例如,0x000xff),否则,你可能会更好地利用一种 lo机来确定这些数值(与你照相),例如:

#include <iostream>

#define DIM 500
long long arr[DIM][DIM];

int main(){
    for(size_t dim1 = 0; dim1 < DIM; ++dim1) {
        for(size_t dim2 = 0; dim2 < DIM; ++dim2) {
            arr[dim1][dim2] = -3;
        }
    }

    for(size_t idx = 0; idx < 3; ++idx) {
        std::cout << arr[idx][idx] <<  
 ;
    }

    return 0;
}

如所期望的那样,这将产生:

-3
-3
-3

<>> 假设你的系统有64-bit long long的类型是合理的,因为0xfdfdfdfd的32-bit 2s-complement Value is -33,686,019, 你是not>/em see.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?