?
Solved

Gnu C - segmentation fault

Posted on 1998-05-05
3
Medium Priority
?
360 Views
Last Modified: 2012-05-04
Hello, I have problems with the following script, in which I tried to "copy" the C library function strtod with the GNU inline assembler:

#include "stdio.h"
#include "stdlib.h"

double TGIstrtod(const char *nptr, char **endptr) {

     long a;
     double returnvalue;
     long b;
     double c;
     long d;
     d = (long)*endptr;
     __asm__ __volatile__(
//here comes the assembler part...
//...
       :"=m" (returnvalue)
       :"S" (nptr), "m" (errno), "m" (a), "m" (b), "m" (c), "m" (d)
       :"eax","ebx","edx");
     *endptr = (char*)d;
     return returnvalue;
}


void main (int argc, char *argv[]){
  char **s;
  *s = ".";
  if (argc > 1) {
     printf("Float: %.10e\n",  TGIstrtod (argv[1],(char **)s));
     printf("%s\n",*s); }
   else printf ("Please run with parameter!!");
}

I get the segmentations fault in the lines *s = "." and if I submit a parameter. I think there's a problem with s, but - being very unfamiliar with C - I don't know why. Any help if greatly appreciated!
0
Comment
Question by:Christian_Wenz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
ID: 1635066
*s is a pointer, you need to alloc memory befor you assign values
0
 
LVL 5

Author Comment

by:Christian_Wenz
ID: 1635067
this might be it. I also found out that I should replace char **)s by &s and put char *s instead of char **s. But how do I allocate memory?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1635068
...
char *s = NULL;
s = malloc(100); /* replace 100 by an appropriate value */
if (s == NULL) {
  printf("error: malloc failed.\n"); exit(1);
}
...
    TGIstrtod(argv[1], &s)
...

Keep in mind that you allocate enough memory for s (used in
TGIstrtod, 'cause it does not check the size of the given
pointer).
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Fine Tune your automatic Updates for Ubuntu / Debian
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question