Declaring a union pointer with a pointer inside [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I'm trying to learn SSE instructions and I aspire to multiply two matices. However, when I try to initialize one of them, the program crashes with an
Access violation when typing in location
Here's the code that throws the error:
typedef union{
__m128 vec;
float* afloat;
}u_float;
int main(){
__declspec(align(16)) u_float *mat1;
mat1 = (u_float*)malloc(sizeof(u_float)*4);
for(int i = 0; i < 4; i++)
mat1[i].afloat = (float*)malloc(sizeof(float)*4);
for(int i = 0; i < 4; i++)
for(int j = 0; i < 4; j++)
mat1[i].afloat[j] = 1; // Error.
return 0;}
Why is it throwing that error?
And which is the best way to resolve the problem?

This has nothing to do with unions. You have typo in your loop:
for(int i = 0; i < 4; i++)
for(int j = 0; i < 4; j++) // <-- Here
mat1[i].afloat[j] = 1;
Notice that the inner for loop loops while i is less than 4, not when j is less than 4, so this loops infinitely.

Related

Visual Studio 2015 IDE breaking for no reason [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 7 years ago.
Improve this question
Whenever I run this code, my IDE is breaking (no specific error just this Windows messages that says Programm doesn't work anymore).
Could you check if it is due to my code?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i; int array1[10], array2[10];
for (i = 0; 1 < 10; i++) {
array1[i] = i;
array2[i] = i;
}
array2[9] = 30;
for (i = 0; i < 10; i++) {
if (array1[i] == array2[i]) {
continue;
}
else {
printf("Die Arrays unterscheiden sich an Position %d\n", i);
break;
}
}
return 0;
}
Yes, I assume it is your code.
1 < 10 is always true. So for (i = 0; 1 < 10; i++) {...} runs forever.
I imagine what you meant to do was more like...
for (i = 0; i < 10; i++) {...}

My program skips the if statement [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 7 years ago.
Improve this question
I'm writing a program that would make a power of a matrix.
as you see, I'm trying to ask at the for (int n...) loop if n==0,
but when I'm debugging - I see that the program just skips the condition and doesn't even enter it. I mean it doesn't even "ask" the question if n==0...
What is the problem?
void Matrix::pow(int power, Matrix & result)
{
for (int i = 0; i < power-1; i++)
{
for (int j = 0; j < rows; j++)
{
for (int k = 0; k < cols; k++)
{
for (int n = 0; n < cols; n++)
{
if (n==0)
{
(&result)->_array[i][j] == 0; //Reset result's array.
}
(&result)->_array[i][j] += this->_array[i][n] * this->_array[n][j];
}
}
}
}
}
This is a boolean expression, not an assignment.
(&result)->_array[i][j] == 0; //Reset result's array.

Generating a random out of 2 dice roll [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 7 years ago.
Improve this question
I fixed the typos that I had in my code, and now it runs perfect
int rollDice(int diceRoll[], int numberRolling) // Random dice rolls
{
int values = 0;
for (int i = 0; i < numberRolling; i++)
{
diceRoll[i] = 1 + rand() % 6;
}
for (int i = 0; i < numberRolling; i++)
{
values = values + diceRoll[i];
}
return values;
}
You have a typo in your first for
for (int i = 0; i < numberRolling, i++)
Should be
for (int i = 0; i < numberRolling; i++)

Using arrays to find a maximum [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
I take in an array I go through all it's elements, if any member of the array is greater than j, than it makes that element the new j. For some reason I keep getting back that the maximum is 4. What's going on?
##include <iostream>
using namespace std;
int MAXIM(int arg[],int sz){
int j = 0;
for(int i = 0; i < sz; i++){
if(arg[i] > j){
j = i;
}
}
return j;
}
int main(){
int coolarr[5] = {5,17,45,7,34};
int maxxy = MAXIM(coolarr, 5);
cout << maxxy << endl;
}
j = i; this line is wrong; it should be j = arg[i];.

Accesing an element in vector of pair of integer and vector [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
In the following code, I receive segfault at the last line:
int MAX_ITER = 4, n = 5;
vector< pair<int, vector<int> > > InputVector(MAX_ITER);
srand((unsigned)time(NULL));
for (int i = 0; i < MAX_ITER; i++)
InputVector[i].second.resize(n);
for (int i = 0; i < MAX_ITER; i++) {
InputVector[i].first = i+1;
for (int j = 0; j < InputVector[i].second.size(); i++)
InputVector[i].second[j] = rand()%2;
How to access jth element of InputVector[i].second?
for (int j = 0; j < InputVector[i].second.size(); i++)
^^ should be j++