Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > segmentation fault

Reply
Thread Tools

segmentation fault

 
 
priyanka
Guest
Posts: n/a
 
      10-19-2006
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

 
Reply With Quote
 
 
 
 
Ancient_Hacker
Guest
Posts: n/a
 
      10-19-2006

priyanka wrote:

> 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"

 
Reply With Quote
 
 
 
 
priyanka
Guest
Posts: n/a
 
      10-19-2006
Thank you so much.

On Oct 19, 8:29 am, "Ancient_Hacker" <g...@comcast.net> wrote:
> priyanka wrote:
> > 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"


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-19-2006
priyanka wrote:
>
> 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      10-20-2006
priyanka wrote:
> 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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-20-2006
CBFalconer <> writes:
[...]
> 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>

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      10-20-2006
Keith Thompson wrote:
> CBFalconer <> writes:
> > > 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.

>
> 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.]

--
Peter

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-20-2006
"Peter Nilsson" <> writes:
> Keith Thompson wrote:
>> CBFalconer <> writes:
>> > > 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.

>>
>> 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);


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.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-20-2006
CBFalconer <> wrote:

> You print pointers with a %p specifier.


After casting them to void* (for OP).

> -W -wall -ansi -pedantic

^

That's "-Wall" - perhaps there should be a lint for comp.lang.c posts


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


Particularly when the C is Unleashed

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments