Cannot access memory - c

I'm getting a segfault, and when I debug it I get the following message:
Cannot access memory at 0x806d128
I attempted to set a watchpoint there to monitor the address, but the response gdb gave me was:
Cannot watch constant value '0x806d128'
However when I print the pointer containing the address it recognises the struct that I've got stored there:
(body *) 0x806d128
According to gdb the error occurs in addForce(), which is a bit odd since that address is accessed earlier in init()
Here's the relevant code:
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define prec float
prec gdt = 0.0001;
typedef struct {
float Fx, Fy, vx, vy, mass;
int posX, posY;
} body;
void addForce(body* a, body* b){
int xa=(*a).posX, ya=(*a).posY;
int xb=(*b).posX, yb=(*b).posY;
float F=(*a).mass * (*b).mass * gdt /(pow(xa-xb, 2)+pow(ya-yb, 2));
float v=atan((ya-yb)/(xa-xb));
float Fx=cos(v)/F, Fy=sin(v)/F;
(*a).Fx+=Fx;
(*a).Fy+=Fy;
(*b).Fx-=Fx;
(*b).Fy-=Fy;
}
int newRandInt(int bot, int top){
return bot + (rand() % (top++));
}
prec newRand()
{
prec r = (prec)((double)rand()/(double)RAND_MAX);
return r;
}
void init(int N, body* star){
for(int i=0; i<N;i++){
star[i].posX = newRandInt(0, 800);
star[i].posY = newRandInt(0, 800);
star[i].Fx = newRand();
star[i].Fy = newRand();
star[i].vx = newRand();
star[i].vy = newRand();
star[i].mass = newRand();
}
}
int main(int argc, char* argv[]) {
int N = 200;
body* stars = malloc(sizeof(body)*N);
init(N, stars);
for(int j1=0; j1<N; j1++){
for(int j2=0; j2<N-1; j2++){
if(j1!=j2){
addForce(stars+sizeof(body)*j1, stars+sizeof(body)*j2);
}
}
}
return 0;
}

This call to addForce is not what you want:
addForce(stars+sizeof(body)*j1, stars+sizeof(body)*j2);
You don't want the sizeof(body) expression in this call to addForce. The call you probably want is:
addForce(stars+j1, stars+j2);
stars is an array of body so you can reference individual stars with
body *p = &stars[i]
or just
body *p = stars + i
Both of these have the type body*

In this line:
addForce(stars+sizeof(body)*j1, stars+sizeof(body)*j2);
when you do sizeof(body) it evaluates to size of structure in bytes, then it is multiplied by j1. So you have some_nr*j1.
That value is added to stars, which is pointer to body.
Pointer arithmetics evaluate some_nr*j1*sizeof(pointer to body).
What you want to do is stars+j1 alone, which will be correctly evaluated as pointer to body and added to arrays.
The same happends in the second part of addForce.
What you missed is that with pointers arithmetics the second operand of '+' operator is recalculated to match size of pointee.

Related

Ex. 2.1 K&R 2nd edition: How can I calculate the variables?

I am a beginner in programming. I referred to power function in chapter 1 and modified it to be power_sum function. My calculation is to insert value of bits in "show_val()" to show range of each variable. The problem is in the main when I want to insert a value into show_val. Please guide me how to solve this. Thank you.
#include <stdio.h>
int power_sum(int base, int n);
void show_val(int power);
int main() {
unsigned char vc = show_val(8);
/* I try to show range of unsigned char here, but because show_val
is a void function, so it becomes error.*/
}
/* power_sum: raise base to n-th power and sum up; n => 0 */
int power_sum(int base, int n) {
int i, p, sum;
p = 1;
sum = 0;
for (i = 1; i <= n; ++i) {
p = p * base;
sum = sum + p;
}
sum = sum - 2;
return sum;
}
/* show_val: to show value of each variable */
void show_val(int pw) {
int n;
int i;
n = power_sum(2, pw);
for (i = 0; i < 10; ++i) /* to display increments of a value ten times */ {
printf("%d\n", ++n);
}
}
You are trying to assign void function void show_val(int pw) (a function that returns nothing) to unsigned char vc variable. If you want to return value from show_val function change: int show_val(int pw) and return something.
Otherwise, if you don't want to return anything, you can just call function in main:
int main()
{
show_val(8);
}
unsigned char vc = show_val(8);
Let us break the above piece of code into LHS and RHS.
LHS :
Its a variable expecting a character to be inputed.
Now what is being done in RHS
RHS:
You have given show_val(8)
Which is a mistake
Why?
Because if you see you show_val declaration:
void show_val(int)
You are returning a void i.e nothing.
And trying to equate with char on LHS.
So, the compiler will definitely throw an error.
There are more changes to be made,
1st edit this part correctly in the question if you are clear and then we can look upon the other changes.

Why is this function parameter not treated as local variable to this function?

I have a function which returns an integer pointer type:
int* f(int a, int b){
int *result;
result = &a;
*result += b;
return result;
}
and when I call this on main:
int main(){
int a = 5;
int b = 2;
int *result = f(a,b);
printf("The result is: %d \n", *result);
return 0;
}
It gives me the correct output(in this case 7). I was under the impression that by assigning the address of the parameter a to result I would get a segmentation fault when I ran this function.
My assumption is that C treats function parameters as local in scope to the function definition. But, I see that this is not the case so why is this specific program working ?
I'm using Code::Blocks 16.01 with gcc compiler.
Just because it works on your machine doesn't mean it isn't undefined behaviour. This works by fluke, but it's invalid.
It may produce the correct result because that stack is not overwitten or otherwise mangled by the time you do something later on.
For example, if you make another function call:
#include <stdio.h>
#include <stdlib.h>
int noop(int x, int y) {
return x + y;
}
int* f(int a, int b){
int *result;
result = &a;
*result += b;
return result;
}
int main(){
int a = 5;
int b = 2;
// Do something with undefined behaviour
int *result = f(a,b);
// Do something else which uses the stack and/or the same memory
int x = 10;
int y = 11;
int z = noop(x, y);
printf("The result is: %d \n", *result);
return 0;
}
Now the output gets stomped with the definition of x which coincidentally takes the same piece of memory so the output is 10. As this is undefined behaviour, though, anything could happen, including a crash.

Cast void pointer to double without knowing its type in C

I need to write a function which takes as parameters a void pointer (representing an array) and an integer which represents the length of the array. In the function, I need to cast this void pointer into a double one. The problem is that the void pointer can represent an array of integers or floats or doubles.
So the following is obviously not working in case the void pointer represents an array of integers or floats:
void foo(void *v,int n){
double *values;
values=(double*)v;
for(i=0;i<n;i++)
printf("%f\n",values[i]);
}
so this will print the correct output:
foo((double[]){1,2,3,4},4);
and this will print a wrong output:
foo((int[]){1,2,3,4},4);
foo((float[]){1,2,3,4},4);
So... can I correctly cast the void pointer to a double one only knowing that it can be an array of integers or floats or doubles? And is it necessary to have the length of the array or I can calculate it somehow?
P.s. without using callbacks
can I correctly cast the void pointer to a double one only knowing that it can be an array of integers or floats or doubles?
No, you cannot. You need to pass the type information somehow.
And is it necessary to have the length of the array or I can calculate it somehow?
It's necessary to pass the length.
You can "hide" the information inside a struct
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum etype { INTEGER, FLOAT, DOUBLE };
struct data {
void *values;
size_t n;
enum etype type;
};
void foo(struct data *x) {
int *xd = x->values;
float *xf = x->values;
double *xg = x->values;
for (int k = 0; k < x->n; k++) {
switch (x->type) {
default: printf("%g ", xg[k]); break;
case FLOAT: printf("%f ", xf[k]); break;
case INTEGER: printf("%d ", xd[k]); break;
}
}
puts("");
}
int main(void) {
struct data x;
x.values = malloc(4 * sizeof(int));
((int*)(x.values))[0] = 42;
((int*)(x.values))[1] = -1;
((int*)(x.values))[2] = 0;
((int*)(x.values))[3] = 999;
x.n = 4;
x.type = INTEGER;
foo(&x);
x.values = calloc(4, sizeof(float));
x.type = FLOAT;
float tmp = 3.14159;
memcpy(((float*)(x.values))+1, &tmp, sizeof (float));
foo(&x);
free(x.values);
x.values = calloc(4, sizeof(double));
x.type = DOUBLE;
((double*)(x.values))[2] = 2.7182818;
foo(&x);
free(x.values);
}
See code running on ideone

C malloc of pointer to pointer inside function not giving correct size

So I am now rewriting my fortran code in C (to use CUDA), and apparently I do not understand how to properly use malloc and pointers. I am trying to make the main function just calls to other functions, which need to malloc arrays that will then be used inside other functions. So, I am passing pointers of pointers to them as per this post: C Programming: malloc() inside another function
But the right amount of memory is not being allocated so I get segmentation faults. Here is the code:
#include <stdio.h>
#include <stdlib.h>
//#include <cuda.h>
#include <math.h>
//#include "cublas.h"
//datatype to match FORTRAN complex type
typedef float real;
typedef struct{
int nx;
int ny;
int nz;
int sz;
int tz;
} states;
void set_SPB(real **,int,states **,states **,int **);
//void set_SPB();
int find_minimum(int a[], int n,int start);
const real hc =197.32697,pi=3.1415927;
int main(){
int nmax = 2, A = 28;
real *etemp, *fock;
int *Ndex,*lookup,*lookup_a;
states *channel,*SPB;
//!generates the single particle basis to be used
set_SPB(&etemp,nmax,&SPB,&channel,&Ndex);
free(etemp);
free(Ndex);
free(SPB);
return 0;
}
void set_SPB(real **etemp,int nmax,states **SPB,states **channel,int **Ndex){
int tot_orbs = (2*nmax+1)*(2*nmax+1)*(2*nmax+1)*4;
int D = tot_orbs/4;
int Nalpha = (2*nmax+1)*(2*nmax+1)*(2*nmax+1)*9;
real E;
*etemp = (real*)malloc(D);
*Ndex = (int*)malloc(D*3);
*SPB = (states*)malloc(tot_orbs);
printf("orbits without spin degeneracy %d \n",D);
printf("size of etemp %ld \n",sizeof(*etemp)/sizeof(*etemp[0]));
return;
int i = 0;
for(int nx =-nmax;nx<=nmax;nx++){
for(int ny =-nmax;ny<=nmax;ny++){
for(int nz =-nmax;nz<=nmax;nz++){
E = 0.5*4.0*pi*pi*(nx*nx+ny*ny+nz*nz);
//printf("%d\n",i);
*etemp[i] = E;
*Ndex[0*D+i] =nx;
*Ndex[1*D+i] = ny;
*Ndex[2*D+i] = nz;
i+=1;
}
}
}
return;
}
Also I am not sure exactly if my assignments of the arrays are correct.
Specifically the print to find the number of elements of that have been allocated always gives 2, when it should be D = 125.
I cannot believe that float and int take only 1 byte in your environment.
Multiply the size to be allocated by size of their elements.
*etemp = malloc(sizeof(**etemp) * D);
*Ndex = malloc(sizeof(**Ndex) * D*3);
*SPB = malloc(sizeof(**SPB) * tot_orbs); /* not sure because this is not used */
Note that they say you shouldn't cast the result of malloc() in C.
Also note that [] operator has higher precedence than * operator, so you have to use parentheses to use the arrays.
(*etemp)[i] = E;
(*Ndex)[0*D+i] =nx;
(*Ndex)[1*D+i] = ny;
(*Ndex)[2*D+i] = nz;

Changing int to double changes my result to zero

If I leave all variables as int I get 32. The division is supposed to give me 32.5 so I thought that changing everything to double would do it, but it just gives me zero...
Here is the code (everything in int):
#include <stdio.h>
#include <stdlib.h>
void sommeTableau(int tableau[], int tailleTableau, int *x, int *average);
int main(int argc, char *argv[])
{
int tableau[4] = {30, 50, 50};
int *x = 0;
int *average = 0;
const int tailleTab = 4;
sommeTableau(tableau, tailleTab, &x, &average);
printf("The average is %d\n", average);
return 0;
}
void sommeTableau(int tableau[], int tailleTableau, int *x, int *average)
{
int i = 0;
for (i = 0 ; i < tailleTableau ; i++)
{
*x = *x + tableau[i];
}
*average = *x/tailleTableau;
}
So this works and gives me 32... Now if I change everything to double and %d to %f in the printf function, it gives me zero and I don't understand why...
There is lots of mistakes in your code. I corrected all of them
#include <stdio.h>
#include <stdlib.h>
void sommeTableau(int tableau[], int tailleTableau, int *x, double *average);
int main(int argc, char *argv[])
{
int tableau[4] = {30, 50, 50};
int x = 0;
double average = 0;
const int tailleTab = 4;
sommeTableau(tableau, tailleTab, &x, &average);
printf("The average is %lf\n", average);
return 0;
}
void sommeTableau(int tableau[], int tailleTableau, int *x, double *average)
{
int i = 0;
for (i = 0 ; i < tailleTableau ; i++)
{
*x = *x + tableau[i];
}
*average = (double)(*x)/(double)tailleTableau;
}
You are passing x and average as null pointer. Thats why your code is giving segmentation fault.
As per you describtion please check(http://codepad.org/z8fVUTe5).
#include <stdio.h>
#include <stdlib.h>
void sommeTableau(int tableau[], int tailleTableau, int *x, int *average);
int main(int argc, char *argv[])
{
int tableau[4] = {30, 50, 50};
int x = 0;
int average = 0;
const int tailleTab = 4;
sommeTableau(tableau, tailleTab, &x, &average);
printf("The average is %d\n", average);
return 0;
}
void sommeTableau(int tableau[], int tailleTableau, int *x, int *average)
{
int i = 0;
for (i = 0 ; i < tailleTableau ; i++)
{
*x = *x + tableau[i];
}
*average = *x/tailleTableau;
}
Output:-
The average is 32
One of your problems lies here:
int *x = 0;
int *average = 0;
sommeTableau(tableau, tailleTab, &x, &average);
Those first two lines should be:
int x = 0;
int average = 0;
This is evident from the warnings you should be getting, assuming you're using a decent compiler:
qq.c: In function ‘main’:
qq.c:13: warning: passing argument 3 of ‘sommeTableau’ from incompatible pointer type
qq.c:4: note: expected ‘int *’ but argument is of type ‘int **’
qq.c:13: warning: passing argument 4 of ‘sommeTableau’ from incompatible pointer type
qq.c:4: note: expected ‘int *’ but argument is of type ‘int **’
In addition, you don't need _everything to be a double, just the average, so you should only change the type of that variable and match that with the parameters passed to the function.
It will also be necessary to cast the total *x to a double before doing the division, so that it knows you don't mean integer division.
With those changes, the output changes from:
The average is 4.000000
(for me) to the correct:
The average is 32.500000
See the following program for more detail:
#include <stdio.h>
#include <stdlib.h>
void sommeTableau(int tableau[], int tailleTableau, int *x, double *average) {
int i;
for (i = 0 ; i < tailleTableau ; i++)
*x = *x + tableau[i];
*average = ((double)(*x))/tailleTableau;
}
int main (void) {
int tableau[4] = {30, 50, 50};
int x = 0;
double average = 0;
const int tailleTab = 4;
sommeTableau(tableau, tailleTab, &x, &average);
printf("The average is %lf\n", average);
return 0;
}
int *x = 0;
int *average = 0;
This is defining two pointers and setting the pointers to zero -- which, in a pointer context translates to a null pointer. So, you have to pointers to nothing.
sommeTableau(tableau, tailleTab, &x, &average);
Here, you're passing the addresses of those pointers to the function, so the function is really receiving an int **.
printf("The average is %d\n", average);
This is then taking the value of the pointer -- the address its holding -- and (probably) treating that bit pattern as an int. This is technically undefined behavior, but on many machines, a pointer it enough like an int for it to appear to work. The same will almost never be true with a floating point number though.
What you want to do here is define two ints (or two doubles) and pass their addresses to your function:
double x=0.0, average=0.0;
sommeTableau(tableau, tailleTab, &x, &average);
// ...
printf("%f, %f\n", x, average);
It doesn't actually work.
Here you define x and average as NULL pointers to int:
int *x = 0;
int *average = 0;
And here instead of pointers to int you pass pointers to pointers to int:
sommeTableau(tableau, tailleTab, &x, &average);
Which is clearly wrong. You should enable compiler warnings to see such problematic places and correct them.
Here the correction would be:
int x = 0;
int average = 0;
Now, here:
printf("The average is %d\n", average);
You are lying to printf() about what you're going to give it (an int, because of %d), but you're actually giving it a pointer to int. The above correction fixes this. If you lie to printf() about the type of some parameter, anything can happen. If your compiler is gcc and you enable warnings, you can spot this kind of problems as well.
Something is terribly wrong here -- you're defining x and average as pointer variables in main, which if you are lucky is the same size as an int, but may result in undefined behavior.
Remove the * from the local variable declarations of x and average in main and it may actually work.
Also, in sommeTableau, both operands of the division (*x and tailleTableau) are int, so integer division is performed and the remainder discarded prior to assignment to *average.

Resources