C++ How to memcpy int to char[] - c++

I want to convert time from int to string in a fast way.The sprintf is too slow. I write a test code like this:
int year = 2020;
int month = 8;
int day = 16;
char* log = "test log";
char output[1024];
char timeformat[] = "[0000:00:00] ";
int n = strlen(timeformat);
/*------------------1start---------------*/
for (int i = 0; i < 100000000; ++i) {
for (int j = 0; j < 4; ++j) {
timeformat[4-j] += year%10;
year /= 10;
}
for (int j = 0; j < 2; ++j) {
timeformat[7-j] += month%10;
month /= 10;
}
for (int j = 0; j < 2; ++j) {
timeformat[10-j] += day%10;
day /= 10;
}
memcpy(output, timeformat, n);
memcpy(output+n,log, strlen(log));
}
/*------------------1end-----------------*/
/*------------------2start---------------*/
for (int i = 0; i < 100000000; ++i) {
sprintf(mem,"%d:%d:%d %s",year,month,day,log);
}
/*------------------2end-----------------*/
The first code cost 2~3seconds for average, and the second is much more slower than first one for 13~20s.
But the first code is too ugly. Is there any beautiful way to convert int to char*?

Related

Assigning value to pointer with multithreading generates wrong result

void assign(short *inPtr, int ext[4], int sz[2])
{
for (int j = ext[2]; j < ext[3]; j++)
for (int i = ext[0]; i < ext[1]; i++)
{
inPtr[i + j * sz[1]] = 100;
}
}
int main(int, char **)
{
int size[2] = {16, 16};
short ptr[256];
std::vector<std::thread> pool;
int block = 2;
int row = size[0] / block;
int col = size[1] / block;
for (size_t j = 0; j < row; j++)
for (size_t i = 0; i < col; i++)
{
int ext[4] = {i * block, (i + 1) * block, j * block, (j + 1) * block};
pool.push_back(std::thread(assign, ptr, ext, size));
}
for (size_t i = 0; i < row * col; i++)
{
pool[i].join();
}
for (size_t i = 0; i < 256; i++)
{
std::cout << (double)ptr[i] << ", ";
}
std::cout << std::endl;
}
I'm trying to assign value to pointer ptr by using multithreading.
The code above is supposed to assign 100 to all element of ptr, however, there are many 0 in the output. Why does it happen and how to fix it ?

multiply two negative numbers in c++

when I tried to multiple two negative numbers the value it is zero in c++,
for example -5 * -3
the result is zero,
why?
this is my code
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void Multiply(const int v_arr[], const int m_arr[][3], int signed
o_arr[], int size)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
o_arr[i] = 0;
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
}
//End your code here
}
int main()
{
int n;
cin >> n;
int v_array[n];
int m_array[n][3];
int signed o_array[3];
for (int i = 0; i < n; i++) {
cin >> v_array[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> m_array[i][j];
}
}
//fuction
Multiply(v_array, m_array, o_array, n);
for (int j = 0; j < 3; j++) {
cout << o_array[j] << " ";
}
return 0;
}
how to fix it to get the correct result?
the input is
2
2 -3
2 -3
2 -4
Your issue is here:
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
You access elements at indices 0, 1 and 2 in v_arr, but it only has 2 elements. That's Undefined Behaviour.
Assuming this is matrix*vector multiplication code, it should look like this (untested):
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
Also, your loop based on j is useless. You can remove it:
void Multiply(const int v_arr[], const int m_arr[][3], int signed o_arr[], int size)
{
for(int k = 0; k < 3; k++) { //initialize output array
o_arr[k] = 0;
}
for (int i = 0; i < size; i++) {
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
}

C++: Sorting strings using LSD radix sort crashing

I have written some code that is meant to sort an array of strings using the radix sort, starting with the least significant digit. This function assumes all of the strings are the same length and each character is lowercase.
I am encountering crashes whenever I get to the loop in which I assign values to the temporary array. You can see my function here:
#ifndef RADIX_H
#define RADIX_H
#include <string>
#include <iostream>
using namespace std;
void lsd_string_radix(string array[], int array_size, int max_chars)
{
string *temp = new string[array_size];
for(int i = max_chars - 1; i >= 0; i--)
{
int count[26] = {0};
for(int j = 0; j < array_size; j++)
{
count[static_cast<int>(array[j][i]) - 97]++;
}
for(int j = 1; j <= 26; j++)
{
count[j] += count[j - 1];
}
for(int j = 0; j < array_size; j++)
{
temp[count[static_cast<int>(array[j][i])]++] = array[j]; // crashes here
}
for(int j = 0; j < array_size; j++)
{
array[j] = temp[j];
}
}
}
#endif
I'm guessing I have a failing in logic but I can't figure it out for the life of me.
After the second loop, count[0] should be zero, and the third loop is missing a -97. This example fixes the problem using count of size 27 instead of 26. The first loop in this example uses -96, so count[0] = 0, count[1] = # instances of 'a's, count[2] = # instances of 'b's, ... . count[26] = # instances of 'z's but it's only used in the first loop. It's not needed, but it's simpler to put a count of 'z's there rather than adding an if statement to avoid storing a count at count[26].
#include<iomanip>
#include<iostream>
#include <string>
using namespace std;
void lsd_string_radix(string array[], int array_size, int max_chars)
{
string *temp = new string[array_size];
for(int i = max_chars - 1; i >= 0; i--)
{
int count[27] = {0};
for(int j = 0; j < array_size; j++)
count[static_cast<int>(array[j][i]) - 96]++;
for(int j = 2; j < 26; j++)
count[j] += count[j - 1];
for(int j = 0; j < array_size; j++)
temp[count[static_cast<int>(array[j][i]) - 97]++] = array[j];
for(int j = 0; j < array_size; j++)
array[j] = temp[j];
}
}
int main()
{
string a[6] = {"mnop", "ijkl", "efgh", "uvwx", "qrst", "abcd"};
lsd_string_radix(a, 6, 4);
for(size_t i = 0; i < 6; i++)
cout << a[i] << endl;
return 0;
}
If the size of count[] is to be 26, the first loop needs to be modified:
for(int j = 0; j < array_size; j++){
if(array[j][i] == 'z')continue;
count[static_cast<int>(array[j][i]) - 96]++;
}
or the first two loops are modified:
for(int j = 0; j < array_size; j++)
count[static_cast<int>(array[j][i]) - 97]++;
int m = 0;
int n;
for(int j = 0; j < 26; j++){
n = count[j];
count[j] = m;
m += n;
}

Putting String into a 2D Matrix in Objective C++

So I'm using Objective C++ and I want to put a string into a 4 by X (X = length of string/4) int array by using the ASCII code. The first quarter of the string (which is formatted to fit completely into a 4 by X array) is supposed to go in [0][col], the second quarter into [1][col], the third quarter into [2][col] and the fourth quarter into [3][col]. So I tried the following with 4 for loops, but it doesnt work at all, and I just can't seem to get it to work somehow. Any suggestions would be greatly appreciated.
textMatrix is the matrix in which I want to put the NSString/ASCII number, and inputFinal is the NSString itself. Length * (1/4) or whatever is also always going to be an integer.
for(int i = 0; i < length*(1/4); i++)
{
textMatrix[0][i] = (int)[inputFinal characterAtIndex: i];
}
for(int j = length*(1/4); j < length*(2/4); j++)
{
textMatrix[1][j] = (int)[inputFinal characterAtIndex: j];
}
for(int k = length*(2/4); k < length*(3/4); k++)
{
textMatrix[2][k] = (int)[inputFinal characterAtIndex: k];
}
for(int l = length*(3/4); l < length; l++)
{
textMatrix[3][l] = (int)[inputFinal characterAtIndex: l];
}
You can rewrite your 4 loops in 1 loop:
for(int i = 0; i < length; i++)
{
textMatrix[i/4][i%4] = (int)[inputFinal characterAtIndex:i];
}
I don't think I understand what you're trying to do..
Given a string: "Here";
do you want:
Matrix[0][0] = 'H';
Matrix[1][1] = 'e';
Matrix[2][2] = 'r';
Matrix[3][3] = 'e';
If so then this works:
#import <objc/objc.h>
#import <objc/Object.h>
#import <Foundation/Foundation.h>
#implementation TestObj
int main()
{
NSString* str = #"Here";
int matrix[4][4] = {0};
for (int i = 0, j = 0; j < 4; ++j)
{
matrix[i][i++] = (int) [str characterAtIndex: j];
}
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
printf("%c", (char)matrix[i][j]);
}
}
return 0;
}
#end
The above prints Here.
actually a double loop like so ended up working best for me:
int index = 0;
for(int row = 0; row < 4; row++)
{
for(int col = 0; col < length/4; col++)
{
textMatrix[row][col] = (int)[inputFinal characterAtIndex:index];
index++;
}
}

Complex malloc usage

My program crashes always on the same allocation when I try to allocate memory for the array_x.
In the code bellow if I uncommend the line size = 10; It works like charm. If I commend the line it crashes. Why?
struct dt
{
int *array_x;
int b;
void start(int size)
{
//size = 10;
array_x = (int*)malloc(sizeof(int)*size);
}
};
void c_f()
{
dt *d = (dt*)malloc(sizeof(dt)*100);
for(int i = 0; i < 100; i++)
{
d[i].start(i);
for(int j = 0; j < 10; j++)
d[i].array_x[j] = 1;
}
d = (dt*)realloc(d, sizeof(dt)*200);
for(int i = 100; i < 200; i++)
{
d[i].start(i);//here it crashes
for(int j = 0; j < 10; j++)
d[i].array_x[j] = 1;
}
for(int i = 0; i < 200; i++)
for(int j = 0; j < 10; j++)
cout << d[i].array_x[j];
}
When you are using dt::start inside the for loop, you are passing 0 to the function, leading:
(int*)malloc(sizeof(int)*size);
to be:
(int*)malloc(0);
Of course, accessing the returned pointer is undefined behavior.