Strange problem with fmemopen() and fgetwc()

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.
 
B

Ben Bacarisse

Pasquale Minervini said:
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+");

fmemopen is not standard C. The only one I know (glibc's fmemopen)
makes no claims to support wide characters at all. I suspect you need
to re-think. A temporary file might do the job.
 
P

Pasquale Minervini

Hi, thanks a lot for your answer!

Yes, fmemopen is actually a GNU extension; I was looking for any
alternative to using temporary files (since using them would cause
frequent interaction with the filesystem, in my case).

Pasquale Minervini said:
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+");

fmemopen is not standard C.  The only one I know (glibc's fmemopen)
makes no claims to support wide characters at all.  I suspect you need
to re-think.  A temporary file might do the job.
   fgetwc(in);
   return(EXIT_SUCCESS);
}
 
A

Antoninus Twink

You probably mean wlen * sizeof(wchar_t): the first argument to
fmemopen() is a void * so it doesn't know the stride of wbuffer.
fmemopen is not standard C. The only one I know (glibc's fmemopen)
makes no claims to support wide characters at all.

It shouldn't need to do much to support them, should it? I always
assumed that fgetwc() and friends just read a multibyte sequence and
then interpret it as a wide character. I'd be more tempted to call it a
library bug than a missing feature...
I suspect you need to re-think. A temporary file might do the job.

There's also open_wmemstream(), which might or might not do what the OP
wants (it's not clear from his post exactly what he's trying to
accomplish).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top