Unhandled exception at 0x012219c4 in SW-Serial.exe [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i declare 2d dynamic array. when run program this error shown:
Unhandled exception at 0x012219c4 in SW-Serial.exe: 0xC0000005: Access violation writing location 0xabababab.
the part of my program that error occured:
double** SWArray;
SWArray = (double**) malloc(lenA*sizeof(double*));
for (int i = 0; i <= lenA; i++)
SWArray[i] = (double*) malloc(lenB*sizeof(double));
for(int i=0;i<=lenA;i++){
SWArray[0][i]=0;
}
for(int j=0;j<=lenB;j++){
SWArray[j][0]=0;
}
picture of this problem

Arrays start from 0 in C. Wherever you say i <= lenA it should be i < lenA. Same goes for j and lenB. Also, the second loop doesn't really make sense. Did you mean lenB instead of lenA ?

You have lenA And lenB mixed up. It should be:
SWArray[i][0] and SWArray[0][j] in your loops.
And you loops should use < not <=

Both the loops are indexing in the wrong dimension and also are accessing elements beyond the size for which memory is allocated. You should change i<= to i< in both the loops
The correct way should be:
for(int i=0;i<lenA;i++){
SWArray[i][0]=0;
}
for(int j=0;j<lenB;j++){
SWArray[0][j]=0;
}

Related

Why does memcpy change the last element of original array? [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 5 years ago.
Improve this question
I encountered a weird bug in this code:
int *a = (int*) malloc(N*sizeof(int)); // allocate array
int i;
for (i=2; i<=N; i++)
{
a[i] = i;
}
int *b = (int*) malloc(N*sizeof(int));
memcpy(b, a, N*sizeof(b));
If I were to print out array a, output = 2,3,...,19 0
Whereas the expected output should have been 2,3,...,19,20.
Copying the array onto b strangely affected the last element.
An array of N elements has valid indexes 0, 1, ..., N - 1. Your final loop round accesses a[N], which is out of bounds and has undefined behaviour.

Segmentation fault (Core dumped) error when trying to populate a 2d matrix [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 6 years ago.
Improve this question
I'm trying to populate my 2D array with random integers.
//rows, cols are randomly generated ints between 1 and 10
int rows = rand()%10 +1;
int cols = rand()%10 +1;
int arr[rows][cols];
for (i = 0; i <rows; i++){
for (j=0; i<cols; j++){
arr[i][j] = rand()%10;
}}
Every time I try to run this code, it gives me a segmentation default.
I edited the post to give the actual code I am running
I suppose it's because the i < ... which should be a j < ... in for (j=0; i<cols; j++);

for loop in c not outputting full array [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 6 years ago.
Improve this question
this is a simple question as i'm a new-comer to C. I am trying to write a script for outputting an array of the tangents of radians, of multiples of 5 from 0-60. but for some reason the for loop i have written only does this for the first element, and all other elements in the resulting array are 0.00, and it wont print them for each loop. i'm sure i've done something simple wrong with my loop, but i just can't see it.
#include <stdio.h>
#include <math.h>
float rad(float degree){
return degree*M_PI/180;
}
int main(void){
int i, j, dim=13;
float Tan[dim];
for(i=0; i<13; i++);{
j+=5;
Tan[i]=tan(rad(j));
printf("%f\n", Tan[i]);
}
return 0;
}
First of all, in your code
j+=5;
is undefined behavior, as the intial value of j is indeterminate. To elaborate, j is an automatic local variable and not initialized explicitly, so the content is indeterminate.
Then, the for loop is also buggy.
for(i=0; i<13; i++);
should be
for(i=0; i<13; i++) // no ; here
to have a meaningful loop body to be executed.
1. You have inserted a semi-colon which you shouldn't have. Change your loop to :
for(i = 0; i < 13; i++){ //erase the ; after the parenthesis
j+=5;
Tan[i] = tan(rad(j));
printf("%f\n", Tan[i]);
}
2. Initialize variable j before trying to increase it with the statement j+=5, as this will lead to undefined behaviour.
There are two problems in your code :-
1) You haven't initialized j here int i, j, dim=13;
2) The way you used for loop is as per your requirement.Remove semicolon from the for loop statement.

Dynamic Array of strings [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 6 years ago.
Improve this question
I am trying to create an array of strings but I keep getting an error.
Can you help me figure out what's wrong with this code?
int size;
scanf("%d",&size);
char** arr;
arr=(char**)malloc(sizeof(char*)*size);
You can simply use array of n number of pointers to char. Then use a loop to allocate space for those.
int n, size;
scanf("%d %d", &n, &size);
char *arr[n];
for( int i = 0; i < n; ++i ){
arr[i] = malloc( size * sizeof(char) );
}

Array and loops [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have an array contain 100 elements. Can anyone help me to figure out how to write loops that perform this:
data[0] = 1/(2*3*4)
data[1] = 1/(4*5*6)
data[2] = 1/(6*7*8)
...
data[99] = 1/(200*201*202)
data[0]-data[1]+data[2]-data[3]+data[4]-data[5]+...+data[98]-data[99]
I just can't understand how to start. Any suggestions would be appreciated!
Try this
double c=0;
for (int i=0;i<100;i++)
{
c=i*2+2;
data[i]=1/(c*(c+1)*(c+2));
}
for (int i = 0; i < 100; i+=2)
{
op+= data[i] - data[i+1];
}
My suggestions how to start, if you really want them and want to manage this by your own:
Generalize your algorithm:
Find a function f(x) such that data[i] = f(i)
Just write the algorithm in your native language.
Then learn basic operators of C language, including loop construct.
Write your "native language algorithm" in C language.
Just in one loop:
int total = 0;
for(size_t i=0; i<100; ++i){
int temp = (i+1)*2;
data[i] = 1/(temp*(temp+1)*(temp+2));
total = total + (i%2==0?data[i]:-data[i]);
}

Resources