Troubleshooting C Code: Segmentation Fault in GCC on Ubuntu

In summary, the conversation discusses the issue of a segmentation fault that occurs at runtime when trying to set the contents of a pointer that has not been initialized. The expert explains that this is due to accessing memory in the wrong way and provides a detailed explanation of how pointers work. The conversation also delves into the topic of passing pointers by value versus by reference, with the expert providing a solution to modify the actual pointer itself by passing a pointer to the pointer of the function.
  • #1
matiasmorant
39
0
the following code

#include<stdio.h>
#include<stdlib.h>

int
main (int argc, char *argv[])
{
int* a;
*a=1;
return 0;
}

compiles ok, but throws "Segmentation fault (core dumped)" at runtime
why is that?

i'm working with gcc in ubuntu. I have been able to compile and run many c programs without any difficulties
 
Technology news on Phys.org
  • #2
matiasmorant said:
the following code

#include<stdio.h>
#include<stdlib.h>

int
main (int argc, char *argv[])
{
int* a;
*a=1;
return 0;
}

compiles ok, but throws "Segmentation fault (core dumped)" at runtime
why is that?

i'm working with gcc in ubuntu. I have been able to compile and run many c programs without any difficulties

Hey matiasmorant and welcome to the forums.

The simple reason is that a is a pointer that has not been initialized and you are trying to set the contents of the memory of that location to something.

Because of this, you are trying to access memory in the wrong way and it crashes.

Before you work with pointers, you must realize that what you reference must be declared either on the stack or on the heap. Heap elements are created with malloc and other similar statements and are freed using free in C (in C++ it's new and delete).

Things declared on the stack are things like say 'int x = 0;' or 'char r[128];'

If you want to set say x in the above example you can create a pointer that points to this by saying int* xpointer = &x; *xpointer = 3; which will set the value of x to 3. If you want to use the heap then do int* xpointer = malloc(4); (Check to make sure xpointer is not 0 before doing the next part!). *xpointer = 3. This will set a heap variable. (Make sure you call free(xpointer); when you are finished).

Just out of curiosity, how long have you been learning? Is this your first time?
 
  • #3
thanks a lot! it's great to receive such a quick response

I started learning programming about 10 months ago, I already finished the programming course, and I did plenty of programs with pointers, pointers to pointers, pointers to functions, etc. Then I started working with java and forgot some of the c language. Now, I was trying to learn openGL with java, but couldn't make jogl work, so i came back to c, where it works perfect up to now. That's my programming biography ;)

now, what about the following code? (i hope this will be the last question)

#include<stdio.h>
#include<stdlib.h>

void SWAP(double *f1, double *f2){
double *temp;
temp=f1;
f1=f2;
f2=temp;
printf("inside %f %f\n",*f1,*f2);
}

int
main (int argc, char *argv[])
{
double *f1, *f2;

f1=(double*)malloc(sizeof(double));
f2=(double*)malloc(sizeof(double));
*f1=1;
*f2=2;
printf("before %f %f\n",*f1,*f2);
SWAP(f1,f2);
printf("after %f %f\n",*f1,*f2);

return 0;
}

i expected the answer to be

before 1.000000 2.000000
inside 2.000000 1.000000
after 2.000000 1.000000

but it was

before 1.000000 2.000000
inside 2.000000 1.000000
after 1.000000 2.000000

why doesn't the swap function... swap?

(i need to swap the references and not the contents, since i will be working with matrices)

thanks in advance!
 
  • #4
matiasmorant said:
thanks a lot! it's great to receive such a quick response

I started learning programming about 10 months ago, I already finished the programming course, and I did plenty of programs with pointers, pointers to pointers, pointers to functions, etc. Then I started working with java and forgot some of the c language. Now, I was trying to learn openGL with java, but couldn't make jogl work, so i came back to c, where it works perfect up to now. That's my programming biography ;)

now, what about the following code? (i hope this will be the last question)

#include<stdio.h>
#include<stdlib.h>

void SWAP(double *f1, double *f2){
double *temp;
temp=f1;
f1=f2;
f2=temp;
printf("inside %f %f\n",*f1,*f2);
}

int
main (int argc, char *argv[])
{
double *f1, *f2;

f1=(double*)malloc(sizeof(double));
f2=(double*)malloc(sizeof(double));
*f1=1;
*f2=2;
printf("before %f %f\n",*f1,*f2);
SWAP(f1,f2);
printf("after %f %f\n",*f1,*f2);

return 0;
}

i expected the answer to be

before 1.000000 2.000000
inside 2.000000 1.000000
after 2.000000 1.000000

but it was

before 1.000000 2.000000
inside 2.000000 1.000000
after 1.000000 2.000000

why doesn't the swap function... swap?

(i need to swap the references and not the contents, since i will be working with matrices)

thanks in advance!

The reason for this is because you are passing the pointers by value and not by reference.

Passing by value means that you give the values to the function more or less a kind of copy, whereas passing the values by reference passes the actual pointer to the object and allows you to modify either the actual corresponding pointer or object.

What you are actually doing is passing a copy of the pointer and just setting stuff inside the routine that has no effect on anything outside the scope of the function. This is why when you print the two variables, you get the swap inside the function but not outside.

What you need to do is pass a pointer to the pointer of the function and modify the pointer itself to change by reference *pointer where you pass double** pointertopointer. This will modify the actual pointer itself even outside the function and everything will act as expected.
 
  • #5
You would need the code to look like this:

Code:
void SWAP(double **f1, double **f2){
    double *temp;
    temp=*f1;
    *f1=*f2;
    *f2=temp;
    printf("inside %f %f\n",**f1,**f2);
}

int main (int argc, char *argv[])
{
/* ... */
    SWAP(&f1, &f2);
/* ... */
}
 
Last edited:
  • #6
thanks friend! that was the last question
 

1. What is an error in C code?

An error in C code refers to a mistake or bug in a program written in the C programming language. This can lead to unexpected or incorrect results when the program is run.

2. What are the common types of errors in C code?

Some common types of errors in C code include syntax errors, logic errors, and runtime errors. Syntax errors occur when the code violates the rules of the programming language. Logic errors happen when the code does not produce the intended result. Runtime errors occur during the execution of the program and may cause the program to crash or produce unexpected results.

3. How can errors in C code be detected?

Errors in C code can be detected through the use of debugging tools, such as a debugger or compiler, which can identify and pinpoint the location of the error in the code. Manual code review and testing can also help to detect errors.

4. How can errors in C code be prevented?

To prevent errors in C code, it is important to follow coding standards and best practices, such as using meaningful variable names and properly commenting the code. Regular testing and debugging can also help to catch and fix errors before they become bigger issues.

5. What should be done when encountering an error in C code?

When encountering an error in C code, the first step is to carefully read any error messages or warnings that appear. This can provide information about the type and location of the error. Then, the code can be debugged using a debugger or by tracing the code to identify the source of the error. Once the error is identified, it can be fixed by making necessary changes to the code.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
7
Views
870
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
926
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
6
Views
868
  • Programming and Computer Science
Replies
30
Views
2K
Back
Top