Click here to Skip to main content
15,885,767 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: MFC: Use firefox in MFC Pin
Maximilien13-Feb-11 1:02
Maximilien13-Feb-11 1:02 
QuestionHow to free the variable Pin
goldenrose912-Feb-11 4:47
goldenrose912-Feb-11 4:47 
AnswerRe: How to free the variable Pin
Yusuf12-Feb-11 5:04
Yusuf12-Feb-11 5:04 
GeneralRe: How to free the variable Pin
goldenrose912-Feb-11 5:10
goldenrose912-Feb-11 5:10 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 5:19
Aescleal12-Feb-11 5:19 
GeneralRe: How to free the variable Pin
goldenrose912-Feb-11 6:23
goldenrose912-Feb-11 6:23 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 10:07
Aescleal12-Feb-11 10:07 
AnswerRe: How to free the variable PinPopular
Aescleal12-Feb-11 5:17
Aescleal12-Feb-11 5:17 
It looks like you're getting confused by what a pointer is. A pointer is just a variable that can be set to the addresses of arbitrary chunks of system memory. You don't need to "free" reader but you have to release whatever it points to back to the compiler's runtime. In the case you've presented all you have to do is:

delete [] reader;


when you've finished with the block of memory the pointer points to.

However the way you've written the code is a bit crap - if anything throws an exception between calling bReadFile and the delete you're going to leak memory. Instead of using an array consider using something with a bit more behavioural intelligence - e.g. std::vector. Then you'll not have to worry about cleaning up after yourself:

std::vector<char> read_from_file( std::size_t bytes_to_read )
{
    std::vector<char> bytes_read( bytes_to_read );
    std::size_t number_bytes_read = 0;
    ReadFile( handle_of_file, &bytes_read[ 0 ], &number_of_bytes_read, 0 );
    return bytes_read;
}


The code there will be within 5% of the performance of what you've written (faster on some compilers as there's no pointer aliasing) AND exception safe.

Cheers,

Ash

PS: Anyone who thinks there's an expensive copy of the vector returned to the caller should upgrade their compiler.
GeneralRe: How to free the variable [modified] Pin
Niklas L12-Feb-11 6:30
Niklas L12-Feb-11 6:30 
GeneralRe: How to free the variable [modified] Pin
goldenrose912-Feb-11 7:30
goldenrose912-Feb-11 7:30 
GeneralRe: How to free the variable Pin
Niklas L12-Feb-11 8:18
Niklas L12-Feb-11 8:18 
GeneralRe: How to free the variable PinPopular
Aescleal12-Feb-11 10:02
Aescleal12-Feb-11 10:02 
GeneralRe: How to free the variable Pin
Niklas L12-Feb-11 12:56
Niklas L12-Feb-11 12:56 
GeneralRe: How to free the variable Pin
Stefan_Lang13-Feb-11 21:58
Stefan_Lang13-Feb-11 21:58 
GeneralRe: How to free the variable Pin
Niklas L13-Feb-11 23:28
Niklas L13-Feb-11 23:28 
GeneralRe: How to free the variable Pin
Stefan_Lang14-Feb-11 0:18
Stefan_Lang14-Feb-11 0:18 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 10:10
Aescleal12-Feb-11 10:10 
QuestionMessage to response function SetFont() Pin
includeh1012-Feb-11 3:38
includeh1012-Feb-11 3:38 
AnswerRe: Message to response function SetFont() Pin
Code-o-mat12-Feb-11 6:52
Code-o-mat12-Feb-11 6:52 
AnswerRe: Message to response function SetFont() Pin
User 742933813-Feb-11 2:57
professionalUser 742933813-Feb-11 2:57 
AnswerRe: Message to response function SetFont() Pin
Hans Dietrich12-Feb-11 6:53
mentorHans Dietrich12-Feb-11 6:53 
QuestionCEdit Box Error Pin
raju_shiva12-Feb-11 0:40
raju_shiva12-Feb-11 0:40 
AnswerRe: CEdit Box Error Pin
Richard MacCutchan12-Feb-11 2:04
mveRichard MacCutchan12-Feb-11 2:04 
QuestionUsing gdiplus to skew, rotate and resize a matrix (image) Pin
Kiran Satish11-Feb-11 13:43
Kiran Satish11-Feb-11 13:43 
AnswerRe: Using gdiplus to skew, rotate and resize a matrix (image) Pin
Rozis14-Feb-11 10:34
Rozis14-Feb-11 10:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.