Re: Segmentation fault when writing to a file once it has reacher
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Re: Segmentation fault when writing to a file once it has reacher




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Re: Segmentation fault when writing to a file once it has reacher  
Bjorn Reese


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-19-06 06:21 PM

Wes wrote:

>         char*                   ofile   = new char[sizeof(file) + 4];
[...]
>         for(int i = 0; i < sizeof(file); i++)
>         {
>                 ofile[i] = file[i];
>         }
>         // then tack on the '.enc'
>         ofile[sizeof(file) + 1] = '.';
>         ofile[sizeof(file) + 2] = 'e';
>         ofile[sizeof(file) + 3] = 'n';
>         ofile[sizeof(file) + 4] = 'c';

sizeof(file) will return the size of the file pointer (most likely 4),
not the number of characters in the string that file is pointing to.
You should use strlen(file) instead.

>                 tbuff = Encrypt(buffer);

This is probably the cause of your crash. You have, most likely, been
fooled by the fact that char pointers work differently than std::string.
If tbuff was a std::string then the above would copy the results of
Encrypt() to the tbuff variable, and all would be fine.

However, the tbuff variable is a char pointer. Initially you allocate 32
bytes for this buffer. However, the above assignment overwrites the
tbuff pointer with whatever pointer is returned by Encrypt(). The result
of Encrypt() is not copied into the initially allocated tbuff. If
Encrypt() returns an allocated buffer then you have a memory leak here,
otherwise you are probably returning garbage that is causing the
subsequent crash. The fact that it happens when you reach 15360 is
accidental.

--
mail1dotstofanetdotdk





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:09 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register