Compile time error..Lvalue requiered c++ [closed] - c++

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 8 years ago.
Improve this question
This code shows a compile time error
#include<stdio.h>
#include<conio.h>
#define SIZE 7
int a[SIZE][SIZE],q[SIZE*SIZE],visited[SIZE][SIZE],n,i,j,f=0,r=-1;
int parent[SIZE*SIZE], x_dest, y_dest, x_temp, y_temp;
int flag =0;
void find_neighbours(int x, int y)
{
if (( ((y+1)=0) && (a[x][y-1])) && (visited[x][y-1]))
{
q[++r]= x*n + (y-1);
parent[x*n + (y-1)] = x*n +y;
visited[x][y-1] =1;
}
if ((x+1)=0 && (a[x-1][y]) && !visited[x-1][y])
{
q[++r]=(x-1)*n+(y);
parent[(x-1)*n+(y)]=x*n+y;
visited[x-1][y]=1;
}
}
void bfs(int x, int y)
{
find_neighbours(x, y);
if(f<=r)
{
x_temp = q[f]/n;
y_temp = q[f] - n*x_temp;
if (x_temp == x_dest && y_temp == y_dest)
{
flag =1;
return ;
}
//visited[x_temp][y_temp] = 1;
f++;
bfs(x_temp,y_temp);
}
}
int main()
{
int x,y,val;
int source_x , source_y;
n = SIZE;
for(i=0;i< n;i++)
{
q[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
visited[i][j] = 0;
}
}
for(i=0;i<n*n;i++)
parent[i] = 0;
printf("n Enter graph data in matrix form:n");
for(i=0;i< n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
source_x = 0;
source_y = 0;
visited[source_x][source_y] =1;
x_dest = 6;
y_dest = 6;
bfs(0,0);
if (!flag)
{
printf("not reachable \n");
return 0;
}
x = x_temp;
y = y_temp;
while(x!=0 || y!=0)
{
val = parent[x *n + y];
x = val/n;
y = val - x*n;
printf("%d %dn ", x,y);
}
return 0;
}

You have code like this all over the place:
if ((x+1)=0)
x+1 is not an lvalue yet you're trying to assign to it.
Presumably you meant ==.
As an aside, your code is generally very difficult to read. What prevented you from writing documenting comments and employing meaningful variable names?

Related

How can I get the common digits of two int in C++? Example: (1234, 41567) --> 1 4

Given two int I want to get all the common digits and print out them separated by spaces.
So for example, if int x=1234; int y=41567; then I want to print out: 1 4.
This is my code. It does not work properly. When I run it, it prints 0 1 2 3 4 5 then stops.
I don't want to use vector nor arrays.
void problema3() {
int x, y, kX=0, kY=0;
cout << "x="; cin >> x;
cout << "y="; cin >> y;
int cx = x;
int cy = y;
for (int i = 0; i < 10; i++) {
kX = 0;
kY = 0;
x = cx;
y = cx;
while (x != 0 || kX==0) {
if (x % 10 == i) kX=1;
x /= 10;
}
while (y != 0 || kY == 0) {
if (y % 10 == i) kY=1;
y /= 10;
}
if (kX == 1 && kY == 1) cout << i << ' ';
}
}
int main()
{
problema3();
return 0;
}
If you're allowed to use std::set then you can do what you want as follows:
#include <iostream>
#include <set>
void print(int x, int y)
{
int individual_number1 = 0, individual_number2 = 0;
std::set<int> myset;
int savey = y;//this will be used to reset y when the 2nd do while loop finishes
do
{
individual_number1 = x % 10;
do
{
individual_number2 = y % 10;
if(individual_number1 == individual_number2)
{
myset.insert(individual_number1);
break;
}
y = y / 10;
}while( y > 0);
y = savey;
x = x / 10;
} while (x > 0);
//print out the element of the set
for(int i: myset)
{
std::cout<<i<<" ";
}
}
int main()
{
int x = 1234, y = 41567;
print(x, y);
return 0;
}
The output of the above program is as follows:
1 4
which can be seen here.
Your main bug is when assigning copies of cy.
//...
for (int i = 0; i < 10; i++) {
//...
x = cx;
y = cx; // <-- BUG! should read y = cy;
But that's not the only bug in your program.
Your digit detection logic is wrong. In particular, zero is not handled correctly, and since you did not put that reusable code in a function, your program is way more complex than it needs.
Here's the corrected logic for digit detection.
// checks if base 10 representation of a positive integer contains a certain digit (0-9)
bool hasDigit(int x, int d)
{
do
{
if (x % 10 == d)
return true;
x /= 10;
} while (x != 0);
return false;
}
Your main loop then becomes:
// assuming int x, y as inputs.
// ...
for (int i = 0; i < 10; ++i)
{
if (hasDigit(x, i) && hasDigit(y, i))
std::cout << i << ' ';
}
Which leaves very little room for bugs.
You can play with the code here: https://godbolt.org/z/5c5brEcEq

CUDA SHA-Calculation fails [closed]

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 8 years ago.
Improve this question
I want to write a SHA1-Function in CUDA, but when I execute the function, I get wrong results out of the function. When I run the same function on the CPU, I get correct results. My SHA-Function looks like:
__device__ void SHA1_CUDA(uint8_t input_string[], int slen, uint32_t Hash_ptr[])
{
printf("Input string is %s, input len is %d\n", input_string, slen);
uint32_t K[80];
uint32_t A,B,C,D,E,TEMP;
int r,k,ln,t,l,i,j;
Hash_ptr[0]=0x67452301;
Hash_ptr[1]=0xefcdab89;
Hash_ptr[2]=0x98badcfe;
Hash_ptr[3]=0x10325476;
Hash_ptr[4]=0xc3d2e1f0;
ln=slen;
r = (int)((ln+1)/64);
if (((ln+1) % 64) > 56)
{
r=r+1;
}
// initialize Constants
for(t=0; t<80; t++)
{
if (t<20)
{
K[t] = 0x5a827999;
}
if ((t>19)&(t<40))
{
K[t] = 0x6ED9EBA1;
}
if ((t>39)&(t<60))
{
K[t] = 0x8F1BBCDC;
}
if (t>59)
{
K[t] = 0xca62c1d6;
}
}
for(l=0; l <= r; l++)
{
uint32_t W[80]={0};
//Initialize Text
for (i=0; i<16; i++)
{
for(j=0; j<4; j++)
{
if (4*i+j <= ln)
{
k = input_string[64*l+4*i+j];
}
else
{
k =0;
}
if (k<0)
{
k = k +256;
}
if (4*i+j == ln)
{
k = 0x80;
}
// W[i]= W[i] + k*(uint32_t)pow(256,(double)3-j);
W[i]= W[i] + k*expo_d[3-j];
}
}
if ((W[14]==0)&(W[15]==0))
{
W[15]=8*slen;
}
// Hash Cycle
for (t = 16; t <80; t++)
{
W[t] = Rol(W[t-3]^W[t-8]^W[t-14]^W[t-16],1);
}
A = Hash_ptr[0];
B = Hash_ptr[1];
C = Hash_ptr[2];
D = Hash_ptr[3];
E = Hash_ptr[4];
for(t = 0; t < 80; t++)
{
TEMP = (Rol(A,5) + f(B,C,D,t) + E + W[t] + K[t]);
E = D;
D = C;
C = Rol(B,30);
B = A;
A = TEMP;
}
Hash_ptr[0] = Hash_ptr[0] + A;
Hash_ptr[1] = Hash_ptr[1] + B;
Hash_ptr[2] = Hash_ptr[2] + C;
Hash_ptr[3] = Hash_ptr[3] + D;
Hash_ptr[4] = Hash_ptr[4] + E;
ln = ln - 64;
}
}
(host function is analogous, only with __host__ instead of __device__).
My kernel function is
__global__ void test_sha(uint8_t pw[], int* pw_len, uint32_t H[])
{
SHA1_CUDA(pw, *pw_len, H);
}
and I'm calling it like
printf("\nTesting SHA\n");
uint32_t * H_h = (uint32_t*)malloc(sizeof(uint32_t)*5);
memset(H_h, 0, sizeof(uint32_t) * 5);
uint32_t * H_d;
cudaMalloc(&H_d, sizeof(uint32_t)*5);
cudaMemcpy(H_d, H_h, 5*sizeof(uint32_t), cudaMemcpyHostToDevice);
test_sha<<<1, 1>>>(Pass_d, Pass_len_d, H_d);
cudaMemcpy(H_h, H_d, 5*sizeof(uint32_t), cudaMemcpyDeviceToHost);
cudaFree(H_d);
for(int i = 0; i < 5; i++)
printf("%x ", H_h[i]);
printf("\n\n");
printf("Comparing to CPU: \n");
SHA1_CUDA_h(Pass_h, Pass_len, H_h);
for(int i = 0; i < 5; i++)
printf("%x ", H_h[i]);
printf("\n\n");
free(H_h);
So, my printf-function in the SHA-function tells me that everything has been transferred correctly, but nevertheless I get wrong results...
Where is my mistake?
Problem solved, the ROL-function Rol_CUDA I was using in my function returned bad values, thus no one except me could solve the problem.
For everyone who wants to use this function: In line 51 on pastebin, there should be a 32-y, and not a -y. With this correction everything works.

Execution automatically quits when executing a program coded using visual studio in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The execution stops automatically, when I execute the program. I access this function multiplyImages. The code is as followed
#include "rt_nonfinite.h"
#include "multiplyImage.h"
/* Function Declarations */
static double rt_roundd_snf(double u);
/* Function Definitions */
static double rt_roundd_snf(double u)
{
double y;
if (fabs(u) < 4.503599627370496E+15) {
if (u >= 0.5) {
y = floor(u + 0.5);
} else if (u > -0.5) {
y = u * 0.0;
} else {
y = ceil(u - 0.5);
}
} else {
y = u;
}
return y;
}
void multiplyImage(const unsigned char img[/*2115216*/6], double parameter, unsigned
char imgout[/*2115216*/6])
{
int i0;
double d0;
unsigned char u0;
/* implements a function that multiplies an image with a parameter */
for (i0 = 0; i0 < 6; i0++)
{
d0 = rt_roundd_snf(parameter * (double)img[i0]);
if (d0 < 256.0)
{
if (d0 >= 0.0) {
u0 = (unsigned char)d0;
} else
{
u0 = 0;
}
} else if (d0 >= 256.0)
{
u0 = MAX_uint8_T;
} else
{
u0 = 0;
}
imgout[i0] = u0;
Ou[i0] = imgout[i0];
}
printf(d0);
}
When the execution comes to printf, the execution automatically stops. The img[6] and imgout[6] are just dummy images created to test the working.. I am using this function in another main function as followed.
unsigned char test[6];
for (int c = 0; c < color; ++c)
{
for (int h = 0; h < height; ++h)
{
for (int w = 0; w < width; ++w)
{
x = 'R';
Image_Conversion(c, h, w) = x;
test[c*h*w] = Image_Conversion(c, h, w);
std::cout << "Test: " << test[c*w*h];
}
}
}
multiplyImage(&(const unsigned char)test[6], 1, &test[6]);
It would be great if someone could guide me with the issue.
"Execution automatically quits" seems to be an euphemism for "my program segfaults". d0 is of type double, and you should tell printf about that fact. Please try this code instead:
printf("%f", d0);

Buddy Allocation Algorithm - Heap Extension

This is a second inquiry towards my implementation of a Buddy Allocation scheme, the first question is here, which also explains what Buddy Allocation actually is. In the standard implementation, one starts with a large block of 2^i where i is an integer, which works with a static heap size (the entire heap is the largest block in this case).
My question hinges on an implementation that deals with a dynamically sizing heap, where the heap size starts at 0. Currently, when the highest order i, cannot find a block in a free list (a list of free blocks), I make a call to extend the heap size in order to appropriate this highest order block.
The problem is that I am not sure if this derivative breaks the invariant within the buddy system, which is the calculation of the buddy block's address given an address. This simply can be computed via flipping the ith order bit. The explanation of this calculation is in my previous question. When I implement this scheme sometimes I return the wrong buddy address.
I'm not absolutely sure if you can increase the heap size after some blocks have already been allocated. I think you will have to increase your heap and reallocate all the blocks again, following the allocation algorithm, but now considering your new heap size.
#include <stdio.h>
typedef struct X
{
unsigned int address;
int empty;
int id_req;//id_req
int allow;
} Block;
typedef struct Y
{
int enz;
int id_req;//req_id
int ok;
} Defer;
unsigned int block_sizes[8] = {0};
unsigned int memSize;
unsigned int allcSize;
Block blocks[8][128];
int num_blocks[8];
int defer_pointer = 0;
Defer defers[100];
int main(int argc, char **argv)
{
int x, y, z;
for(x = 0;x < 100;x++)
defers[x].ok = 1;
for(x = 0;x < 8;x++)
for(y = 0;y < 128;y++)
blocks[x][y].empty = blocks[x][y].allow = 0;
scanf("%u", &memSize);
scanf("%u", &allcSize);
block_sizes[0] = allcSize;
for(x = 0;x+1 < 8;x++)
if(block_sizes[x] == memSize)
break;
else
block_sizes[x+1] = block_sizes[x]*2;
blocks[x][0].address = 0;
blocks[x][0].empty = 1;
num_blocks[x] = 1;
for(;x > 0;x--)
{
num_blocks[x-1] = num_blocks[x]*2;
for(y=0;y < num_blocks[x];y++)
{
blocks[x-1][2*y].address = blocks[x][y].address;
blocks[x-1][2*y+1].address = blocks[x][y].address + block_sizes[x-1];
}
}
while(scanf("%d", &z) != EOF)
{
char op = getchar();
while(op != '-' && op != '+') op = getchar();
if(op == '+')
{
unsigned int size;
scanf("%u", &size);
printf("Request ID %d: allocates %u byte%s.\n", z, size, size == 1 ? "" : "s");
int enz = 0;
while(block_sizes[enz] < size)
enz++;
unsigned int address;
if(allocate(enz, z, &address))
{
printf("\tSuccess; addr = 0x%08x.\n", address);
}
else
{
printf("\tRequest deferred.\n");
defers[defer_pointer].ok = 0;
defers[defer_pointer].enz = enz;
defers[defer_pointer].id_req = z;
defer_pointer++;
}
}
else
{
printf("Request ID %d: deallocate.\n", z);
int success = 0;
for(x = 0;x < 8 && block_sizes[x] != 0 && success != 1;x++)
for(y = 0;y < num_blocks[x] && success != 1;y++)
if(blocks[x][y].allow)
if (blocks[x][y].id_req == z)
{
blocks[x][y].allow = 0;
blocks[x][y].empty = 1;
success = 1;
}
x--;y--;
if(success)
printf("\tSuccess.\n");
else
continue;
// the buddy system
while(x < 8 && num_blocks[x] > 1)
{
int buddy = (blocks[x][y].address / block_sizes[x]) %2 == 0 ? (y+1) : (y- 1);
if(blocks[x][buddy].empty)
{
blocks[x][y].empty = 0;
blocks[x][buddy].empty = 0;
blocks[x+1][y/2].empty = 1;
x++;
y /= 2;
}
else
{
break;
}
}
for(x = 0;x < defer_pointer;x++)
if(!defers[x].ok)
{
unsigned int address;
if(allocate(defers[x].enz, defers[x].id_req, &address))
{
defers[x].ok = 1;
printf("\tDeferred request %d allocated; addr = 0x%08x\n", defers[x].id_req, address);
}
}
}
}
return 0;
}
int allocate(int enz, int id_req, unsigned int *address)
{
int x, y, ret = 0;
for(x = enz;x < 8 && block_sizes[x] != 0 && ret == 0;x++)
for(y = 0;y < num_blocks[x] && ret == 0;y++)
if(blocks[x][y].empty)
ret = 1;
x--;y--;
if(ret == 0)
return 0;
while(x != enz)
{
blocks[x][y].empty = 0;
blocks[x-1][2*y].empty = 1;
blocks[x-1][2*y+1].empty = 1;
x = x-1;
y = 2*y;
}
blocks[x][y].empty = 0;
blocks[x][y].id_req = id_req;
blocks[x][y].allow = 1;
*address = blocks[x][y].address;
}

C++ time spent allocating vectors

I am trying to speed up a piece of code that is ran a total of 150,000,000 times.
I have analysed it using "Very Sleepy", which has indicated that the code is spending the most time in these 3 areas, shown in the image:
The code is as follows:
double nonLocalAtPixel(int ymax, int xmax, int y, int x , vector<nodeStructure> &nodeMST, int squareDimension, Mat &inputImage) {
vector<double> nodeWeights(8,0);
vector<double> nodeIntensities(8,0);
bool allZeroWeights = true;
int numberEitherside = (squareDimension - 1) / 2;
int index = 0;
for (int j = y - numberEitherside; j < y + numberEitherside + 1; j++) {
for (int i = x - numberEitherside; i < x + numberEitherside + 1; i++) {
// out of range or the centre pixel
if (j<0 || i<0 || j>ymax || i>xmax || (j == y && i == x)) {
index++;
continue;
}
else {
int centreNodeIndex = y*(xmax+1) + x;
int thisNodeIndex = j*(xmax+1) + i;
// add to intensity list
Scalar pixelIntensityScalar = inputImage.at<uchar>(j, i);
nodeIntensities[index] = ((double)*pixelIntensityScalar.val);
// find weight from p to q
float weight = findWeight(nodeMST, thisNodeIndex, centreNodeIndex);
if (weight!=0 && allZeroWeights) {
allZeroWeights = false;
}
nodeWeights[index] = (weight);
index++;
}
}
}
// find min b
int minb = -1;
int bCost = -1;
if (allZeroWeights) {
return 0;
}
else {
// iteratate all b values
for (int i = 0; i < nodeWeights.size(); i++) {
if (nodeWeights[i]==0) {
continue;
}
double thisbCost = nonLocalWithb(nodeIntensities[i], nodeIntensities, nodeWeights);
if (bCost<0 || thisbCost<bCost) {
bCost = thisbCost;
minb = nodeIntensities[i];
}
}
}
return minb;
}
Firstly, I assume the spent time indicated by Very Sleepy means that the majority of time is spent allocating the vector and deleting the vector?
Secondly, are there any suggestions to speed this code up?
Thanks
use std::array
reuse the vectors by passing it as an argument of the function or a global variable if possible (not aware of the structure of the code so I need more infos)
allocate one 16 vector size instead of two vectors of size 8. Will make your memory less fragmented
use parallelism if findWeight is thread safe (you need to provide more details on that too)