Solved

Gnu C - segmentation fault

Posted on 1998-05-05
3
348 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
  • 2
3 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS CLI - Issue with name display 2 72
Can't See Site After DNS Resolved 7 55
Linux hostname change 2 71
android secure ftp 3 38
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

776 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