Segmentation fault (core dumped) in c program - c

i am using gcc compiler on ubuntu 14.04 LTS for compilation of following c program
#include<stdio.h>
void main()
{
int *a,*b;
*a=2;
*b=3;
printf("\n printing address.....\n address of a = %d \n address of b = %d \n",a,b);
printf("\n\n printing values ..... \n value of a = %d \n value of b = %d \n",*a,*b);
}
and when i run above program than i get following in output
output: Segmentation fault (core dumped)
please suggest where i am doing wrong.
thanks

You're declaring and using pointers (pointed-to memory), without allocating space for them.
Just declaring:
int *a;
doesn't give you memory to use, this just declares a variable that can reference memory.
The pointer, once declared, is un-initialized, and will point to some part of memory that doesn't belong to you. Using that memory - in your case, putting a value there - will result in undefined behavior; you see a core dump when you touch that memory.
In order to get some space to use, learn about malloc:
int *a = NULL; // good practive to initialize/reset pointers to NULL
// malloc will give you space for 1 int, and a will point to that new space
a = malloc(sizeof(int));
if (a != NULL) // malloc returns NULL in the event of a failure
{
// a is non-NULL so now we can use the memory pointed-to:
*a = 5;
// other code that uses a goes here:
...
// and when you're finished with a give the memory back:
free(a);
a = NULL;
}

When you declare a pointer
int* p;
it is similar as declaring an integer variable:
int v;
the contents of v is uninitialized - same with p - it is also uninitialized so when you use p e.g. *p you may be dereferencing an address anywhere in memory, even in read-only memory. Instead you need to initialize the variables.
int v = 0;
int* p = &v; // p points to v in memory

Related

pointer reassignment gives segmentation fault

I'm having an issue understanding why I can't make the re-assignment for lastPrefix, on the line specified it gives me a segment fault. I appear to be missing something fundamental.
char * somefunc(char ** strs, int numStrings)
{
char * lastPrefix;
printf("%d\n", *(strs[0]+0));
printf("%d\n", *(strs[1]+0));
printf("%d\n", *(strs[2]+0));
printf("%d\n", *(strs[0]+1));
printf("%d\n", *(strs[1]+1));
printf("%d\n", *(strs[2]+1));
printf("%d\n", *(strs[0]+2));
printf("%d\n", *(strs[1]+2));
printf("%d\n", *(strs[2]+2));
printf("%d\n", *(strs[0]+0));
printf("%d\n", *(strs[1]+0));
printf("%d\n", *(strs[2]+0)); // ALL IS WELL
*lastPrefix = *(strs[1]+0);
*lastPrefix = *(strs[2]+0);
*lastPrefix = *(strs[0]+1); // WILL SEGMENT FAULT HERE
*lastPrefix = *(strs[1]+1);
*lastPrefix = *(strs[2]+1);
}
int main()
{
char * strs[] = {
"flower", "flow", "flight"
};
char * res = somefunc(strs, SIZEOF(strs));
}
Could I also ask for a good reference for C pointers?
When you create any variable, whether it's pointer or data, the initial value of the variable will be some garbage or 0. In your case,
char *lastPrefix;
Is such a variable, which contains some garbage value. You have to give it a valid value using malloc() or something like that. Or else, when you de reference it(*), it might points to some inaccessible or even nonexistent memory location. Maybe it points to the middle of your code. Maybe it points to NULL. Memories like that is inaccessible in userspace. So, windows(or linux) kills your program.
What you want to do:
char *lastPrefix;
lastPrefix=malloc(101); //holds strings upto 100 characters
The malloc() will allocate value into your variable from heap and the program should work okay.
Just remember to free the memory at the end: free(lastPrefix);
int *ptr; // pointer variable declaration */
int kk; // actual variable declaration */
*a = 11; // `a` pointing to unknown memory address and accessing this will give segmentation fault/
a = &kk; *a = 11 // This is valid. store address of `kk` in pointer variable */
Similarily, in your code lastPrefix points to an unknown address, accessing it will give you segmentation fault.

Segmentation fault after realloc in function

I have created this code to test one error, that I get in my main code, and it shares the same problem. I'm always getting either segmentation fault or corrupted data (zeros or strange numbers).
Here is the code:
int *p=NULL;
int func (int **point);
int main() {
int num = 5647;
p = malloc(sizeof(int)*2);
p[0] = num;
p[1]= 657;
printf("%d\n", p[0]);
printf("%d\n", p[1]);
func(&p);
printf("%d\n", p[0]);
printf("%d\n", p[1]);
printf("%d\n", p[2]);
printf("%d\n", p[3]);
return 0;
}
int func (int **point){
*point = realloc(*point,sizeof(int)*4);
if (*point==NULL){
printf("\n abort \n");
exit(0);
}
*point[0] = 867;
*point[1]= 777;
*point[2] = 67;
*point[3]= 77;
}
I'm getting the segmentation fault on the *point[1]=777;. If I'm trying to do like point[1]=777; I'm getting wrong data. With any changes in int func (int **point); or func(&p); I'm getting segmentation fault on realloc.
Please advise, I have read information about double pointers and tried to follow all solutions I found, but every time I'm getting this error.
Your problem is operator precedence, change *point[0] to (*point)[0] and so forth.
What you have right now is *(point[0]). You treat a pointer to a single element as a pointer to multiple consecutive elements and then dereference some indeterminate value as an address. This results in undefined behavior, which luckily for you manifests as a crash.
After the change you first dereference point and then use that address to index into consecutive elements you allocated.
Two suggestions for improvement:
Don't assign the result of realloc directly to *point. If the call fails, then you leak the original memory. Assign it to a temporary first for verification.
Also, try not to repeat types. Instead of sizeof(int) try for sizeof(**point), i.e whatever the output buffer is supposed to point at. That way you won't have silent errors in your code if you change the type from int to something else.
void *point_check = realloc(*point,sizeof(**point)*4);
if (point_check == NULL){
printf("\n abort \n");
exit(0); // If this ever returns instead of exit, `*point` will not leak
}
*point = point_check;

How does int *p = 10; work?

I have a statement int *p = 10; . This statement executes perfectly fine on any compiler. I also know that 10 is put in read-only memory. Is there a way i can access this memory. How can I print this on console? The statement printf("%d",*p) crashes. How can I make it print on console.
Edit
int main()
{
int *p = 10;
}
the above code compiles fine and runs fine.
int main()
{
int *p = 10;
printf("\n%d",*p); //crashes
}
the above code gives segmentation fault. I wanted to know more explanation on this?
By typing int *p = 10; you say to compiler:
Lets have a pointer on integer, called "p".
Set p to 10 => p points on address 10 on memory.
By typing printf("%d",*p); you say to compiler:
Show me -as a integer- what is at the address 10 on memory.
The code
int *p = 10;
Is equivalent to:
int *p;
p = 10;
Is not equivalent to:
int *p;
*p = 10;
Corrected code could be:
// define an integer
int i;
// define a pointer on the integer
int *p = &i;
// set integer to 10, through pointer
*p = 10;
// display integer through pointer
printf("%d",*p);
I think my answer is great for you,you may misunderstand the definition of the pointer and use the pointer in a wrong way. Let`s analyse your code first:
int *p = 10
this statement defines a pointer which is pointed to address 10 in memory, it compiles OK because there's no syntax error--the content of p is the address 10, but you have no idea what's value in address 10, and it generally doesn't have a internal memory to store a value cuz you haven`t allocate the memory for it.it's also very dangerous that you are tring to use a system memory 10.
You can print the address which has been pointed by p:
printf("%d\n", p);//p is pointed to address 10
So when you tried to print the content of p by:
printf("\n%d",*p);
which haven't allocate the memory to store the content,segmentation fault occurs!
If u want to assign value for pointer directly u must dynamic application of memory space for it first, you should write in this way:
int *p = NULL;//the right and safe habit to define a pointer
p = (int *)malloc (sizeof(int));//dynamic application of memory space of pointer p to store value
if (NULL == p)
{
printf("malloc failed!\n");//show error
exit(1);//exit
}
*p = 10;//now you have memory space to store value 10
printf("%d\n", *p);
free(p);//release the memory to avoid memory leaks
p = NULL;//the right and safe habit
You can also write in this way:
int transfer_value = 10;//integer has memory when you declare it
int *p = &transfer_value;//p stores the address of i which content is value 10
printf("%d\n", *p);//because variable i has memory which size is sizeof(int), you can print *p(it stands for the value of i)directly.
Hope my explanation could help you ^_^
Segmentation fault is a error when there is a memory access violation. Dynamic memory allocation will solve your problem. Kernel will decide which memory address should be used to store the value.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p = malloc(sizeof(int)); //Kernel will assign a memory from Heap segment
*p=10;
printf("\n%d",*p);
free(p);
}
You have created a pointer, not a value. You probable want just int i=10. Otherwise, the program is accessing random memory at address 10 and will crash.
When you assign the value 10 to your pointer, you actually assign the address 10 to it. When you initialize you don't get any error, but when you try to print it you have to access it, and you don't have access to this memory address (that's why you have a segmentation fault).
If you do this :
int i = 10;
int *ptr = &i;
printf("%d\n", *ptr);
It will work because you make the pointer points to the variable i which contains the value 10.
int *p = 10;
creates a pointer p and sets it to point to the memory address 10, which is most likely not an accessible address on your platform, hence the crash in the printf statement.
A valid pointer is obtained by using the unary & operator on another object, such as
int i = 10;
int *p = &i;
or by calling a function that returns a pointer value such as malloc or fopen. Some platforms may expose specific addresses for particular operations, but those are usually well-documented and not such low address values.

glibc detected free() invalid pointer

I'm having a bit of trouble with some dynamic memory allocation.
Below is just a test code that I've been playing around with to try and fix the problem (it's the same problem in my current project's code, this is just a simpler way to show it).
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
int main(){
int x = 5;
int *ptr = (int*) malloc(sizeof(int));
assert(ptr != NULL);
ptr = &x;
printf("x = %d\n",x);
*ptr = 3;
printf("x = %d\n",x);
free(ptr);
return 0;
}
The program compiles fine and when run I get the correct outputs printed "x = 5 x = 3"
But then I get the error:
glibc detected ./dnam: free(): invalid pointer: 0xbfccf698
dnam is the name of the test program.
From what I've read about the error, it's supposedly caused by freeing memory that you haven't malloc/calloc/realloc'd.
This error message is followed by a backtrace and a memory map. AT the end of the memory map I'm told the program has aborted (core dumped).
int *ptr = (int*) malloc(sizeof(int));
ptr = &x;
You are changing ptr value! The compiler will be taking unlimited revenge if you attempt to free it.
Here:
free(ptr);
You are free-ing an object not allocated through malloc.
You are allocating memory and saving its address into ptr. Then, you make ptr point to x's address, thus when you run free(ptr) you essentially freeing &x, which doesn't work.
tl;dr: there's no need for malloc and free when you're assigning the pointer a pointer to another var.
if you want use this code.I think you should use a temp pointer save the init ptr.at last free(temp).like this:
int *temp = ptr;
free(temp);

integer pointer is not working while assigning two dimensional array via another pointer?

I was trying to copy the contents of one 2d array to another using pointers. I wrote this simple test program but it shows me segmentation fault but i still cannot find a rock solid reason why?
#include <stdio.h>
void main(){
int m[2][3]={
{2,3,4},{5,6,7}
};
int *p=m;
int *n;
int i,j;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d \t", *(p+3*i+j));
printf("Debug here\n");
*(n+3*i+j)=*(p+3*i+j);
}
printf("\n");
}
}
// Output:
// 2 Debug here
// Segmentation fault (core dumped)
int *n is an unitialized pointer, you never allocated memory for it, therefore you are trying to write your copy onto unknown territory.
You can alloc space to n using this:
int *n = (int *)malloc(2 * 3 * sizeof(int));
You are not allocating space for your target array. At minimum, you should do:
int *n = malloc(2 * 3 * sizeof(int));
or
int n[2][3];
A pointer is a variable that points to a memory location. It stores the address of the memory location. You can access that location by dereferencing the pointer using *.
In your case,
int *n;
This only declares a pointer to an int. This can point to a single integer or an array integers. You have not yet assigned any value to it (It is still not assigned a memory location). We don't know where it is pointing right now (You might not be allowed to access the memory etc.). (That's why the seg fault)
You need to allocate memory as such,
int *n = malloc(2 * 3 * sizeof(int));
The advantage of a pointer to a dynamic array than a static array
int n[2][3];
is that the pointer can be re-used (Of course you need to take care of freeing the existing memory before resuing the pointer [If you dont have any other access paths to the memory])
reason why?
The destination area is not ensured
#include <stdlib.h>
int *p=&m[0][0];
int *n = (int*)malloc(sizeof(m));

Resources