texmacs-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Texmacs-dev] character encoding


From: david
Subject: Re: [Texmacs-dev] character encoding
Date: Tue, 28 Jan 2003 17:18:36 +0100
User-agent: Mutt/1.4i

On Tue, Jan 28, 2003 at 04:44:03PM +0100, Joris van der Hoeven wrote:
> 
> > On Tue, 2003-01-28 at 14:28, Joris van der Hoeven wrote:
> > > > Any idea why?
> > > 
> > > Yes: you should use delete[] instead of delete
> > > in order to delete arrays.
> > 
> > I now tried
> > 
> > string
> > convert_using_iconv(string input, string from, string to) {
> >   char* in_buf = as_charp(input);
> >   char* from_cp = as_charp(from);
> >   char* to_cp = as_charp(to);
> >   size_t inbytesleft = (size_t) N(input);
> >   string result;
> > 
> >   iconv_t cd = iconv_open(to_cp,from_cp);
> >   if(cd==(iconv_t)-1) return "";
> >   
> >   while (inbytesleft > 0) {
> >     size_t out_buf_len = max(inbytesleft,10);
> >     size_t outbytesleft = out_buf_len;
> >     char* out_buf = new char[out_buf_len];
> >     char* start_out_buf = out_buf;
> >     size_t flag = iconv (cd, &in_buf, &inbytesleft, &out_buf, 
> > &outbytesleft);
> >     if(flag==(size_t)-1 && errno != E2BIG) { system_error("String 
> > conversion using iconv failed!"); return "";}
> >     result << string(start_out_buf, out_buf_len - outbytesleft);
> >     //delete[] out_buf;
> >   }
> >   iconv_close(cd);
> >   delete[] in_buf; delete[] from_cp; delete[] to_cp;
> >   return result;
> > }
> > 
> > which worked, but as soon as I remove the comment befor delete[]
> > out_buf, I got a segfault again. Obviously this is because I allocated
> > out_buf manually. But what did I do wrong?
> 
> I think that the string::string(char* s, int n)
> does not make a copy of the memory.

Wrong:

    string::string (char* a, int n) {
      register int i;
      rep= new string_rep(n);
      for (i=0; i<n; i++)
        rep->a[i]=a[i];
   }

I find the line

   size_t flag = iconv (cd, &in_buf, &inbytesleft, &out_buf, &outbytesleft);

very suspect. Indeed, out_buf can be modified my the iconv function.
That is probably what is happening.

I suggest that you free start_out_buf instead.

-- 
David Allouche         | GNU TeXmacs -- Writing is a pleasure
Free software engineer |    http://www.texmacs.org
   http://ddaa.net     |    http://alqua.com/tmresources
   address@hidden  |    address@hidden
TeXmacs is NOT a LaTeX front-end and is unrelated to emacs.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]