c - Segmentation fault on 11.4 - need help figuring out (strtod?)

classic Classic list List threaded Threaded
34 messages Options
12
Reply | Threaded
Open this post in threaded view
|

c - Segmentation fault on 11.4 - need help figuring out (strtod?)

David C. Rankin
Guys,

   Helping my son with a simple little area program, I am running into a
segmentation fault after program execution. It seems to be with the strtod
conversion, but I can't figure out why. It compiles without issue and executes
fine, but then I get a Segmentation Fault error after the program ends. The code is:

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

int main (int argc, char *argv[]) {

   if (argc < 2) {
     fprintf(stderr, "\nERROR:  Usage: %s radius list\n", argv[0]);
     fprintf(stderr, "\n      example: %s 4 16 21 8  for area calculations\n\n",
argv[0]);
     exit (1);
   }

   char *p;
   int i;
   double rad;
   double area;

   for (i=1;i<=argc;i++) {
     rad = strtod(argv[i],&p);
     area = M_PI*rad*rad;
     printf("  rad : %5.2f      area :  %10.4f\n", rad, area);
   }

   return 0;
}

   It compiles without error on 11.4 (x86_64) with:

gcc -Wall ztst.c -o ztst

   It seems to run fine:

13:22 alchemy:~/dev/prg/ccpp/src-c/misc> ./ztst 1 2 3 4
   rad :  1.00      area :      3.1416
   rad :  2.00      area :     12.5664
   rad :  3.00      area :     28.2743
   rad :  4.00      area :     50.2655
Segmentation fault

   So where is this segfault coming from?  I have experimented and commented out
of the strtod function and just hard coded values for rad (i.e.: rad = (double)
i;) and it runs without a segfault. Am I doing something wrong with the strtod
call?  What say the experts?

--
David C. Rankin, J.D.,P.E.
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Jim Henderson-4
On Wed, 02 Nov 2011 13:33:27 -0500, David C. Rankin wrote:

> So where is this segfault coming from?

I'd compile with debug info and load it up in gdb to see what's
happening.  My instinct is that the issue has to do with how you're
addressing p - you may well be using an incorrect redirection.

If memory serves, when you declare a pointer:

char *p;

When you want to reference that address, you just reference it as 'p',
not as '&p' - the latter is the address of the address of p, not the
address of p.

But it's been a while since I did anything with pointers in C.

Jim

--
 Jim Henderson
 Please keep on-topic replies on the list so everyone benefits

--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

John Andersen-2
In reply to this post by David C. Rankin
On 11/2/2011 11:33 AM, David C. Rankin wrote:
> Guys,
>
> Helping my son with a simple little area program, I am running into a segmentation fault after program execution.

David:

I have a little mail pop-up running for this list.
It shows the subject of the new message.

When "Segmentation fault on 11.4" appeared I nearly fell off the chair.
I'll have a portable defibrillator installed in my office tomorrow.





--
_____________________________________
---This space for rent---
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Cristian Rodríguez-2
In reply to this post by David C. Rankin
On 02/11/11 15:33, David C. Rankin wrote:

>   So where is this segfault coming from?

gdb --args ./a.out 1 2 3 4

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a88b03 in ____strtod_l_internal () from /lib64/libc.so.6
(gdb) bt full
#0  0x00007ffff7a88b03 in ____strtod_l_internal () from /lib64/libc.so.6
No symbol table info available.
#1  0x00000000004006d1 in main (argc=5, argv=0x7fffffffda78) at dcr.c:19
        p = 0x7fffffffdf72 ""
        i = 5
        rad = 4
        area = 50.26548245743669
(gdb) frame 1
#1  0x00000000004006d1 in main (argc=5, argv=0x7fffffffda78) at dcr.c:19
19          rad = strtod(argv[i],&p);
(gdb) p argv[i]
$1 = 0x0 --> that's why ! argv[argc] is a null pointer!

bug is

for (i=1;i<= argc;i++) {
          ^^
should be < argc







--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Cristian Rodríguez-2
On 02/11/11 16:11, Cristian Rodríguez wrote:
> On 02/11/11 15:33, David C. Rankin wrote:
>
>>   So where is this segfault coming from?
>

It would be easy to spot at compile time if GCC did something meaningful
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17308 , but as we all know,
it is made to make the programmer's life miserable.

it should tell you:

warning: null argument where non-null required (argument 1)
because the prototypes of strtod() clearly says that argument 1 should
be non-null...




--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Martin Helm
In reply to this post by David C. Rankin
Am Mittwoch, 2. November 2011, 13:33:27 schrieb David C. Rankin:
> i<=argc

should be i < argc
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

David C. Rankin
In reply to this post by Cristian Rodríguez-2
On 11/02/2011 02:11 PM, Cristian Rodríguez wrote:

> On 02/11/11 15:33, David C. Rankin wrote:
>
>>    So where is this segfault coming from?
>
> gdb --args ./a.out 1 2 3 4
>
> Program received signal SIGSEGV, Segmentation fault.
> 0x00007ffff7a88b03 in ____strtod_l_internal () from /lib64/libc.so.6
> (gdb) bt full
> #0  0x00007ffff7a88b03 in ____strtod_l_internal () from /lib64/libc.so.6
> No symbol table info available.
> #1  0x00000000004006d1 in main (argc=5, argv=0x7fffffffda78) at dcr.c:19
>          p = 0x7fffffffdf72 ""
>          i = 5
>          rad = 4
>          area = 50.26548245743669
> (gdb) frame 1
> #1  0x00000000004006d1 in main (argc=5, argv=0x7fffffffda78) at dcr.c:19
> 19          rad = strtod(argv[i],&p);
> (gdb) p argv[i]
> $1 = 0x0 -->  that's why ! argv[argc] is a null pointer!
>
> bug is
>
> for (i=1;i<= argc;i++) {
>  ^^
> should be<  argc
>

Thank you Christian!  I would have looked at this for days and not caught the
'<=' issue. Aye, yie, yie, what possessed me to do that is beyond me, but I can
assure you that once I did it, it would have taken an act of congress to make me
see the error in my ways!  Thanks again!

18:46 alchemy:~/dev/prg/ccpp/src-c/misc> gcc -Wall ztst.c -o ztst
18:46 alchemy:~/dev/prg/ccpp/src-c/misc> ./ztst 2 4 6 8 10
   rad :  2.00      area :     12.5664
   rad :  4.00      area :     50.2655
   rad :  6.00      area :    113.0973
   rad :  8.00      area :    201.0619
   rad : 10.00      area :    314.1593


--
David C. Rankin, J.D.,P.E.
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Anton Aylward-2
The real issue here is that C uses zero-base-index arrays

Yes there were 5 arguments in the example

argv[0] is the name of the program
and argv[1..4] were the parameters.

That is why you count from 1 .. argc-1 as opposed to 0 .. argc.


The zero-base-index arrays of C have caught many people out.

--
Man's mind once stretched by a new idea, never regains its original
dimension.
   -- Oliver Wendell Holmes.
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Cristian Rodríguez-2
In reply to this post by David C. Rankin
On 02/11/11 20:49, David C. Rankin wrote:

> Thank you Christian!  I would have looked at this for days and not
> caught the '<=' issue.

Well deserved then :-D , unless you are doing this as a learning
experience or plan to roll such code in an intensive math computing
environment, you are picking the wrong language. python or ruby will do
the trick just fine.



--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Roger Oberholtzer
On Wed, 2011-11-02 at 21:34 -0300, Cristian Rodríguez wrote:
> On 02/11/11 20:49, David C. Rankin wrote:
>
> > Thank you Christian!  I would have looked at this for days and not
> > caught the '<=' issue.
>
> Well deserved then :-D , unless you are doing this as a learning
> experience or plan to roll such code in an intensive math computing
> environment, you are picking the wrong language. python or ruby will do
> the trick just fine.

C is never the wrong language. It is the tasks that are wrong :) ;)

Add Tcl to the list of alternatives.


Yours sincerely,

Roger Oberholtzer

OPQ Systems / Ramböll RST

Office: Int +46 10-615 60 20
Mobile: Int +46 70-815 1696
[hidden email]
________________________________________

Ramböll Sverige AB
Krukmakargatan 21
P.O. Box 17009
SE-104 62 Stockholm, Sweden
www.rambollrst.se


--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Anton Aylward-2
Roger Oberholtzer said the following on 11/03/2011 04:58 AM:

> On Wed, 2011-11-02 at 21:34 -0300, Cristian Rodríguez wrote:
>> On 02/11/11 20:49, David C. Rankin wrote:
>>
>>> Thank you Christian!  I would have looked at this for days and not
>>> caught the '<=' issue.
>>
>> Well deserved then :-D , unless you are doing this as a learning
>> experience or plan to roll such code in an intensive math computing
>> environment, you are picking the wrong language. python or ruby will do
>> the trick just fine.
>
> C is never the wrong language. It is the tasks that are wrong :) ;)
>
> Add Tcl to the list of alternatives.

And shell.
The bash shell is an incredibly powerful programming language and very
easy to debug.

It used to be, in Bourne-days, that large shell programs were unwieldy
and slow.  I found that I'd have to turn to perl to get performance.
Not now.  These days I see perl as being unwieldy for most of what I
have to deal with.

Unless Its GUI-fied (yes, Tcl wins!) then the more complex things need
Ruby.  It just seems to make complexity vanish.  Despite all that's said
the Object extensions to Perl (5) are a kludge.

Ocaml?  One day ...

--
Auditing security is complex, challenging, and not for the uninformed
   Avoiding IS Icebergs
   http://infosecuritymag.techtarget.com/articles/october00/features3.shtml
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Roger Oberholtzer
On Thu, 2011-11-03 at 07:24 -0400, Anton Aylward wrote:

> Ocaml?  One day ...

My next goal in this area is python + Qt. See if it displaces my use of
Tcl/Tk. Unless I get crazy and try TkQt, which loads Qt interfaces into
Tcl/Tk...

Too many languages. Too little time...



Yours sincerely,

Roger Oberholtzer

OPQ Systems / Ramböll RST

Office: Int +46 10-615 60 20
Mobile: Int +46 70-815 1696
[hidden email]
________________________________________

Ramböll Sverige AB
Krukmakargatan 21
P.O. Box 17009
SE-104 62 Stockholm, Sweden
www.rambollrst.se


--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

David C. Rankin
In reply to this post by Anton Aylward-2
On 11/03/2011 06:24 AM, Anton Aylward wrote:

> Roger Oberholtzer said the following on 11/03/2011 04:58 AM:
>> On Wed, 2011-11-02 at 21:34 -0300, Cristian Rodríguez wrote:
>>> On 02/11/11 20:49, David C. Rankin wrote:
>>>
>>>> Thank you Christian!  I would have looked at this for days and not
>>>> caught the '<=' issue.
>>>
>>> Well deserved then :-D , unless you are doing this as a learning
>>> experience or plan to roll such code in an intensive math computing
>>> environment, you are picking the wrong language. python or ruby will do
>>> the trick just fine.
>>
>> C is never the wrong language. It is the tasks that are wrong :) ;)
>>
>> Add Tcl to the list of alternatives.
>
> And shell.
> The bash shell is an incredibly powerful programming language and very
> easy to debug.
>
<snip>

   I actually did the little area calc in bash first :)

for i in 5 7 4 16 9 15 11 13 12; do \
   printf "r: %2s  area: %7.3f\n" $i $(calc "3.1459265358*$i*$i") \
done

   I agree, C is never the wrong language. The reason for the choice was I want
my son to have exposure to it and learn the fundamentals before he gets
corrupted by whatever the latest trendy flavor of language is they teach in
school now. C can do it all, but sometimes it is just takes a bit more effort.
Forget the polymorphism and multi-inheritance for now, well structured basic C
will more than do anything you need to do and with the flexibility to
encapsulate assembler and with tools ranging from X libraries to mysql
connector, there really is no language quite as structured or flexible. It's a
big step up from the F77 I cut my teeth on :)

--
David C. Rankin, J.D.,P.E.
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Anders Johansson-4
On Thursday 03 November 2011 07:57:42 David C. Rankin wrote:
> Forget the polymorphism and multi-inheritance for now,

You might want to forget it, but it is of course possible to do this in C as
well (trivial proof of concept: almost all compilers for languages with these
features are written in C - by the same token, it is possible to do it in
assembly :)

Anders
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

David C. Rankin
In reply to this post by David C. Rankin
On 11/03/2011 07:57 AM, David C. Rankin wrote:

> On 11/03/2011 06:24 AM, Anton Aylward wrote:
>> Roger Oberholtzer said the following on 11/03/2011 04:58 AM:
>>> On Wed, 2011-11-02 at 21:34 -0300, Cristian Rodríguez wrote:
>>>> On 02/11/11 20:49, David C. Rankin wrote:
>>>>
>>>>> Thank you Christian! I would have looked at this for days and not
>>>>> caught the '<=' issue.
>>>>
>>>> Well deserved then :-D , unless you are doing this as a learning
>>>> experience or plan to roll such code in an intensive math computing
>>>> environment, you are picking the wrong language. python or ruby will do
>>>> the trick just fine.
>>>
>>> C is never the wrong language. It is the tasks that are wrong :) ;)
>>>
>>> Add Tcl to the list of alternatives.
>>
>> And shell.
>> The bash shell is an incredibly powerful programming language and very
>> easy to debug.
>>
> <snip>
>
> I actually did the little area calc in bash first :)
>
> for i in 5 7 4 16 9 15 11 13 12; do \
> printf "r: %2s area: %7.3f\n" $i $(calc "3.1459265358*$i*$i") \
> done
>

The full script, with input checking would look like this:

#!/bin/bash

for i in $@; do
   [ $i -eq $i ] &>/dev/null || continue
   printf "r: %2s  area: %7.3f\n" $i $(calc "3.14592638*$i*$i")
done

exit 0

   Save as area.sh, then

sh area.sh 1 2 3 dog 4 5

r:  1  area:   3.146
r:  2  area:  12.584
r:  3  area:  28.313
r:  4  area:  50.335
r:  5  area:  78.648

   Oh, don't forget to install 'calc' first:

http://www.isthe.com/chongo/tech/comp/calc/

tar -xjf calc-2.12.4.4.tar.bz2
cd calc-2.12.4.4
make all
sudo make install

or see:

http://sourceforge.net/projects/calc/files/calc/2.12.4.3/

There are src.rpms there, but they don't build on 11.4 and I haven't taken time
to debug. Job left to the interested reader :)


--
David C. Rankin, J.D.,P.E.
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

David C. Rankin
On 11/03/2011 08:20 AM, David C. Rankin wrote:

>    Oh, don't forget to install 'calc' first:
>
> http://www.isthe.com/chongo/tech/comp/calc/
>
> tar -xjf calc-2.12.4.4.tar.bz2
> cd calc-2.12.4.4
> make all
> sudo make install
>
> or see:
>
> http://sourceforge.net/projects/calc/files/calc/2.12.4.3/
>
> There are src.rpms there, but they don't build on 11.4 and I haven't taken time
> to debug. Job left to the interested reader :)

Oops, latest sources here:

http://www.isthe.com/chongo/src/calc/

--
David C. Rankin, J.D.,P.E.
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

gregfreemyer
In reply to this post by David C. Rankin
On Thu, Nov 3, 2011 at 9:20 AM, David C. Rankin
<[hidden email]> wrote:
> On 11/03/2011 07:57 AM, David C. Rankin wrote:
<snip>

>  Oh, don't forget to install 'calc' first:
>
> http://www.isthe.com/chongo/tech/comp/calc/
>
> tar -xjf calc-2.12.4.4.tar.bz2
> cd calc-2.12.4.4
> make all
> sudo make install
>
> or see:
>
> http://sourceforge.net/projects/calc/files/calc/2.12.4.3/
>
> There are src.rpms there, but they don't build on 11.4 and I haven't taken
> time to debug. Job left to the interested reader :)

Sounds like a perfect job for a budding contributor.

If anyone is interested it is just:
Build that in your home OBS project.  Test it.  Submit it to a devel
project.  Test it.  Submit it to factory..

It goes into the next release.  It's too late for 12.1, but 12.2 will
be in another 8 months.

Greg
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

Malcolm-31
On Thu, 3 Nov 2011 09:48:10 -0400
Greg Freemyer <[hidden email]>
wrote:

> On Thu, Nov 3, 2011 at 9:20 AM, David C. Rankin
> <[hidden email]> wrote:
> > On 11/03/2011 07:57 AM, David C. Rankin wrote:
> <snip>
> >  Oh, don't forget to install 'calc' first:
> >
> > http://www.isthe.com/chongo/tech/comp/calc/
> >
> > tar -xjf calc-2.12.4.4.tar.bz2
> > cd calc-2.12.4.4
> > make all
> > sudo make install
> >
> > or see:
> >
> > http://sourceforge.net/projects/calc/files/calc/2.12.4.3/
> >
> > There are src.rpms there, but they don't build on 11.4 and I
> > haven't taken time to debug. Job left to the interested reader :)
>
> Sounds like a perfect job for a budding contributor.
>
> If anyone is interested it is just:
> Build that in your home OBS project.  Test it.  Submit it to a devel
> project.  Test it.  Submit it to factory..
>
> It goes into the next release.  It's too late for 12.1, but 12.2 will
> be in another 8 months.
>
> Greg
Hi
There is a user with it already...
https://build.opensuse.org/project/packages?project=home%3Amichal-m

--
Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 11.4 (x86_64) Kernel 2.6.37.6-0.7-desktop
up 3 days 1:26, 6 users, load average: 0.27, 0.32, 0.22
GPU GeForce 8600 GTS Silent - Driver Version: 285.05.09


--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

gregfreemyer
On Thu, Nov 3, 2011 at 10:22 AM, Malcolm <[hidden email]> wrote:

> On Thu, 3 Nov 2011 09:48:10 -0400
> Greg Freemyer <[hidden email]>
> wrote:
>
>> On Thu, Nov 3, 2011 at 9:20 AM, David C. Rankin
>> <[hidden email]> wrote:
>> > On 11/03/2011 07:57 AM, David C. Rankin wrote:
>> <snip>
>> >  Oh, don't forget to install 'calc' first:
>> >
>> > http://www.isthe.com/chongo/tech/comp/calc/
>> >
>> > tar -xjf calc-2.12.4.4.tar.bz2
>> > cd calc-2.12.4.4
>> > make all
>> > sudo make install
>> >
>> > or see:
>> >
>> > http://sourceforge.net/projects/calc/files/calc/2.12.4.3/
>> >
>> > There are src.rpms there, but they don't build on 11.4 and I
>> > haven't taken time to debug. Job left to the interested reader :)
>>
>> Sounds like a perfect job for a budding contributor.
>>
>> If anyone is interested it is just:
>> Build that in your home OBS project.  Test it.  Submit it to a devel
>> project.  Test it.  Submit it to factory..
>>
>> It goes into the next release.  It's too late for 12.1, but 12.2 will
>> be in another 8 months.
>>
>> Greg
> Hi
> There is a user with it already...
> https://build.opensuse.org/project/packages?project=home%3Amichal-m

The specfile looks nicely done.  (In general getting the specfile in
good shape is the hardest part of packaging a utility for openSUSE
formal submissions.)

https://build.opensuse.org/package/view_file?file=calc.spec&package=calc&project=home%3Amichal-m&srcmd5=ec9ad07630f88d98e960a1ec300ac0d9

So all the budding contributor has to do is branch that package to
their own OBS home project.  Build it against factory (should happen
automatically.) and whatever release they are running. (Likely will
need to use the WebUI to add that repo.)

Enable publishing for the build they want to test (via the WebUI).

It should then show up in the test search at:
http://software.opensuse.org/search?q=calc&baseproject=openSUSE%3AFactory&lang=en&include_home=true&exclude_debug=true

Once you find it, install it and test it.

Assuming it works submit to an appropriate devel project.  Once
accepted install from that repo, then submit it on to factory for
inclusion in 12.2 and beyond.

Greg
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: c - Segmentation fault on 11.4 - need help figuring out (strtod?)

John Andersen-2
In reply to this post by David C. Rankin
On 11/3/2011 5:57 AM, David C. Rankin wrote:
> The reason for the choice was I want my son to have exposure to it and learn the fundamentals before he gets corrupted by whatever the
> latest trendy flavor of language is they teach in school now.

Good Idea, because once they are exposed to visual basic they can't be taught to program.

--
_____________________________________
---This space for rent---
--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

12