### UpS0lver's blog

By UpS0lver, 7 years ago,

just I noticed that memset doesn't work well with long double arrays
so is there any way to use it with long double if not what's the effective way to fill long double arrays without using nested loops

long double memo[50][50][50][50];
memset(memo,-1,sizeof memo);
cout<<memo[0][0][0][0]<<endl; //value will be nan


I use this array for memoization
also I noticed that if I used memset with -1 then if I checked if this transitions seen before or not

long double &re=memo[i][j][k][l];
if(re != -1)return re; //doesn't work
if(re  > -1)return re; //worked well


Thanks

• +14

 » 7 years ago, # | ← Rev. 4 →   +18 memset initilizes array with given bytes. For example, in this code: int a[100]; memset(a, -1, sizeof a); all elements will be equal to -1, but in this code memset(a, 127, sizeof a); all elements will be very large numbers (2139062143).Next program will output "-nan": #include using namespace std; int main() { int x = -1; cout << (*((float*)(&x))) << endl; } It happens because of different representation in memory of integer and float numbers.If you want to fill float array, you can use function "fill(a, a + n, -1)". In case of more dimensions, use "fill((long double*)a, (long double*)a + 50 * 50 * 50 * 50, -1)".
•  » » 7 years ago, # ^ |   0 Can you please explain why memset(a, 127, sizeof a); will fill the array with very large numbers?
•  » » » 7 years ago, # ^ |   +5 Do you know about bit representation of numbers in computer memory? If not, read about it. You will understand everything.
•  » » » 7 years ago, # ^ |   0 say you have an integer array int a[100] and trying to memset it and memset belongs to the string.h header what it does is byte by byte it will fill in your initializing character that means memset( a , 1 , sizeof a ) what will happen here is that it will take a[0] and it has four bytes right (assuming a standard gcc compiler) takes the first byte and fill it with 1 , second byte and again fill it with 1, and 3rd and 4th, so finally what you will be having is 00000001000000010000000100000001 for a[0] this is bit representation of (2^0+2^8+2^16+2^24) understood ? so -1 all bits will be one and so a[0] has -1, some big value greater than 2^8 it will mod by 2^8 and again do the same what I told you above. understood ? though it has been 4 months still it may be useful to some other guy.
•  » » » » 7 years ago, # ^ | ← Rev. 4 →   0 for -1 all bits are 1 but we get -1 if we print a[0] , how its possible ? If this time its mod by 2^8 but why not when use 127
•  » » » » » 7 years ago, # ^ |   0 You get "-1" when printing a[0], because integers are stored in two's complement in C. No matter what power of two you choose, all bits set to one will always yield "-1".
 » 7 years ago, # |   +18 if(re == re) return re; // you can use this for memoization... 
•  » » 7 years ago, # ^ |   +1 very good idea it works because the fact that nan != nan
 » 7 years ago, # | ← Rev. 2 →   0 Can't resist: "I have a dream"
 » 7 years ago, # |   -20 tourist Petr YuukaKazami sdya yeputons cgy4ever I_love_Tanya_Romanova fap fap fap fap fap fap fap vepifanov [user:scott_wu][user:0O0o00OO0Oo0o0Oo] Egor TankEngineer lol fap fap fap fappie !~~~~~~