Christian_Wenz
asked on
Gnu C - segmentation fault
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!
#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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
...
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).
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).
ASKER