Related
library(stringr)
I want to substract the secong number on the string per_ocu in a bigger table. I did use a nested ifelse with substr() but the result is the last table below. It is only substracting the last line and it worked previously with the perocu_min version. I have also tried to trim the string. What is wrong with my code?
t_perocu
A tibble: 7 × 3
Groups: per_ocu [7]
per_ocu char_perocu n
1 0 a 5 personas 14 1471172
2 101 a 250 personas 18 3531
3 11 a 30 personas 16 37998
4 251 y m�s personas 20 3014
5 31 a 50 personas 16 5178
6 51 a 100 personas 17 3468
7 6 a 10 personas 15 85071
This is my code. (I want to make sure there is no bug in my R Studio Version.)
denue_1$perocu_max <- ifelse(denue_1$char_perocu == 14, substr(denue_1$per_ocu, 5, 1),
ifelse(denue_1$char_perocu == 15, substr(denue_1$per_ocu, 5, 2),
ifelse(denue_1$char_perocu == 16, substr(denue_1$per_ocu, 6, 2),
ifelse(denue_1$char_perocu == 17, substr(denue_1$per_ocu, 6, 3),
ifelse(denue_1$char_perocu == 18, substr(denue_1$per_ocu, 7, 3),
ifelse(denue_1$char_perocu == 20, substr(denue_1$per_ocu, 1, 3), 0))))))
table(denue_1$perocu_max)
table(denue_1$perocu_max)
This is what I got:
251
1606418 3014
This is what I was expecting
View(tperocu_max)
tperocu_max
Var1 Freq emp
1 5 1471172 7355860
2 10 85071 850710
3 30 37998 1139940
4 50 5178 258900
5 100 3468 346800
6 250 3531 882750
7 251 3014 756514
I am getting a core dump, the pstack core shows as below:
fed4ebd4 _lwp_kill (b, fe984948, 0, 1, fffffffc, 0) + 8
fdbd6c6c skgesigOSCrash (ffbfb5d4, ffbfb47c, fec31460, feb7536c, bc0f4, bc000) + 4c
fe1253a0 kpeDbgSignalHandler (ffbfb5d4, b13d630, 9e800, b, ffbfb9dd, 6060000) + 2f0
fdbd71ac skgesig_sigactionHandler (b, ffbfc260, febef8f8, 0, fec314a0, ffbfb5c0) + f0
fed4b00c __sighndlr (b, ffbfc260, ffbfbfa8, fdbd70bc, 0, 1) + c
fed3f6bc call_user_handler (b, 0, 8, 0, fc7f2a00, ffbfbfa8) + 3b8
fed3f8a4 sigacthandler (b, ffbfc260, ffbfbfa8, 29a8d0, 0, 0) + 60
--- called from signal handler with signal 11 (SIGSEGV) ---
fecd8028 _malloc_unlocked (308, bad8f38, bad8f38, bad8f38, fffffffc, 0) + 22c
fecd7de0 malloc (304, 1, ea654, 297f1c, fedc23f0, fedcc5e0) + 4c
002f0ee8 malloc (304, ffbfddd4, ba56c40, 1c, 3a, ba56d7d) + 54
001c4c78 allocate__t23__malloc_alloc_template1i0Ui (304, 0, 0, ffbfd99f, 0, 80808080)
+ c
001c4cfc allocate__t24__default_alloc_template2b0i0Ui (304, 304, ffbfd99f, 1, b, 0) +
18
0029a80c allocate__t12simple_alloc2ZPt15_Hashtable_node1Zt4pair2ZCt12basic_string3ZcZt
18string_char_traits1ZcZt24__default_alloc_template2b0i0Zt6vector2ZiZt9allocator1ZiZt24
__default_alloc_template2b0i0Ui (c1, 0, 0, 0, 0, ffffffff) + 20
00299d38 _M_allocate__t18_Vector_alloc_base3ZPt15_Hashtable_node1Zt4pair2ZCt12basic_st
ring3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0Zt6vector2ZiZt9allocat
or1ZiZt9allocator1Zt6vector2ZiZt9allocator1Zib1Ui (ffbfddf4, c1, 0, ffffffff, a, 808080
80) + 10
0029a8d0 _M_allocate_and_copy__H1ZPPt15_Hashtable_node1Zt4pair2ZCt12basic_string3ZcZt1
8string_char_traits1ZcZt24__default_alloc_template2b0i0Zt6vector2ZiZt9allocator1Zi_t6ve
ctor2ZPt15_Hashtable_node1Zt4pair2ZCt12basic_string3ZcZt18string_char_traits1ZcZt (ffbf
ddf4, c1, 0, 0, ffbfe047, 1) + 1c
00299e24 reserve__t6vector2ZPt15_Hashtable_node1Zt4pair2ZCt12basic_string3ZcZt18string
_char_traits1ZcZt24__default_alloc_template2b0i0Zt6vector2ZiZt9allocator1ZiZt9allocator
1Zt6vector2ZiZt9allocator1ZiUi (ffbfddf4, c1, ffbfdb74, 0, ffbfe047, 7ffffff0) + 48
00297f1c _M_copy_from__t9hashtable6Zt4pair2ZCt12basic_string3ZcZt18string_char_traits1
ZcZt24__default_alloc_template2b0i0Zt6vector2ZiZt9allocator1ZiZt12basic_string3ZcZt18st
ring_char_traits1ZcZt24__default_alloc_template2b0i0Z7strhashZt10_Select1st1Zt4pa (ffbf
ddf0, ffbfddd4, c1, ffbfdbe8, 38, ba56602) + 3c
0029ace8 __t9hashtable6Zt4pair2ZCt12basic_string3ZcZt18string_char_traits1ZcZt24__defa
ult_alloc_template2b0i0Zt6vector2ZiZt9allocator1ZiZt12basic_string3ZcZt18string_char_tr
aits1ZcZt24__default_alloc_template2b0i0Z7strhashZt10_Select1st1Zt4pair2ZCt12basi (ffbf
ddf0, ffbfddd4, ba56c40, 1c, 3a, ba56d7d) + 124
002998ac __t8hash_map5Zt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_
template2b0i0Zt6vector2ZiZt9allocator1ZiZ7strhashZ5streqZt9allocator1Zt6vector2ZiZt9all
ocator1ZiRCt8hash_map5Zt12basic_string3ZcZt18string_char_traits1ZcZt24__default_a (ffbf
ddf0, ffbfddd4, ffbfdcef, ffbfdcee, ffbfdce0, 80808080) + 14
00294754 __Q212Notification4._47RCQ212Notification4._47 (ffbfddec, ffbfddd0, 484564, 1
, b, 0) + 1c
0029adf8 __t4pair2ZCt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_tem
plate2b0i0ZQ212Notification9_RecordIdRCt12basic_string3ZcZt18string_char_traits1ZcZt24_
_default_alloc_template2b0i0RCQ212Notification9_RecordId (ffbfdde8, ffbfde88, ffbfddd0,
0, 0, ffffffff) + 2c
00299fcc __vc__t8hash_map5Zt12basic_string3ZcZt18string_char_traits1ZcZt24__default_al
loc_template2b0i0ZQ212Notification9_RecordIdZ7strhashZ5streqZt9allocator1ZQ212Notificat
ion9_RecordIdRCt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_tem (4845
64, ffbfde88, ffbfde80, ffffffff, a, 80808080) + 2c
002948a8 _Add__12NotificationPCcP8RecordId (484560, ba56d60, 0, ffbfe047, ffbfe047, 1)
+ a0
...
The core dump is coming when using hash_map with Solaris 10, g++ 2.95.3 compiled.
What's __Q212Notification4._47RCQ212Notification4._47?
Please suggest if any clue.
I have a piece of code like this:
#include<LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void leftShift(bool toShift[28], int noOfShifts, bool destination[28]) {
for (int i = 0; i < 28; i++) {
destination[(i - noOfShifts + 28) % 28] = toShift[i];
}
}
void divideBinary(bool binary[], size_t sizeOfBinary, bool LB[], bool RB[]) {
size_t half = sizeOfBinary / 2;
// LB - first half
size_t i = 0;
for (; i < half; i++) {
LB[i] = binary[i];
}
// RB - second half
for (; i < half * 2; i++) {
RB[i - half] = binary[i];
}
}
void createSubkeys(bool binaryKey[8 * 8], bool subkeys[16][48]) {
Serial.println("just entered subkeys"); Serial.flush();
int pc_1[56] = {
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
bool keyPermutation[56];
// according to pc_1 create from 64-bit key 56-bit keyPermutation
for (int i = 0; i < 56; i++) {
keyPermutation[i] = binaryKey[pc_1[i] - 1];
}
// C and D will be saved here: [C/D] [index] [28 bools]
bool CD[2][16 + 1][56 / 2];
Serial.println("CD ready"); Serial.flush();
// divide keyPermutation into halves to C0 a D0 - each consists of 28 bits
divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]);
// from C0, D0 and shifts make C1, D1 -> C16, D16
int shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
for (int i = 1; i < 17; i++) {
leftShift(CD[0][i - 1], shifts[i - 1], CD[0][i]);
leftShift(CD[1][i - 1], shifts[i - 1], CD[1][i]);
}
// each subKey out of 16 is made from one out of 16 CD with the use of pc_2
int pc_2[48] = {
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 48; j++) {
// find out which part of CD we should look at - that means C, or D? for C CorD is 0, for D 1
int where = pc_2[j] - 1;
bool CorD = 0;
if (where >= 56 / 2) {
CorD = 1;
where -= 56 / 2; // subtract 28, to start indexing from 0 again in case of D
}
subkeys[i][j] = CD[CorD][i + 1][where];
}
}
// Serial.println("subkeys ready");
}
void setup() {
// put your setup code here, to run once:
Serial.begin( 9600 );
lcd.begin(16, 2);
Serial.println("ready"); Serial.flush();
bool binaryKey[8 * 8];
bool subkeys[16][48];
createSubkeys(binaryKey, subkeys);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print("haf");
}
It is not really important what it does, it is just so you can roughly see its complexity.
Why won't this work on Arduino, even if it were to be much slower? Instead, when I run it, my Arduino UNO really behaves weirdly. In Serial it just repeats a sequence of characters "jready" in a loop. Forever. It never prints anything else and it never reaches the loop() function.
My Arduino and its Serial both work perfectly fine for smaller programs.
EDIT: If I attempt to locate the problem by commenting out blocks of code, it seems to occur here:
// from C0, D0 and shifts make C1, D1 -> C16, D16
int shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
for (int i = 1; i < 17; i++) {
leftShift(CD[0][i - 1], shifts[i - 1], CD[0][i]);
leftShift(CD[1][i - 1], shifts[i - 1], CD[1][i]);
}
But if I make my setup() function more complex, it happens earlier, on this line:
divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]);
You are passing variables to methods without correctly specifying whether they should return that value which you modify inside that method. Look at:
void leftShift(bool toShift[28], int noOfShifts, bool destination[28])
You modify destination in there but destination is boolean array copied to the method but when the method finishes that modified value is not returned to the caller. Change your declarations.
Here:
bool CD[2][16 + 1][56 / 2];
You have a third dimension to the array but never use it. The third dimension has 28 elements but you only ever use the second dimension. If you are trying to do pointer operations then you will have to change leftShift
Also consider the above points I made with divideBinary.
In my multithread C++ program on Solaris 10 using GNU 2.95.3, I am getting a contention issue while one thread is trying to call the string constructor and other thread is calling a stringbuf constructor, as mentioned below:
One thread is calling
__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0PCc
i.e.
basic_string<char, string_char_traits<char>, __default_alloc_template<false, 0>>::basic_string(char const *)
Another thread is calling
__9stringbufRCt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0i
i.e.
stringbuf::stringbuf(basic_string<char, string_char_traits<char>, __default_alloc_template<false, 0> > const &, int)
Eventually both threads lead to allocating space for the string or stringbuf as applicable and results in a contention leading to SIGBUS error.
Any clue?
Do I need to overload malloc or new to be thread-safe or do I need to overload string and stringbuf to ensure thread safety?
I use g++ linker flag as below:
I g++ -g -lclntsh -ldl -lsocket -lrt –lthread
Do I need to add 'g++ -pthread' during compilation? I hope this would add -D_RENENTRANT as well. But Solaris g++ 2.95.3 does not support -pthread
Here's more pstack core output:
core 'core' of 28477:
----------------- lwp# 1 / thread# 1 --------------------
fcb48ac4 __lwp_park (0, 0, fcbb74c4, 1c00, 0, 0) + 14
ff375244 sem_wait (1da56fd8, 1da56c00, 8ea400, 2aae33d0, fcbb49cc, 0) + 20
....
001965f0 _start (0, 0, 0, 0, 0, 0) + 5c
----------------- lwp# 2 / thread# 2 --------------------
fcb4c714 _lwp_kill (a, fed84948, 0, 1, fffffffc, 0) + 8
fdfd6c6c skgesigOSCrash (fc3eea54, fc3ee8fc, ff031460, fef7536c, bc0f4, bc000) + 4c
fe5253a0 kpeDbgSignalHandler (fc3eea54, 2b200cd8, 9e800, a, fc3eee5d, 6060000) + 2f0
fdfd71ac skgesig_sigactionHandler (a, fc3ef6e0, fefef8f8, 0, ff0314a0, fc3eea40) + f0
fcb48b4c __sighndlr (a, fc3ef6e0, fc3ef428, fdfd70bc, 0, 1) + c
fcb3d1f8 call_user_handler (a, 0, 8, 0, fc540200, fc3ef428) + 3b8
fcb3d3cc sigacthandler (a, fc3ef6e0, fc3ef428, 1, fc540200, 0) + 4c
--- called from signal handler with signal 10 (SIGBUS) ---
00608e00 allocate__t24__default_alloc_template2b0i0Ui (20, 20, 9eff80, 1c7be, 2ab11e06, fcbb4f18) +
a4
003cde3c __nw__Q2t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0_3RepU
iUi (10, 10, ff000000, 4, fc540200, 861908) + 14
003cef8c create__Q2t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0_3Re
pUi (2, 2, 2b10d5c8, fcbb5434, fcbb5784, fffc00) + 24
003ee944 replace__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0UiUiP
CcUi (fc3efaf8, 0, ffffffff, 8618a0, 2, 80808080) + 114
006919cc assign__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0PCcUi
(fc3efaf8, 8618a0, 2, 0, fc540200, 7164b4) + 24
00666684 assign__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0PCc (f
c3efaf8, 8618a0, 8ea400, 0, fc5706c0, 0) + 24
005f7868 __t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0PCc (fc3efaf
8, 8618a0, 861800, 0, fc3efa90, a4090) + 28
....
----------------- lwp# 3 / thread# 3 --------------------
fcb41818 mutex_lock_impl (fcbb3910, 0, 2dfc0030, fc3beee0, 6, 80808080) + 4
fcad640c malloc (6, 1, d9fd8, 6919cc, fcbb03a8, fcbba518) + 44
003c878c __builtin_vec_new (6, 3, 37, fc3bee38, ff32a888, 4) + 3c
005e16ec __9stringbufRCt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b0i0
i (fc3bee60, fc3bee58, 3, 0, 2b2ab6e8, 20) + 64
...
----------------- lwp# 4 / thread# 4 --------------------
fcb48ac4 __lwp_park (0, 0, fcbb74c4, 1c00, 0, 0) + 14
ff375244 sem_wait (1da56f48, 1da56c00, 1, 0, fc541200, 0) + 20
....
fcb48a20 _lwp_start (0, 0, 0, 0, 0, 0)
I found this source which works quite well, I just want to ask about this piece of code which I dont get:
//calculate total size of RGBQUAD scanlines (DWORD aligned)
bih.biSizeImage = (((bih.biWidth * 3) + 3) & 0xFFFC) * bih.biHeight ;
I get why there is "*3", but dont get the "+3" and the bitwise AND with FFFC hexa. Could someone explain me why he claculates size of the image this way?
Thanks
If you try that out for various values, you'll see it's actually forcing (width * 3) to round up to the smallest multiple of 4 that will contain it. He's probably doing this to enforce things to be 32-bit aligned.
Using python:
>>> f = lambda x: ((x * 3) + 3) & 0xFFFC
>>> [f(x) for x in range(1, 20)]
[4, 8, 12, 12, 16, 20, 24, 24, 28, 32, 36, 36, 40, 44, 48, 48, 52, 56, 60]
The following shows the difference between just doing the straight multiplication and rounding upwards towards a multiple of 4
>>> [(3*x, f(x)) for x in range(1, 8)]
[(3, 4), (6, 8), (9, 12), (12, 12), (15, 16), (18, 20), (21, 24)]
I'm surprised the code doesn't actually document this fact. Bit twiddling is a wonderful thing, but it can seem very arbitrary.