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
Solved

Gnu C - segmentation fault

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VMware Workstation 12 Player 16 121
Run same command on multiple files in Linux 3 55
How to enable sync between two yum repo? 1 39
Disabling security updates Ubuntu 3 27
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). …
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

808 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