Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

malloc() and TCHAR*

646 views
Skip to first unread message

Man-wai Chang

unread,
Feb 26, 2012, 5:37:48 AM2/26/12
to

What could cause the following line to crash the program when executed?

my_fff=(TCHAR *)malloc((_tcslen(pathname)+2)*sizeof(TCHAR));

No compilation error.

--
@~@ You have the right to remain silence.
/ v \ Simplicity is Beauty! May the Force and farces be with you!
/( _ )\ (Fedora 15 i686) Linux 3.2.6
^ ^ 18:35:01 up 2 days 18:48 0 users load average: 0.00 0.01 0.05
不借貸! 不詐騙! 不援交! 不打交! 不打劫! 不自殺! 請考慮綜援 (CSSA):
http://www.swd.gov.hk/tc/index/site_pubsvc/page_socsecu/sub_addressesa

David Lowndes

unread,
Feb 26, 2012, 5:51:33 AM2/26/12
to
>What could cause the following line to crash the program when executed?

"crash" - what sort of "crash"?

>my_fff=(TCHAR *)malloc((_tcslen(pathname)+2)*sizeof(TCHAR));

Probably that your pathname variable points to somewhere that your
process doesn't own - or its not a null terminated string - and hence
eventually points to somewhere you don't own.

Dave

Man-wai Chang

unread,
Feb 26, 2012, 6:11:53 AM2/26/12
to
On 26/02/12 6:51 PM, David Lowndes wrote:
>> What could cause the following line to crash the program when executed?
> "crash" - what sort of "crash"?

Using up memory I believed. Not using the VC debugger.
(please read on)

>> my_fff=(TCHAR *)malloc((_tcslen(pathname)+2)*sizeof(TCHAR));
> Probably that your pathname variable points to somewhere that your
> process doesn't own - or its not a null terminated string - and hence
> eventually points to somewhere you don't own.

Sorry, I was a fool. I failed to plug the memory leak inside the loop of
a recursive function. Problem solved after calling free() at the right
spot to pair the calloc().

I was too used to automatic free() with Visual Foxpro. :)

--
@~@ You have the right to remain silence.
/ v \ Simplicity is Beauty! May the Force and farces be with you!
/( _ )\ (Fedora 15 i686) Linux 3.2.6
^ ^ 19:08:01 up 2 days 19:21 0 users load average: 0.00 0.01 0.05

David Lowndes

unread,
Feb 26, 2012, 9:35:48 AM2/26/12
to
>Sorry, I was a fool. I failed to plug the memory leak inside the loop of
>a recursive function. Problem solved after calling free() at the right
>spot to pair the calloc().

Learn to use modern C++ rather than 'C' - using a std::vector would
have eliminated the source of such problems.

Dave

Man-wai Chang

unread,
Feb 26, 2012, 10:41:55 AM2/26/12
to
> Learn to use modern C++ rather than 'C' - using a std::vector would
> have eliminated the source of such problems.

Later... I might start converting to full C plus plus. But then I only
want to write a really simple Form app for my own use. :)

--
@~@ You have the right to remain silence.
/ v \ Simplicity is Beauty! May the Force and farces be with you!
/( _ )\ (Fedora 15 i686) Linux 3.2.6
^ ^ 23:40:01 up 2 days 23:53 0 users load average: 0.00 0.01 0.05

Man-wai Chang

unread,
Feb 26, 2012, 10:42:57 AM2/26/12
to
On 26/02/12 11:41 PM, Man-wai Chang wrote:
>> Learn to use modern C++ rather than 'C' - using a std::vector would
>> have eliminated the source of such problems.
>
> Later... I might start converting to full C plus plus. But then I only
> want to write a really simple Form app for my own use. :)
>

oops.. sorry! Not "Form app", but "Dialogue". :)

David Lowndes

unread,
Feb 26, 2012, 12:40:56 PM2/26/12
to
>> Learn to use modern C++ rather than 'C' - using a std::vector would
>> have eliminated the source of such problems.
>
>Later... I might start converting to full C plus plus. But then I only
>want to write a really simple Form app for my own use. :)

It might only be simple, but you've already fallen foul of memory
allocation. It's better to learn good techniques and put them into
practice with small examples asap rather than persist with the bad old
ways of doing things. Honestly, you can largely write modern C++
without ever having to write malloc/free or new/delete - and you get
an altogether cleaner and better product for it.

Dave

Man-wai Chang

unread,
Feb 26, 2012, 12:59:53 PM2/26/12
to
> It might only be simple, but you've already fallen foul of memory
> allocation.

I found the bug and learnt. It's been a long time since I did my first C
project... very very long time indeed. Back then, C++ was still a baby.

> It's better to learn good techniques and put them into
> practice with small examples asap rather than persist with the bad old
> ways of doing things. Honestly, you can largely write modern C++
> without ever having to write malloc/free or new/delete - and you get
> an altogether cleaner and better product for it.

My original plan is to use C and Win32API only. I still want to do
things the old-fashioned way. It's challenging and stupid, but then it
was how things were done. I suppose the Window$ OS itself is still
written mostly in plain old C?

Also, MFC is NOT free, not available via VC++ Express.

--
@~@ You have the right to remain silence.
/ v \ Simplicity is Beauty! May the Force and farces be with you!
/( _ )\ (Fedora 15 i686) Linux 3.2.6
^ ^ 01:56:01 up 3 days 2:09 0 users load average: 0.00 0.01 0.05

David Lowndes

unread,
Feb 26, 2012, 2:42:22 PM2/26/12
to
>My original plan is to use C and Win32API only. I still want to do
>things the old-fashioned way. It's challenging and stupid, but then it
>was how things were done.

... and how 'C', & C++ gets a bad reputation. :(

>I suppose the Window$ OS itself is still
>written mostly in plain old C?

I guess it's a mixture of things from assembly up.

>Also, MFC is NOT free, not available via VC++ Express.

No, but C++ and the standard library are.

Dave

Ant

unread,
Feb 26, 2012, 2:46:49 PM2/26/12
to
"Man-wai Chang" wrote:

> My original plan is to use C and Win32API only. I still want to do
> things the old-fashioned way. It's challenging and stupid, but then it
> was how things were done.

It's not stupid at all but the best way to learn how Windows works at
a low level. Also, using the Win32 API directly is sometimes the only
way to get full control of things where a higher level wrapper won't
let you. Granted, you can still use the API in higher level languages
but in my view you can't beat C for simplicity and low overhead.

However, I wouldn't necessarily use C for writing large applications
for corporate or commercial use. There are better languages when
programmer productivity is an issue and you'd rather not have to deal
with the low level stuff.

> I suppose the Window$ OS itself is still written mostly in plain
> old C?

Yes, the core OS is (and assembler) and the Win32 API documentation
assumes C.


Jasen Betts

unread,
Feb 27, 2012, 5:32:25 AM2/27/12
to
On 2012-02-26, Man-wai Chang <toylet...@gmail.com> wrote:
>
> What could cause the following line to crash the program when executed?
>
> my_fff=(TCHAR *)malloc((_tcslen(pathname)+2)*sizeof(TCHAR));
>
> No compilation error.
>

Since you're casting the return of malloc that means you're
coding in c++, In which case my_fff could be a reference,
and if it's not a valid reference that could be your crash.

--
⚂⚃ 100% natural

--- Posted via news://freenews.netfront.net/ - Complaints to ne...@netfront.net ---

Man-wai Chang

unread,
Feb 27, 2012, 6:04:34 AM2/27/12
to
> However, I wouldn't necessarily use C for writing large applications
> for corporate or commercial use. There are better languages when
> programmer productivity is an issue and you'd rather not have to deal
> with the low level stuff.

I agree. Foxpro is a lot more agile and simple when doing business apps.
Too bad it does not support Unicode.

Anyway, my VC++ project is NOT a big one. And I still have spare time to
waste. :)

> Yes, the core OS is (and assembler) and the Win32 API documentation
> assumes C.

How about the Theme and Explorer? Are they in C++ plus MFC?

--
@~@ You have the right to remain silent.
/ v \ Simplicity is Beauty!
/( _ )\ May the Force and farces be with you!
^ ^ (x86_64 Ubuntu 9.10) Linux 2.6.39.3

Man-wai Chang

unread,
Feb 27, 2012, 6:05:07 AM2/27/12
to
>> Also, MFC is NOT free, not available via VC++ Express.
>
> No, but C++ and the standard library are.

Too bad MFC is not free. :)

--
@~@ You have the right to remain silent.
/ v \ Simplicity is Beauty!
/( _ )\ May the Force and farces be with you!
^ ^ (x86_64 Ubuntu 9.10) Linux 2.6.39.3

Ant

unread,
Feb 27, 2012, 7:59:33 AM2/27/12
to
"Man-wai Chang" wrote:

>> Yes, the core OS is (and assembler) and the Win32 API documentation
>> assumes C.
>
> How about the Theme and Explorer? Are they in C++ plus MFC?

They might be but I don't know. That's getting away from the core and
certainly, when you start looking at more advanced stuff like the COM
interfaces and ActiveX then C++ is more in evidence.

What I consider the core (on a 32 bit OS) are ntoskrnl.exe, hal.dll
and win32k.sys, plus basic drivers on the kernel mode side; and in
user mode: kernel32.dll and ntdll.dll, plus a few others like
advapi32.dll, user32.dll and gdi32.dll. These user-mode DLLs contain
the bulk of the Win32 API.


Man-wai Chang

unread,
Feb 27, 2012, 8:55:09 AM2/27/12
to
> What I consider the core (on a 32 bit OS) are ntoskrnl.exe, hal.dll
> and win32k.sys, plus basic drivers on the kernel mode side; and in
> user mode: kernel32.dll and ntdll.dll, plus a few others like
> advapi32.dll, user32.dll and gdi32.dll. These user-mode DLLs contain
> the bulk of the Win32 API.

A bit expected. Thanks.

--
@~@ You have the right to remain silence.
/ v \ Simplicity is Beauty! May the Force and farces be with you!
/( _ )\ (Fedora 15 i686) Linux 3.2.6
^ ^ 21:53:01 up 3 days 14:29 0 users load average: 0.19 0.10 0.07

r_z_...@pen_fact.com

unread,
Feb 27, 2012, 10:48:36 AM2/27/12
to
On Mon, 27 Feb 2012 19:05:07 +0800, Man-wai Chang
<toylet...@gmail.com> wrote:

>>> Also, MFC is NOT free, not available via VC++ Express.
>>
>> No, but C++ and the standard library are.
>
>Too bad MFC is not free. :)

You don't need MFC to use C++ with Win32. It doesn't add any
functions, just possible conveniences. You can write your own
replacements for everything in MFC.

-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret
PenFact, Inc.
20 Park Plaza, Suite 400
Boston, MA 02116
www.penfact.com
Useful reading (be sure to read its disclaimer first):
http://catb.org/~esr/faqs/smart-questions.html

Man-wai Chang

unread,
Feb 27, 2012, 12:08:11 PM2/27/12
to
> You don't need MFC to use C++ with Win32. It doesn't add any
> functions, just possible conveniences. You can write your own
> replacements for everything in MFC.

I was expecting MFC to speed up coding by a significant margin ....

--
@~@ You have the right to remain silence.
/ v \ Simplicity is Beauty! May the Force and farces be with you!
/( _ )\ (Fedora 15 i686) Linux 3.2.6
^ ^ 01:07:02 up 3 days 17:43 0 users load average: 0.00 0.01 0.05

Ulrich Eckhardt

unread,
Feb 27, 2012, 12:37:37 PM2/27/12
to
Am 27.02.2012 18:08, schrieb Man-wai Chang:
>> You don't need MFC to use C++ with Win32. It doesn't add any
>> functions, just possible conveniences. You can write your own
>> replacements for everything in MFC.
>
> I was expecting MFC to speed up coding by a significant margin ....
>

This "speed up" is the "conveniences" that Robert is talking about.
However, using C++ with its standard library is also a convenience that
speeds up development compared to plain C.

At least that's how I understand it. ;)

Uli

Man-wai Chang

unread,
Feb 28, 2012, 4:15:46 AM2/28/12
to
> However, using C++ with its standard library is also a convenience that
> speeds up development compared to plain C.
> At least that's how I understand it. ;)

As long as I made no mistake in type casting and matching calloc-free
pairs correctly, plain C should be fine. ;)

--
@~@ You have the right to remain silent.
/ v \ Simplicity is Beauty!
/( _ )\ May the Force and farces be with you!
^ ^ (x86_64 Ubuntu 9.10) Linux 2.6.39.3
0 new messages