Solved

Gnu C - segmentation fault

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

19 Experts available now in Live!

Get 1:1 Help Now