Solved

Gnu C - segmentation fault

Posted on 1998-05-05
3
344 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now