segmentation fault

P

priyanka

Hi,

I am getting segmentation fault in a very simple thing. But I am not
able to figure out waht is wrong. I should be missing something.

char *kernel_rget_pt;
char kernel_rec_buf[5] = {'t', 'e', 'f', 'g', '\n'};
kernel_rget_pt = &kernel_rec_buf[0];

int GetFifo(char *datapt){
printf("Inside GetFifo\n");
printf("address of datapt: 0x%x\n",datapt);
printf("1st position and data in kernel_rec_buf: 0x%x,
%c\n",&kernel_rec_buf[0],kernel_rec_buf[0]);
printf("1st position and data in kernel_rec_buf: 0x%x,
%c\n",kernel_rget_pt,*kernel_rget_pt);
kernel_rget_pt++;
printf("2nd position and data in kernel_rec_buf: 0x%x,
%c\n",&kernel_rec_buf[1],kernel_rec_buf[1]);
printf("2nd position and data in kernel_rec_buf: 0x%x,
%c\n",kernel_rget_pt,*kernel_rget_pt);
//*datapt = *(kernel_rget_pt++);
printf("fdfdf\n");
*(datapt) = *kernel_rget_pt; // segementation fault here
printf("*datapt:%c\n",*datapt);//never gets executed
if(kernel_rget_pt == &kernel_rec_buf[4]) kernel_rget_pt =
&kernel_rec_buf[0];
return -1;
}

I call this function from the main as GetFifo(datapt) where datapt is a
char pointer defined in main. I am not sure why I am getting
segmentation fault at *(datapt) = *kernel_rget_pt; I am not able to
copy even one character.
Can anyone point out where I am wrong ?

Thanks,
Priya
 
A

Ancient_Hacker

priyanka said:
I call this function from the main as GetFifo(datapt) where datapt is a
char pointer defined in main. I am not sure why I am getting
segmentation fault at *(datapt) = *kernel_rget_pt; I am not able to
copy even one character.


My oija board spells out: "You have not initialized datapt to point to
anything"
 
C

CBFalconer

priyanka said:
I am getting segmentation fault in a very simple thing. But I am not
able to figure out waht is wrong. I should be missing something.

char *kernel_rget_pt;
char kernel_rec_buf[5] = {'t', 'e', 'f', 'g', '\n'};
kernel_rget_pt = &kernel_rec_buf[0];

int GetFifo(char *datapt){
printf("Inside GetFifo\n");
printf("address of datapt: 0x%x\n",datapt);

The first thing that strikes is that the above printf is WRONG.
You print pointers with a %p specifier. If using gcc, turn up the
warning levels. I recommend at least:

-W -wall -ansi -pedantic

Don't lie to the compiler. It bites.
 
O

Old Wolf

priyanka said:
Hi,

I am getting segmentation fault in a very simple thing. But I am not
able to figure out waht is wrong. I should be missing something.

char *kernel_rget_pt;
char kernel_rec_buf[5] = {'t', 'e', 'f', 'g', '\n'};
kernel_rget_pt = &kernel_rec_buf[0];

Assignment statements are not allowed outside of a function body.
int GetFifo(char *datapt){
printf("Inside GetFifo\n");
printf("address of datapt: 0x%x\n",datapt);
printf("1st position and data in kernel_rec_buf: 0x%x,
%c\n",&kernel_rec_buf[0],kernel_rec_buf[0]);

%x is only for printing unsigned ints, but you pass it a pointer.
*(datapt) = *kernel_rget_pt; // segementation fault here

Probably 'datapt' does not point to something that you are
allowed to modify.

If you still have errors, then post a complete program that
compiles.
 
K

Keith Thompson

CBFalconer said:
The first thing that strikes is that the above printf is WRONG.
You print pointers with a %p specifier.

More precisely, you print pointers of type void* with a %p specifier.
Other pointer types can be printed by explicitly converting them to
void*:

printf("ptr = %p\n", (void*)ptr);
If using gcc, turn up the warning levels. I recommend at least:

-W -wall -ansi -pedantic

That's "-Wall" (case is significant).

<OT>
gcc's "-W" is called "-Wextra" in newer gcc releases. You should pay
attention to any warnings produced with this setting, but not all of
them necessarily indicate real problems. See the gcc documentation
for details.
</OT>
 
P

Peter Nilsson

Keith said:
More precisely, you print pointers of type void* with a %p specifier.
Other pointer types can be printed by explicitly converting them to
void*:

printf("ptr = %p\n", (void*)ptr);

It is the committee's intent that void * and char * are interchangable
as arguments to functions. So the cast is not deemed necessary in
the case of the correction to the original code...

printf("address of datapt: %p\n", datapt);

In the same way, corresponding signed and unsigned integer types
are also interchangable so long as the value being passed is within
range of both types...

printf("%u\n", 42); /* int passed to %u which takes unsigned int */

This is not just a facet of variadic functions, it is also a facet of
unprototyped non-variadic function calls...

#include <stdio.h>
int foo();
int main(void) { foo(42); return 0; } /* signed argument */
int foo(unsigned x) { printf("%u\n", x); } /* unsigned parameter */

Of course if you replace 42 with 42L or 42.0 you have undefined
behaviour. [The committee stopped short of saying that 42L must
work on an implementation where int and long have the same
representation.]
 
K

Keith Thompson

Peter Nilsson said:
It is the committee's intent that void * and char * are interchangable
as arguments to functions. So the cast is not deemed necessary in
the case of the correction to the original code...

printf("address of datapt: %p\n", datapt);

I hadn't noticed that the pointer was of type char*.

In any case, in my opinion it's poor style to depend on void* and
char* being interchangeable. I prefer to apply the explicit
conversion to void* for any pointer type other than void*.

(And I forgot to mention that this works only for object pointers, not
for function pointers -- though it may work as an extension on some
implementations.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top