P
Pasquale Minervini
Hello everyone; I'm having severe headaches with the following
snippet:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <wchar.h>
int main(void) {
wchar_t *wbuffer, *outptr;
size_t len, wlen, outsize;
FILE *in;
char buffer[] = "Hello world.";
len = strlen(buffer) + 1;
wbuffer = (wchar_t *)malloc(len * sizeof(wchar_t));
wlen = mbstowcs (wbuffer, buffer, len);
wbuffer = (wchar_t *)realloc (wbuffer, (wlen + 1) * sizeof
(wchar_t));
in = fmemopen(wbuffer, wlen, "r+");
fgetwc(in);
return(EXIT_SUCCESS);
}
It segfaults on the fgetwc(in) instruction, while if I use fgetc(in)
it works like a charm (but I need to interact with a library using
wide chars). Using GDB I get the following:
Program received signal SIGSEGV, Segmentation fault.
0xb7e3c3c3 in _IO_getwc (fp=0x980a060) at getwc.c:42
42 getwc.c: No such file or directory.
in getwc.c
(gdb) backtrace
#0 0xb7e3c3c3 in _IO_getwc (fp=0x980a060) at getwc.c:42
#1 0x0804856f in main () at src/snippet.c:26
Any idea on what could be the case, and what should I do to fix it?
Thanks a lot in advance.
snippet:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <wchar.h>
int main(void) {
wchar_t *wbuffer, *outptr;
size_t len, wlen, outsize;
FILE *in;
char buffer[] = "Hello world.";
len = strlen(buffer) + 1;
wbuffer = (wchar_t *)malloc(len * sizeof(wchar_t));
wlen = mbstowcs (wbuffer, buffer, len);
wbuffer = (wchar_t *)realloc (wbuffer, (wlen + 1) * sizeof
(wchar_t));
in = fmemopen(wbuffer, wlen, "r+");
fgetwc(in);
return(EXIT_SUCCESS);
}
It segfaults on the fgetwc(in) instruction, while if I use fgetc(in)
it works like a charm (but I need to interact with a library using
wide chars). Using GDB I get the following:
Program received signal SIGSEGV, Segmentation fault.
0xb7e3c3c3 in _IO_getwc (fp=0x980a060) at getwc.c:42
42 getwc.c: No such file or directory.
in getwc.c
(gdb) backtrace
#0 0xb7e3c3c3 in _IO_getwc (fp=0x980a060) at getwc.c:42
#1 0x0804856f in main () at src/snippet.c:26
Any idea on what could be the case, and what should I do to fix it?
Thanks a lot in advance.