Related
I'm new to C++ and I wanted to compress a vector via ZSTD compression library. I used ZSTD simple API ZSTD_compress and ZSTD_decompress in the same way as the example. But I found a wired issue that when I compressed and decompressed a vector, the decompressed vector was not the same as the original vector. I'm not sure which part of my operation went wrong.
I looked at ZSTD's GitHub homepage and didn't find an answer. Please help or try to give some ideas how to solve it.
Example C code: https://github.com/facebook/zstd/blob/dev/examples/simple_compression.c
//Initialize a vector
vector<int> NumToCompress ;
NumToCompress.resize(10000);
for(int i = 0; i < 10000; i++)
{
NumToCompress[i] = rand()% 255;
}
//compress
int* com_ptr = NULL;
size_t NumSize = NumToCompress.size();
size_t Boundsize = ZSTD_compressBound(NumSize);
com_ptr =(int*) malloc(Boundsize);
size_t ComSize;
ComSize = ZSTD_compress(com_ptr,Boundsize,NumToCompress.data(),NumToCompress.size(),ZSTD_fast);
//decompress
int* decom_ptr = NULL;
unsigned long long decom_Boundsize;
decom_Boundsize = ZSTD_getFrameContentSize(com_ptr, ComSize);
decom_ptr = (int*)malloc(decom_Boundsize);
size_t DecomSize;
DecomSize = ZSTD_decompress(decom_ptr, decom_Boundsize, com_ptr, ComSize);
vector<int> NumAfterDecompress(decom_ptr,decom_ptr+DecomSize);
//check if two vectors are same
if(NumToCompress == NumAfterDecompress)
{
cout << "Two vectors are same" << endl;
}else
{cout << "Two vectors are insame" << endl;}
free(com_ptr);
free(decom_ptr);
case 1: If zstd can compress std::vector directly?
case 2: How to properly compress vectors with zstd if zstd can compress std::vector directly ?
Two vectors are insame
Original vector:
163 151 162 85 83 190 241 252 249 121 107 82 20 19 233 226 45 81 142 31 86 8 87 39 167 5 212 208 82 130 119 117 27 153 74 237 88 61 106 82 54 213 36 74 104 142 173 149 95 60 53 181 196 140 221 108 17 50 61 226 180 180 89 207 206 35 61 39 223 167 249 150 252 30 224 102 44 14 123 140 202 48 66 143 188 159 123 206 209 184 177 135 236 138 214 187 46 21 99 14
Decompressed vector:
163 151 162 85 83 190 241 252 249 121 107 82 20 19 233 226 45 81 142 31 86 8 87 39 167 0 417 0551929248 21916 551935408 21916 551933352 21916 551939512 21916 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Seems likely that your API expects the size to be in bytes but you give the size as the number of elements. So you need to multiply the number of elements by the size of each element. Like this
ComSize = ZSTD_compress(com_ptr, Boundsize, NumToCompress.data(),
NumToCompress.size()*sizeof(int), ZSTD_fast);
and similarly when you decompress you need to divide by the element size
DecomSize = ZSTD_decompress(decom_ptr, decom_Boundsize, com_ptr, ComSize);
vector<int> NumAfterDecompress(decom_ptr, decom_ptr+DecomSize/sizeof(int));
the problem is in the int main()
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
const int R = 15;
const int C = 20;
void clearArray(int arr[R][C])
{
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
arr[i][j] = 0;
}
}
}
void makeSpiral(int arr[R][C],int r , int c)
{
int i, a = 0, b = 0;
int value = 1;
while (a < r && b < c)
{
for (i = a; i < r; ++i)
{
arr[i][b] = value++;
}
b++;
for (i = b; i < c; ++i)
{
arr[r - 1][i] = value++;
}
r--;
if (a < r)
{
for (i = r - 1; i >= a; --i)
{
arr[i][c-1] = value++;
}
c--;
}
if (b < c)
{
for (i = c - 1; i >= b; --i)
{
arr[a][i] = value++;
}
a++;
}
}
}
void printSpiral(int arr[R][C],int r , int c, char str[])
{
str[30];
ofstream fout;
fout.open(str, ios::out | ios::app);
if (!fout)
{
cout << "Error: file could not be opened" << endl;
exit(1);
}
fout << endl;
fout << "---------------------------------------------";
for (int i = 0; i < r; i++)
{
fout << endl;
for (int j = 0; j < c; j++)
{
fout <<setw(5)<< arr[i][j] << " ";
}
}
fout.close();
}
int main(int argc, char *argv[])
{
char str[30];
ofstream fout;
int r,c;
int i,j,t;
int arr[R][C];
if (argc < 2)
{
cout << "Error missing file name!" << endl;
return -1;
}
strcpy(str, argv[1]);
int matrix [R][C] = {{1,1},{2,2},{3,3},{4,4},{5,5},{4,7},{7,4},{15,20}};
for (i = 0 ; i < 8 ; i++)
{
clearArray(matrix);
makeSpiral(matrix,R,C);
printSpiral (matrix,R,C,str);
}
return 0;
}
I'm a new C++ programmer please go easy on me. So i have this piece of code, the professor requires me to store corner sizes of a spiral matrix into a table, then use a loop to call out my functions so i don't have to call my functions repeatedly like this:
clearArray(matrix);
makeSpiral(matrix,1,1);
printSpiral (matrix,1,1,str);
clearArray(matrix);
makeSpiral(matrix,2,2);
printSpiral (matrix,2,2,str);
clearArray(matrix);
makeSpiral(matrix,3,3);
printSpiral (matrix,3,3,str);
clearArray(matrix);
makeSpiral(matrix,4,4);
printSpiral (matrix,4,4,str);
I want to print a counter-clockwise matrix 8 times which with corner 1 by 1 ; 2 by 2; 3 by 3; 4 by 4; 5 by 5; 4 by 7; 7 by 4; and 15 by 20.
My code doesn't give me the correct output, can you tell me where I did wrong, thank you!
The output that i supposed to have
---------------------------------
1
---------------------------------
1 3
---------------------------------
3
2
---------------------------------
1 4
2 3
---------------------------------
1 8 7
2 9 6
3 4 5
---------------------------------
1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7
---------------------------------
1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
---------------------------------
1 18 17 16 15 14 13
2 19 28 27 26 25 12
3 20 21 22 23 24 11
4 5 6 7 8 9 10
---------------------------------
1 18 17 16
2 19 28 15
3 20 27 14
4 21 26 13
5 22 25 12
6 23 24 11
7 8 9 10
---------------------------------
1 20 19 18 17 16 15 14
2 21 32 31 30 29 28 13
3 22 23 24 25 26 27 12
4 5 6 7 8 9 10 11
---------------------------------
1 16 15 14
2 17 24 13
3 18 23 12
4 19 22 11
5 20 21 10
6 7 8 9
What my code output looks like:
---------------------------------------------------------
1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 47
3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 107 46
4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204 159 106 45
5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203 158 105 44
6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202 157 104 43
7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201 156 103 42
8 73 130 179 220 253 278 295 296 297 298 299 300 287 266 237 200 155 102 41
9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199 154 101 40
10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198 153 100 39
11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197 152 99 38
12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196 151 98 37
13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 97 36
14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
------------------------------------------------------------
1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 47
3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 107 46
4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204 159 106 45
5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203 158 105 44
6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202 157 104 43
7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201 156 103 42
8 73 130 179 220 253 278 295 296 297 298 299 300 287 266 237 200 155 102 41
9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199 154 101 40
10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198 153 100 39
11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197 152 99 38
12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196 151 98 37
13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 97 36
14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
---------------------------------------------------------
1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 47
3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 107 46
4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204 159 106 45
5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203 158 105 44
6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202 157 104 43
7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201 156 103 42
8 73 130 179 220 253 278 295 296 297 298 299 300 287 266 237 200 155 102 41
9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199 154 101 40
10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198 153 100 39
11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197 152 99 38
12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196 151 98 37
13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 97 36
14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
--------------------------------------------------------
......
It prints the 15x20 corner size 8 times.
In your main function, arguments to makeSpiral are fixed R and C. This means makeSpiral always tries to fill a 15 rows, 20 columns spiral matrix to your matrix:
makeSpiral(matrix,R,C);
printSpiral(matrix,R,C,str);
You may want this instead:
int matrix[R][C]; // no need of initialization
int matrixSize[8][2] = {{1,1},{2,2},{3,3},{4,4},{5,5},{4,7},{7,4},{15,20}};
for (i = 0 ; i < 8 ; i++) {
clearArray(matrix, matrixSize[i][0], matrixSize[i][1]);
makeSpiral(matrix, matrixSize[i][0], matrixSize[i][1]);
printSpiral (matrix, matrixSize[i][0], matrixSize[i][1], str);
}
Here you use an assist matrixSize to store expected matrix sizes. Within the for loop makeSpiral will fill matrixSize[i][0] rows, matrixSize[i][1] columns only, so does printSpiral.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My main objective is to populate an array of indices. These indices will be retrieved from multiple Wall objects, each of which contains its own array of 6 indices.
These Wall objects are stored in two arrays, one for rows and one for columns.
Here is the Wall class:
#ifndef WALL_H
#define WALL_H
#include "Angel.h"
#include <vector>
using namespace std;
class Wall
{
public:
Wall();
Wall(int index, int orientation);
~Wall();
Wall(const Wall& other);
Wall& operator=(const Wall& other);
int * getIndices();
int indices[6];
void testWall();
protected:
private:
void makeRow(int index);
void makeColumn(int index);
};
#endif // WALL_H
#include "Wall.h"
Wall::Wall()
{
//ctor
}
//This function checks orientation and call the appropriate method
Wall::Wall(int index, int orientation)
{
if(orientation == 0)
{
makeRow(index);
}
else
{
makeColumn(index);
}
}
//this function prints the contents of the array of indices
void Wall::testWall()
{
for (int i = 0; i < 6; ++i)
cout << indices[i] << " ";
cout << "\n";
}
//This function returns the array
int * Wall::getIndices()
{
return indices;
}
//This function takes an index as an argument and populates the array of indices
//for a row wall based on that index.
void Wall::makeRow(int index)
{
int indexAbove = index + 121;
indices[0] = (index);
indices[1] = (indexAbove);
indices[2] = (indexAbove - 1);
indices[3] = (indexAbove - 1);
indices[4] = (index -1);
indices[5] = (index);
testWall();
}
//This function takes an index as an argument and populates the array of indices
//for a column wall based on that index. It then tests the wall
void Wall::makeColumn(int index)
{
int indexAbove = index + 121;
indices[0] = (index);
indices[1] = (index - 11);
indices[2] = (indexAbove - 11);
indices[3] = (indexAbove - 11);
indices[4] = (indexAbove);
indices[5] = (index);
testWall();
}
Wall::Wall(const Wall& other)
{
//copy ctor
}
Wall::~Wall()
{
}
Wall& Wall::operator=(const Wall& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
When I populate the arrays of Walls I test the values to verify correct input and print it out.
//This function populates 2 arrays of Wall objects. One for rows and one for columns.
void makeGrid()
{
//Access to lower level vertices
for(int i = 0; i < 11; ++i)
for( int j = 0; j < 11; ++j){
int index = (i * 11) + j;
printf("for index %d, these are the related indices\n", index );
if(j != 0){
rows[index] = Wall(index, 0);
}
if(i != 0){
columns[index] = Wall(index, 1) ;
}
}
}
However when I later retrieve them, the values all change to zero, as if my arrays are all uninitialized or creating a new object. I have no idea where i'm going wrong!
//This function tests the indices array contained within each wall of the rows array.
void getIndices()
{
int index = 0;
int * verts = NULL;
for (int i = 0; i < numRows; ++i)
rows[i].testWall();
/*
verts = rows[1].getIndices();
for(int j = 0; j < 6; j++){
indices[index] = rows[i].indices[j];
//cout << indices[index] << " ";
index++;
}
}
for (int i = 0; i < numColumns; i++){
verts = columns[i].getIndices();
for(int j = 0; j < 6; j++){
indices[index] = verts[j];
cout << verts[j] << " ";
index++;
}
}
*/
}
Edit:
These are my declarations of the arrays i'm working with
const int numVertices = 242, numColors = 242, numRows = 110, numColumns = 110;
const int numIndices = (numRows + numColumns) * 6;
vec4 vertices[numVertices];
vec4 colors[numColors];
Wall rows[numRows];
Wall columns[numColumns];
GLint indices[numIndices];
57 178 177 177 56 57
57 46 167 167 178 57
for index 58, these are the related indices
58 179 178 178 57 58
58 47 168 168 179 58
for index 59, these are the related indices
59 180 179 179 58 59
59 48 169 169 180 59
for index 60, these are the related indices
60 181 180 180 59 60
60 49 170 170 181 60
for index 61, these are the related indices
61 182 181 181 60 61
61 50 171 171 182 61
for index 62, these are the related indices
62 183 182 182 61 62
62 51 172 172 183 62
for index 63, these are the related indices
63 184 183 183 62 63
63 52 173 173 184 63
for index 64, these are the related indices
64 185 184 184 63 64
64 53 174 174 185 64
for index 65, these are the related indices
65 186 185 185 64 65
65 54 175 175 186 65
for index 66, these are the related indices
66 55 176 176 187 66
for index 67, these are the related indices
67 188 187 187 66 67
67 56 177 177 188 67
for index 68, these are the related indices
68 189 188 188 67 68
68 57 178 178 189 68
for index 69, these are the related indices
69 190 189 189 68 69
69 58 179 179 190 69
for index 70, these are the related indices
70 191 190 190 69 70
70 59 180 180 191 70
for index 71, these are the related indices
71 192 191 191 70 71
71 60 181 181 192 71
for index 72, these are the related indices
72 193 192 192 71 72
72 61 182 182 193 72
for index 73, these are the related indices
73 194 193 193 72 73
73 62 183 183 194 73
for index 74, these are the related indices
74 195 194 194 73 74
74 63 184 184 195 74
for index 75, these are the related indices
75 196 195 195 74 75
75 64 185 185 196 75
for index 76, these are the related indices
76 197 196 196 75 76
76 65 186 186 197 76
for index 77, these are the related indices
77 66 187 187 198 77
for index 78, these are the related indices
78 199 198 198 77 78
78 67 188 188 199 78
for index 79, these are the related indices
79 200 199 199 78 79
79 68 189 189 200 79
for index 80, these are the related indices
80 201 200 200 79 80
80 69 190 190 201 80
for index 81, these are the related indices
81 202 201 201 80 81
81 70 191 191 202 81
for index 82, these are the related indices
82 203 202 202 81 82
82 71 192 192 203 82
for index 83, these are the related indices
83 204 203 203 82 83
83 72 193 193 204 83
for index 84, these are the related indices
84 205 204 204 83 84
84 73 194 194 205 84
for index 85, these are the related indices
85 206 205 205 84 85
85 74 195 195 206 85
for index 86, these are the related indices
86 207 206 206 85 86
86 75 196 196 207 86
for index 87, these are the related indices
87 208 207 207 86 87
87 76 197 197 208 87
for index 88, these are the related indices
88 77 198 198 209 88
for index 89, these are the related indices
89 210 209 209 88 89
89 78 199 199 210 89
for index 90, these are the related indices
90 211 210 210 89 90
90 79 200 200 211 90
for index 91, these are the related indices
91 212 211 211 90 91
91 80 201 201 212 91
for index 92, these are the related indices
92 213 212 212 91 92
92 81 202 202 213 92
for index 93, these are the related indices
93 214 213 213 92 93
93 82 203 203 214 93
for index 94, these are the related indices
94 215 214 214 93 94
94 83 204 204 215 94
for index 95, these are the related indices
95 216 215 215 94 95
95 84 205 205 216 95
for index 96, these are the related indices
96 217 216 216 95 96
96 85 206 206 217 96
for index 97, these are the related indices
97 218 217 217 96 97
97 86 207 207 218 97
for index 98, these are the related indices
98 219 218 218 97 98
98 87 208 208 219 98
for index 99, these are the related indices
99 88 209 209 220 99
for index 100, these are the related indices
100 221 220 220 99 100
100 89 210 210 221 100
for index 101, these are the related indices
101 222 221 221 100 101
101 90 211 211 222 101
for index 102, these are the related indices
102 223 222 222 101 102
102 91 212 212 223 102
for index 103, these are the related indices
103 224 223 223 102 103
103 92 213 213 224 103
for index 104, these are the related indices
104 225 224 224 103 104
104 93 214 214 225 104
for index 105, these are the related indices
105 226 225 225 104 105
105 94 215 215 226 105
for index 106, these are the related indices
106 227 226 226 105 106
106 95 216 216 227 106
for index 107, these are the related indices
107 228 227 227 106 107
107 96 217 217 228 107
for index 108, these are the related indices
108 229 228 228 107 108
108 97 218 218 229 108
for index 109, these are the related indices
109 230 229 229 108 109
109 98 219 219 230 109
for index 110, these are the related indices
110 99 220 220 231 110
for index 111, these are the related indices
111 232 231 231 110 111
111 100 221 221 232 111
for index 112, these are the related indices
112 233 232 232 111 112
112 101 222 222 233 112
for index 113, these are the related indices
113 234 233 233 112 113
113 102 223 223 234 113
for index 114, these are the related indices
114 235 234 234 113 114
114 103 224 224 235 114
for index 115, these are the related indices
115 236 235 235 114 115
115 104 225 225 236 115
for index 116, these are the related indices
116 237 236 236 115 116
116 105 226 226 237 116
for index 117, these are the related indices
117 238 237 237 116 117
117 106 227 227 238 117
for index 118, these are the related indices
118 239 238 238 117 118
118 107 228 228 239 118
for index 119, these are the related indices
119 240 239 239 118 119
119 108 229 229 240 119
for index 120, these are the related indices
120 241 240 240 119 120
120 109 230 230 241 120
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Process returned 0 (0x0) execution time : 2.445 s
Press any key to continue.
Please help me! Thanks!
If your copy constructor has nothing more than:
Wall::Wall(const Wall& other)
{
//copy ctor
}
that's the source of the problem. Either let the compiler generate it for you or implement it properly.
Wall::Wall(const Wall& other)
{
for ( int i = 0; i < 6; ++i )
{
indices[i] = other.indices[i];
}
}
The assignment operator suffers from the same problem (Thanks #πάνταῥεῖ).
Three threads are created that instantiate the same class.
Each thread has a different delay.
But after some time only 1 thread executes. ???
GenNum1 = new CGenerateNumbers();
GenNum1->m_threadNumber = 1;
GenNum1->m_usec = 10000;
connect(GenNum1, SIGNAL(OnIndexReady()), this, SLOT(hGenNumOnIndexReady1()));
QThread *tGenNum1 = new QThread;
GenNum1->moveToThread(tGenNum1);
GenNum1->connect(tGenNum1, SIGNAL(started()), GenNum1, SLOT(Started()), Qt::DirectConnection);
tGenNum1->start();
GenNum2 = new CGenerateNumbers();
GenNum2->m_threadNumber = 2;
GenNum2->m_usec = 15000;
connect(GenNum2, SIGNAL(OnIndexReady()), this, SLOT(hGenNumOnIndexReady2()));
QThread *tGenNum2 = new QThread;
GenNum2->moveToThread(tGenNum2);
GenNum2->connect(tGenNum2, SIGNAL(started()), GenNum2, SLOT(Started()), Qt::DirectConnection);
tGenNum2->start();
GenNum3 = new CGenerateNumbers();
GenNum3->m_threadNumber = 3;
GenNum3->m_usec = 20000;
connect(GenNum3, SIGNAL(OnIndexReady()), this, SLOT(hGenNumOnIndexReady3()));
QThread *tGenNum3 = new QThread;
GenNum3->moveToThread(tGenNum3);
GenNum3->connect(tGenNum3, SIGNAL(started()), GenNum3, SLOT(Started()), Qt::DirectConnection);
tGenNum3->start();
//this is what each thread calls to:
void CGenerateNumbers::Started()
{
int i = 0;
int c = 0;
char v;
while(1){
usleep(m_usec);
v = (i % 26) + 65;
for(int j = 0; j < 11; j++){
mem->m_cmMessageWrite.cMessage[j] = v;
}
mem->m_cmMessageWrite.cMessage[0] = m_threadNumber + 48;
mem->m_cmMessageWrite.cMessage[1] = 32;
mem->Write();
m_i = i;
emit OnIndexReady();
i++;
}
}
index index thread message
0 0 1 AAAAAAAAA
1 1 2 AAAAAAAAA
2 2 3 AAAAAAAAA
3 3 1 BBBBBBBBB
4 4 1 CCCCCCCCC
5 5 2 BBBBBBBBB
6 6 3 BBBBBBBBB
7 7 1 DDDDDDDDD
8 8 2 CCCCCCCCC
9 9 1 EEEEEEEEE
10 10 3 CCCCCCCCC
11 11 1 FFFFFFFFF
12 12 2 DDDDDDDDD
13 13 1 GGGGGGGGG
14 14 2 EEEEEEEEE
15 15 3 DDDDDDDDD
16 16 1 HHHHHHHHH
17 17 1 IIIIIIIII
18 18 2 FFFFFFFFF
19 19 3 EEEEEEEEE
20 20 1 JJJJJJJJJ
21 21 2 GGGGGGGGG
22 22 1 KKKKKKKKK
23 23 3 FFFFFFFFF
24 24 2 HHHHHHHHH
25 25 1 LLLLLLLLL
26 26 2 IIIIIIIII
27 27 3 GGGGGGGGG
28 28 1 MMMMMMMMM
29 29 1 NNNNNNNNN
30 30 2 JJJJJJJJJ
31 31 3 HHHHHHHHH
32 32 1 OOOOOOOOO
33 33 2 KKKKKKKKK
34 34 1 PPPPPPPPP
35 35 3 IIIIIIIII
36 36 1 QQQQQQQQQ
37 37 2 LLLLLLLLL
38 38 1 RRRRRRRRR
39 39 2 MMMMMMMMM
40 40 1 SSSSSSSSS
41 41 2 NNNNNNNNN
42 42 1 TTTTTTTTT
43 43 1 UUUUUUUUU
44 44 1 VVVVVVVVV
45 45 1 WWWWWWWWW
46 46 2 OOOOOOOOO
47 47 1 XXXXXXXXX
48 48 2 PPPPPPPPP
49 49 1 YYYYYYYYY
50 50 2 QQQQQQQQQ
51 51 1 ZZZZZZZZZ
52 52 1 AAAAAAAAA
53 53 2 RRRRRRRRR
54 54 1 BBBBBBBBB
55 55 2 SSSSSSSSS
56 56 1 CCCCCCCCC
57 57 1 DDDDDDDDD
58 58 2 TTTTTTTTT
59 59 1 EEEEEEEEE
60 60 2 UUUUUUUUU
61 61 1 FFFFFFFFF
62 62 1 GGGGGGGGG
63 63 2 VVVVVVVVV
64 64 1 HHHHHHHHH
65 65 2 WWWWWWWWW
66 66 1 IIIIIIIII
67 67 1 JJJJJJJJJ
68 68 2 XXXXXXXXX
69 69 1 KKKKKKKKK
70 70 2 YYYYYYYYY
71 71 1 LLLLLLLLL
72 72 1 MMMMMMMMM
73 73 2 ZZZZZZZZZ
74 74 1 NNNNNNNNN
75 75 2 AAAAAAAAA
76 76 1 OOOOOOOOO
77 77 1 PPPPPPPPP
78 78 2 BBBBBBBBB
79 79 1 QQQQQQQQQ
80 80 2 CCCCCCCCC
81 81 1 RRRRRRRRR
82 82 1 SSSSSSSSS
83 83 2 DDDDDDDDD
84 84 1 TTTTTTTTT
85 85 2 EEEEEEEEE
86 86 1 UUUUUUUUU
87 87 1 VVVVVVVVV
88 88 2 FFFFFFFFF
89 89 1 WWWWWWWWW
90 90 2 GGGGGGGGG
91 91 1 XXXXXXXXX
92 92 1 YYYYYYYYY
93 93 2 HHHHHHHHH
94 94 1 ZZZZZZZZZ
95 95 2 IIIIIIIII
96 96 1 AAAAAAAAA
97 97 1 BBBBBBBBB
98 98 2 JJJJJJJJJ
99 99 1 CCCCCCCCC
100 100 2 KKKKKKKKK
101 101 1 DDDDDDDDD
102 102 2 LLLLLLLLL
103 103 1 EEEEEEEEE
104 104 2 MMMMMMMMM
105 105 1 FFFFFFFFF
106 106 1 GGGGGGGGG
107 107 2 NNNNNNNNN
108 108 1 HHHHHHHHH
109 109 2 OOOOOOOOO
110 110 1 IIIIIIIII
111 111 1 JJJJJJJJJ
112 112 2 PPPPPPPPP
113 113 1 KKKKKKKKK
114 114 2 QQQQQQQQQ
115 115 1 LLLLLLLLL
116 116 1 MMMMMMMMM
117 117 2 RRRRRRRRR
118 118 1 NNNNNNNNN
119 119 2 SSSSSSSSS
120 120 1 OOOOOOOOO
121 121 1 PPPPPPPPP
122 122 2 TTTTTTTTT
123 123 1 QQQQQQQQQ
124 124 2 UUUUUUUUU
125 125 1 RRRRRRRRR
126 126 2 VVVVVVVVV
127 127 1 SSSSSSSSS
128 128 1 TTTTTTTTT
129 129 2 WWWWWWWWW
130 130 1 UUUUUUUUU
131 131 2 XXXXXXXXX
132 132 1 VVVVVVVVV
133 133 2 YYYYYYYYY
134 134 1 WWWWWWWWW
135 135 1 XXXXXXXXX
136 136 2 ZZZZZZZZZ
137 137 1 YYYYYYYYY
138 138 2 AAAAAAAAA
139 139 1 ZZZZZZZZZ
140 140 1 AAAAAAAAA
141 141 2 BBBBBBBBB
142 142 1 BBBBBBBBB
143 143 2 CCCCCCCCC
144 144 1 CCCCCCCCC
145 145 1 DDDDDDDDD
146 146 2 DDDDDDDDD
147 147 1 EEEEEEEEE
148 148 2 EEEEEEEEE
149 149 1 FFFFFFFFF
150 150 1 GGGGGGGGG
151 151 2 FFFFFFFFF
152 152 1 HHHHHHHHH
153 153 2 GGGGGGGGG
154 154 1 IIIIIIIII
155 155 2 HHHHHHHHH
156 156 1 JJJJJJJJJ
157 157 1 KKKKKKKKK
158 158 2 IIIIIIIII
159 159 1 LLLLLLLLL
160 160 2 JJJJJJJJJ
161 161 1 MMMMMMMMM
162 162 1 NNNNNNNNN
163 163 2 KKKKKKKKK
164 164 1 OOOOOOOOO
165 165 2 LLLLLLLLL
166 166 1 PPPPPPPPP
167 167 1 QQQQQQQQQ
168 168 2 MMMMMMMMM
169 169 1 RRRRRRRRR
170 170 2 NNNNNNNNN
171 171 1 SSSSSSSSS
172 172 1 TTTTTTTTT
173 173 2 OOOOOOOOO
174 174 1 UUUUUUUUU
175 175 2 PPPPPPPPP
176 176 1 VVVVVVVVV
177 177 2 QQQQQQQQQ
178 178 1 WWWWWWWWW
179 179 1 XXXXXXXXX
180 180 2 RRRRRRRRR
181 181 1 YYYYYYYYY
182 182 2 SSSSSSSSS
183 183 1 ZZZZZZZZZ
184 184 1 AAAAAAAAA
185 185 2 TTTTTTTTT
186 186 1 BBBBBBBBB
187 187 2 UUUUUUUUU
188 188 1 CCCCCCCCC
189 189 1 DDDDDDDDD
190 190 2 VVVVVVVVV
191 191 2 WWWWWWWWW
192 192 1 EEEEEEEEE
193 193 1 FFFFFFFFF
194 194 2 XXXXXXXXX
195 195 1 GGGGGGGGG
196 196 2 YYYYYYYYY
197 197 1 HHHHHHHHH
198 198 2 ZZZZZZZZZ
199 199 1 IIIIIIIII
200 200 1 JJJJJJJJJ
201 201 2 AAAAAAAAA
202 202 1 KKKKKKKKK
203 203 2 BBBBBBBBB
204 204 1 LLLLLLLLL
205 205 1 MMMMMMMMM
206 206 2 CCCCCCCCC
207 207 1 NNNNNNNNN
208 208 2 DDDDDDDDD
209 209 1 OOOOOOOOO
210 210 2 EEEEEEEEE
211 211 2 FFFFFFFFF
212 212 1 PPPPPPPPP
213 213 1 QQQQQQQQQ
214 214 1 RRRRRRRRR
215 215 1 SSSSSSSSS
216 216 1 TTTTTTTTT
217 217 1 UUUUUUUUU
218 218 1 VVVVVVVVV
219 219 1 WWWWWWWWW
220 220 1 XXXXXXXXX
221 221 1 YYYYYYYYY
222 222 1 ZZZZZZZZZ
223 223 1 AAAAAAAAA
224 224 1 BBBBBBBBB
225 225 1 CCCCCCCCC
226 226 1 DDDDDDDDD
227 227 1 EEEEEEEEE
228 228 1 FFFFFFFFF
229 229 1 GGGGGGGGG
230 230 1 HHHHHHHHH
231 231 1 IIIIIIIII
232 232 1 JJJJJJJJJ
233 233 1 KKKKKKKKK
234 234 1 LLLLLLLLL
235 235 1 MMMMMMMMM
236 236 1 NNNNNNNNN
237 237 1 OOOOOOOOO
238 238 1 PPPPPPPPP
239 239 1 QQQQQQQQQ
240 240 1 RRRRRRRRR
241 241 1 SSSSSSSSS
242 242 1 TTTTTTTTT
243 243 1 UUUUUUUUU
There are two issues:
You do not want to tell connect to use a direct connection, just leave that off, the default will be properly chosen for you. In this case, the two QObjects that you connect between live in different threads. The QThread object lives in the current thread - it's not a thread, it's a controller. The CGenerateNumbers instance lives in the new thread. So you do not want a direct connection, it must be a queued connection!
In my applications I'm using a custom build of Qt and I've done a global search-replace of QThread with QThreadController. The latter is a more descriptive and less misleading name. There is no Qt class that is really equivalent to a thread per se.
The mem->Write(); call may be blocking. It would not cause a thread to "die", but merely to block. If it's a deadlock, as I presume it might well be, the thread is stopped forever (deadlocked). It's not dead, only appears to be.
The code below demonstrates a SSCCE that works fine on both Qt 5 and Qt 4. You'd need to reduce your code to a similarly simple example, my bet is that your problem is with code that is NOT shown. Perhaps in the process of reduction you'll figure it out.
# threads-18369174.pro
QT += core
QT -= gui
TARGET = threads-18369174
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
.
// main.cpp
#include <QCoreApplication>
#include <QObjectList>
#include <QList>
#include <QThread>
#include <iostream>
const int cycleCount = 500; //!< number of cycles to run at, set to 0 to run forever
const int threadCount = 5; //!< number of threads to create
class Object : public QObject
{
Q_OBJECT
int m_threadNumber;
int m_delayUs;
volatile bool m_active;
public:
explicit Object(int delayUs, int no) : m_threadNumber(no), m_delayUs(delayUs) {}
Q_SIGNAL void indexReady(int);
Q_SLOT void stop() { m_active = false; }
Q_SLOT void start()
{
m_active = true;
while (m_active) {
usleep(m_delayUs);
emit indexReady(m_threadNumber);
}
}
};
class Consumer : public QObject
{
Q_OBJECT
QList<Object*> m_objects;
QList<QThread*> m_threads;
int m_threadCount; //!< number of active threads in m_threads
int m_count;
public:
Consumer() : m_count(0) {}
void addObject(Object * o) { m_objects << o; }
void addThread(QThread * t) {
m_threads << t;
m_threadCount ++;
connect(t, SIGNAL(finished()), SLOT(done()));
connect(t, SIGNAL(terminated()), SLOT(done()));
}
Q_SLOT void ready(int n) {
std::cout << "<" << m_count++ << ":" << n << ">" << std::endl;
if (m_count == cycleCount) {
foreach (Object * o, m_objects) o->stop();
foreach (QThread * t, m_threads) t->wait();
}
}
Q_SLOT void done() {
QThread * t = qobject_cast<QThread*>(sender());
int i = m_threads.indexOf(t);
if (t) t->deleteLater();
if (i>=0) {
std::cout << "\nThread " << i << " is done." << std::endl;
m_threadCount --;
}
if (! m_threadCount) qApp->quit();
}
};
int main(int argc, char *argv[])
{
Consumer c;
QObjectList l;
QCoreApplication a(argc, argv);
std::cout << "Running under Qt version " << qVersion() << std::endl;
for (int i = 0; i < threadCount; ++i) {
Object * o = new Object(10000 + 5000*i, i+1);
QThread * t = new QThread;
c.addObject(o);
c.addThread(t);
o->moveToThread(t);
o->connect(t, SIGNAL(started()), SLOT(start()));
c.connect(o, SIGNAL(indexReady(int)), SLOT(ready(int)));
t->start();
t->exit();
l << o;
}
return a.exec();
}
#include "main.moc"
For a given time period, thread 1 produces the most messages as it has the smallest sleep.
If your message receiver reads messages at a slower rate than the sending rate, then above output is expected as there were most messages from thread 1 and least messages from thread 3 in the message queue.
I'm new to C++, and doing a bit of googling I thought sprintf would do the job, but I get an error upon compiling that I can't convert between an unsigned char and a char. I need an unsigned char because I am going to print to an image file (0-255 RGB).
unsigned char*** pixels = new unsigned char**[SIZE];
vector<float> pixelColors;
...
sprintf(pixels[i][j][k], "%.4g", pixelColors.at(k));
(pixelColors has size of 3 and 'k' refers to a 'for loop' variable)
I'll guess that the floats are in the range 0.0 ... 1.0, then you do it like this:
float redf = 0.5f;
unsigned char reduc = redf * 255;
The variable reduc is now 128.
EDIT: complete example, outputting image in Net PPM format.
// Usage
// program > file.ppm
#include <vector>
#include <iostream>
typedef struct
{ /* colors in range 0..1 anything else is out of gamut */
float red, green, blue;
} color;
using namespace std;
int main ( int argc, char **argv )
{
int width = 10, height = 10;
vector<color> bitmap; // This should maybe be called floatmap? ;)
// Make an image in memory as a float vector
for( int y = 0; y < height; y++ )
{
for( int x = 0; x < width; x++ )
{
color temp;
temp.red = ((float)x / width);
temp.green = 0;
temp.blue = ((float)y / height);
bitmap.push_back(temp);
}
}
// output image as an Netppm pixmap
cout << "P3" << endl << width << " " << height << endl << 255 << endl;
for( int y = 0; y < height; y++ )
{
for( int x = 0; x < width; x++ )
{
int red, green, blue;
red = (unsigned char)(bitmap[y*width+x].red * 255);
green = (unsigned char)(bitmap[y*width+x].green * 255);
blue = (unsigned char)(bitmap[y*width+x].blue * 255);
cout << red << " ";
cout << green << " ";
cout << blue << " ";
}
cout << endl;
}
return 0;
}
I hope this helps you. You can read about Netpbm format on wikipedia.
EDIT2: The image output is clear text.
Result is like this:
(Tiny, isn't it? edit line 16 to 512x512 or something)
And the actual output is this:
P3
10 10
255
0 0 0 25 0 0 51 0 0 76 0 0 102 0 0 127 0 0 153 0 0 178 0 0 204 0 0 229 0 0
0 0 25 25 0 25 51 0 25 76 0 25 102 0 25 127 0 25 153 0 25 178 0 25 204 0 25 229 0 25
0 0 51 25 0 51 51 0 51 76 0 51 102 0 51 127 0 51 153 0 51 178 0 51 204 0 51 229 0 51
0 0 76 25 0 76 51 0 76 76 0 76 102 0 76 127 0 76 153 0 76 178 0 76 204 0 76 229 0 76
0 0 102 25 0 102 51 0 102 76 0 102 102 0 102 127 0 102 153 0 102 178 0 102 204 0 102 229 0 102
0 0 127 25 0 127 51 0 127 76 0 127 102 0 127 127 0 127 153 0 127 178 0 127 204 0 127 229 0 127
0 0 153 25 0 153 51 0 153 76 0 153 102 0 153 127 0 153 153 0 153 178 0 153 204 0 153 229 0 153
0 0 178 25 0 178 51 0 178 76 0 178 102 0 178 127 0 178 153 0 178 178 0 178 204 0 178 229 0 178
0 0 204 25 0 204 51 0 204 76 0 204 102 0 204 127 0 204 153 0 204 178 0 204 204 0 204 229 0 204
0 0 229 25 0 229 51 0 229 76 0 229 102 0 229 127 0 229 153 0 229 178 0 229 204 0 229 229 0 229
Not sure, what is your exact requirement [ since you didnot paste a snippet of the code .. as Greg requested ], following example might resolve it :
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
float i=1;
unsigned char c;
c = static_cast<unsigned char>(i);
cout << c << endl;
getch();
return 0;
}