Getting seg fault in gdb (debugging) - c

I am a bit new to pointers. I am receiving this error while running the code below specified using gcc compiler in ububtu.
Error:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7de7c13 in _dl_fini () at dl-fini.c:235
235 dl-fini.c: No such file or directory
code
.
#include <stdio.h>
#include <cmath>
#include<stdlib.h>
void update(int *a,int *b) {
int *c;
*c=*a;
*c=abs(*a+*b);
*b=abs(*a-*b);
*a=*c;
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
please post any suggestions/solutions regarding this.

I suspect this is what you were trying to do.
void update(int *a,int *b) {
int c;
c=*a;
c=abs(*a+*b);
*b=abs(*a-*b);
*a=c;
}

The problem is that you're never allocating memory for that pointer c declared in your update() function:
int *c; // <<< not initialized
You must allocate memory to use that pointer:
int *c = new int();
and free it when leaving your function:
delete c;
The easier way of course is just to have a variable
int c = *a;
on the functions local stack, and omit the dereferencing:
c=abs(*a+*b);
// ^ Note the * was removed

Due to the fact your tagged your question with c++ and your code has nothing to do with c++ here is a real c++ code:
#include <iostream>
#include <cmath>
void update(int &a,int &b) {
int c;
c=a;
c=abs(a+b);
b=abs(a-b);
a=c;
}
int main() {
int a, b;
std::cin >> a;
std::cin >> b;
update(a, b);
std::cout << a << std::endl << b << std::endl;
}
I strongly recommend you use some compiler flags which would have already warn you while the compilation and even the code would not have been generated.
For g++ my suggested options are:
-std=c++11 -Wall -Werror -Wextra

Related

How to create a function from other 3 with the same signature in C

I'm working with C, and I'm trying to build a kind of "composite" function, by joining 2 functions with the same signature, and a third with almost the same signature but just one less argument. The functions should be executed in sequence, but the final function must have the same signature. You can think it as building a function with code blocks using same signature(I have it implemented in C++ with policy class, but I'm trying a C approach as the rest of the code is in C already).
I built some code very simple, just to explain my approach.
#include <stdio.h>
#include <stdlib.h>
typedef void simulFileProc(int a, int b);
typedef void simulRead(int a);
typedef struct compFunct{
simulFileProc* file1;
simulRead* read;
simulFileProc* file2;
} compFunct;
void realProc(int a, int b){
printf("call from realProc %d, %d\n",a,b);
}
void realRead(int a){
printf("call from read %d\n",a);
}
simulFileProc* join(int a, int b, compFunct* func){
void sf(int a, int b){
func->file1(a,b);
printf("intermediate line\n");
func->read(a);
func->file2(a,b);
}
return &sf;
}
int main() {
compFunct* c = malloc(sizeof(256));
c->file1 = &realProc;
c->read = &realRead;
c->file2 = &realProc;
int a=0;
int b=0;
simulFileProc* s = join(a,b,c);
s(4,3);
return 0;
}
It is working, but for some reason, just the first function print.
call from realProc 4, 3
intermediate line
If I comment the line "func->read(a);", I have a segmentation fault.
What is wrong ?? Is there a smarter way to do ?

Why is Segmentation fault when using GMP?

I am using the GMP. My program can build successfully, But run failed. The following is error things:
a=1231231231231231
res^n != a
Segment fault
All codes in my program is:
#include <gmp.h>
#include <stdio.h>
int main()
{
mpz_t a,res;
unsigned long int n = 123;
char str1[] = "1231231231231231";
mpz_init_set_str(a, str1, 10);
gmp_printf("a=%Zd\n",a);
mpz_init(res);
if(mpz_root(res, a, n)){
printf("res^n == a\n");
}
else{
printf("res^n != a\n");
}
mpz_clears(a,res);
return 0;
}
You have to call mpz_clears() like:
mpz_clears(a,res, NULL);
Here's what the documentation says:
Function: void mpz_clears (mpz_t x, ...)
Free the space occupied by a NULL-terminated list of mpz_t variables.

Pointer as parameter in C

The code doesn't work in my Xcode compiler. It says *&point expected '('. I really don't know what goes wrong. It should have worked.
#include<stdio.h>
#include<stdlib.h>
void transformCopy(int *point);
void transformTrue(int *&point);
int main(){
int *a,*b,i=0;
transformTrue(a);
transformCopy(b);
for(i=0;i<5;i++) {a[i]=i;}
for(i=0;i<5;i++){printf("%d ",a[i]);}
printf("\n");
for(i=0;i<5;i++) {b[i]=i;}
for(i=0;i<5;i++){printf("%d ",b[i]);}
printf("\n");
return 0;
}
void transformCopy(int *point){
point=(int*)malloc(5*sizeof(int));
}
void transformTrue(int *&point){
point=(int*)malloc(5*sizeof(int));
}
*&point expected '('.
References do not exist in C ( void transformTrue(int *&point) ), this is C++ code, not C
If you want to have the equivalent in C you have to use void transformTrue(int **point) and you have to call transformTrue(&a);
If I change your code to do in C what it is done in C++ (see comments) :
#include<stdio.h>
#include<stdlib.h>
void transformCopy(int *point);
void transformTrue(int ** point); /* ** rather than *& */
int main(){
int *a,*b = 0,i=0;
transformTrue(&a); /* &a rather than just a */
transformCopy(b);
for(i=0;i<5;i++) {a[i]=i;}
for(i=0;i<5;i++){printf("%d ",a[i]);}
printf("\n");
for(i=0;i<5;i++) {b[i]=i;}
for(i=0;i<5;i++){printf("%d ",b[i]);}
printf("\n");
return 0;
}
void transformCopy(int *point){
point=(int*)malloc(5*sizeof(int));
}
void transformTrue(int ** point){ /* ** rather than *& */
*point=(int*)malloc(5*sizeof(int)); /* *point = rather than point = */
}
transformTrue(&a) modifies the value of a, but transformCopy(b); does nothing except locally (and a memory leak) and back in main the value of b is still 0, the program will crash when you will try to write in invalid addresses
one possibility is to change transformCopy like that :
int * transformCopy(){
return (int*)malloc(5*sizeof(int));
}
and of course the call to have b = transformCopy();

Printing an int in C

I am trying to print an int a before and after calling a set function to set the value of a. I am doing this in C. When I compile it I have no errors but when I attempt to run it, I get a segmentation fault.
Here is what I have so far:
#include <stdio.h>
int main(){
int* a;
printf("%d",*a);
set(10);
printf("%d", *a);
return 0;
}
int set(int*a, int val){
*a = val;
return *a;
}
int main(){
int* a;
printf("%d",*a);
What you have there is a pointer to an int rather than an actual int.
And, while that's the correct way to print the int it points to, unfortunately it points to an arbitrary memory location which is why you're crashing.
You are not allowed to dereference arbitrary pointers, they have to point to something valid, such as if you begin your code with:
int main(){
int target_of_a = 42;
int *a = &target_of_a;
printf ("%d", *a);
In addition, you probably should be calling set with something like:
set (a, 10);
something the compiler would generally warn you about though, in this case, it would probably just say it didn't know about set at the time you called it. If it had known, it could have told you about the parameter mismatch.
One way for you to acheive that is to ensure you have a prototype defined for the function before you call it:
int set(int*,int);
or just move the function to before main. With all those changes (and a bit of a general tidy up), you'd end up with:
#include <stdio.h>
int set (int *a, int val) {
*a = val;
return *a;
}
int main (void) {
int target_of_a = 42;
int *a = &target_of_a;
printf ("%d\n", *a);
set (a, 10);
printf ("%d\n", *a);
return 0;
}
The wisdom of returning the variable you're changing is also debatable but there are situations where that might be useful (such as if you want to us it immediately without another statement: printf ("%d\n", set (a, 10)); for example) so I've left that as is.
I should also mention that it's a little unusual to artificially create a pointer variable in a situation like this.
Now it may be that your code is just a simplification of some more complex scenario where you already have a pointer but, if not, the usual way to do this would be to just have the int itself and just use & to create one on the fly:
#include <stdio.h>
int set (int *a, int val) {
*a = val;
return *a;
}
int main (void) {
int a = 42;
printf ("%d\n", a);
set (&a, 10);
printf ("%d\n", a);
return 0;
}
This code should work:
#include <stdio.h>
#define FIRST_VALUE 20
#define SECOND_VALUE 10
int main(){
int a = FIRST_VALUE; /* Declare a as an int variable. */
printf("Before set: a = %d\n",a); /* Print the first value. */
set(&a, SECOND_VALUE); /* Pass the ADDRESS of a to set. */
printf("After set: a = %d\n", a); /* Print the new value of a. */
return 0;
}
int set(int*a, int val){
*a = val;
return *a;
}
Note that the variable a in main() is not the same as the variable a in set(); you have to pass a pointer to a to set() in order for set() to operate on it.
Try this:
And remember, all functions before the main() (if you're using only one file)
Take a read on value and reference params.
#include <stdio.h>
int set(int* a, int val){
*a = val;
}
int main(){
int a = 2;
printf("%d\n", a);
set(&a, 10);
printf("%d\n", a);
return 0;
}

C - function pointer types with named parameters

On MSVC and gcc (GCC) 4.8.3 20140911 the following compiles and runs just fine:
#include <stdio.h>
int func(int a, int b){
return 0;
}
int main(void){
int (*funcPointer)(int a, int b);
funcPointer = func;
printf("funcPointer = %p\n", funcPointer);
return 0;
}
Is such behaviour well-defined, or is it non-standard and it's actually illegal for function pointer types to have named parameters (i.e. names as well as types in their parameter list)?
You can have a parameter in your function pointer. It is totally valid. The parameter list matches that of the function being called and the names is just optional.
It can also be written as
int (*funcPointer)(int,int);
I mean
int (*funcPointer)(int a, int b);
This is valid and you can verify the same by calling
int res = funcPointer(3,4);
and returning
int func(int a, int b){
return a+b;
}
It's perfectly legal. The names a and b in funcPointer are not used for anything, but they are permitted. You could use any (legal) names you want, they don't matter at all.
A question in the answer:
Why does this compile and what does it mean?
int func(int a) { return a; }
int main(int argc, char **argv)
{
int(*a)(int x(float)) = func;
printf("%d\n", a(1));
return 0;
}

Resources