strange? pointer error

Started by
20 comments, last by AbsolutDew 21 years, 11 months ago
When i compile my program I get a warning about some variable not being initialized, ie char* szSomeString. So i set it rewrite it as char* szSomeString = NULL, compiles fine, no warnings, but crashed during execution with the usual helpful windows error code. Anyone know some possible causes?
Advertisement
Without any more info I would say you are dereferencing (and most likely writing to) a NULL pointer -> bad. Allocate some memory for the pointer or give us more details.

- mongrelprogrammer
- I hate these user ratings. Please rate me down. (Seriously) -

  char* szTextBuffer;						SendMessage( IMWatchList[x].GetIMRecvHwnd(), WM_GETTEXT, (WPARAM)iLength, (LPARAM)szTextBuffer );				string strTextBuffer = szTextBuffer;  


that''s what im doing, i was under the impression i didn''t need to allocate memory when using it like this. But im occasionally wrong
you always have to allocate memory for things. the only time you dont is if the function takes a char**, so it can actually return the address to you. if your not sure wether you need to allocate memory, you allocate it. ALWAYS free that memory. if you dont, then you might as well get some corks, cause your app will be leaking something awful.

RTFM when it comes to using functions. actually read what the function does. understand what the args are for.

this is why i *HATE* ppl overusing STL. they neglect important issues when using C fucntions because its all handled for them.

if you dont want to allocate memory, the declare szTextBuffer as
char szTextBuffer[1024];
then its plopped on the stacka nd the compiler worries about allocation, deallocation.
How do we not overuse STL? If we decide to use STL in a project, we should standardise it or your code won''t be consistent. Though when it comes to win32 programming, you can''t use string but I do know how to use them. I know how linked lists works but implementing my own will only serve to confuse other programmers using STL.
quote:Original post by a person
this is why i *HATE* ppl overusing STL. they neglect important issues when using C fucntions because its all handled for them.

How much use is overuse? How much of a low-level must we descend to before our code is acceptable to you? What does the STL have to do with C functions, C being an entirely different language?

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

[edited by - SabreMan on May 1, 2002 8:39:04 AM]
c style fucntions dont accept stl objects. plain and simple. win32 api uses c style styntax. you dont need to use ANY low level code. its just silly to use STL for everything, yet dont understand simple concepts like pointers. i am all for ppl using STL for everything if it is effeicent and works for them, but i dislike ppl using STL when they are using it so they dont have to understand the concepts that STL hides. pointers and memory allocation is a very important concept that sometimes gets lost with STL since it tends to automagically handle most memory management for you. not a problem when undertsand this, and when using the win32 api you can handle memory allocation that may be required for a function.

i am in no way critizing STL use by expierenced coders who know what they are doing. i am critizing overuse of STL by newbies so they can make things quicker without understand the fundamentals.

sabreman, c is not entirly different language. it can be sued along side c++ code. it is used by the win32 api, and other ocde that wants to be easily used from within a c compiler without hacks. the win32 api is OO, but does not use c++. instead using c and encapsulating things using handles to the objects. a window ia an object, it has methods you can call. the difference is that the win32 api was designed with a functional compiler vs an OO compiler in mind. because of this, you have to deal with string allocation, using functions on pointers to change an objects attribute, etc. this is why MFC was created, to hide the c syntax chosen for the win32 api. it have an completely OO approach to most of the api with c++ syntax. now instead of using a char*, you would use a CString. the methods would handle the internal conversions required.

please ponder the following: would you stop using the win32 api and switch to MFC for everything you can use it for? then you can not worry about silly things like char* within most win32 api fucntions. maybe writing an stl wrapper for win32 functions might be in order. then you can stick to completly standard STL objects when dealing with the win32 api. though to write such a wrapper you need to understand what STL is doing under the hood to an extent and be able to take the data stored within the STL object and "convert" it for use within the OO C based win32 api.

such a rant should help answer your questions sabreman. again, i dont care if ppl use STL to solve even the smallest of problems. overuse comes when you use it because you dont understand something, and its a way to hide that (like not understanding concepts like pointers and data storage). perhaps i should have made the previous message clearer. I *HATE* when ppl neglect to understand important fundamental concepts of programming because they use overuse STL. is that better?
quote:Original post by a person
c style fucntions dont accept stl objects. plain and simple.

Yes, but the STL is for C++ programs. If you need to interface with C functions, then it caters for that up to a point.
quote:
i am all for ppl using STL for everything if it is effeicent and works for them, but i dislike ppl using STL when they are using it so they dont have to understand the concepts that STL hides.

OTOH, it is more productive to learn about language concepts when you need them. There is no point teaching a newbie all about confusing pointer and memory management concepts when all he wants to do is, say, write a few strings to a file. You teach the simpler concepts first, the harder ones when they are necessary.
quote:
pointers and memory allocation is a very important concept that sometimes gets lost with STL since it tends to automagically handle most memory management for you.

Good. The less I see of pointers and manual memory management in my code, the happier I am. There are loads of languages where you can do a lot of productive work without ever being exposed to a pointer. Pointers are only important when a better alternative is not available.
quote:
i am in no way critizing STL use by expierenced coders who know what they are doing. i am critizing overuse of STL by newbies so they can make things quicker without understand the fundamentals.

I'd say it's more crucial for a newbie to make things quicker without understanding unnecessary concepts. I go as far as to say C and C++ are terrible languages for newbies to learn programming. C++ isn't actually that bad, but this elitist nonsense about having to learn pointers and memory management first is damaging both to C++, and to the newbie trying to find his way.
quote:
sabreman, c is not entirly different language. it can be sued along side c++ code.

C and C++ are maintained by two distinct ISO working groups. Despite C++ having it's roots in C, it has a distinctly different grammar, and the recommended idioms for each language are incredibly different. To give you an idea of how different they really are, read this: Learning C++ as a new language.
quote:
please ponder the following: would you stop using the win32 api and switch to MFC for everything you can use it for?

Mwah-ha-ha! That's a different issue.
quote:
such a rant should help answer your questions sabreman.

It makes me think that what you have said is ill-considered. C++ is not C with knobs on. It's very different.
quote:
overuse comes when you use it because you dont understand something, and its a way to hide that

Rubbish! Perhaps you would say that you should not use, say, COM if you don't know all the details of how it works? Maybe you shouldn't use your compiler if you can't read and understand all the ASM that it generates. Perhaps you shouldn't use your operating system if you haven't written one yourself. Do you see where your argument leads? There is no reason to make someone learn something that they don't yet need.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

[edited by - SabreMan on May 1, 2002 4:04:39 PM]
You know, most beginners'' instruction languages don''t teach "basics" like pointers - BASIC, Pascal... And that''s why Java is also gaining steam as an intro language. If C++ was taught with the STL from the ground up, with lower level concepts being introduced later, we''d have better C++ programmers overall.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
In defense of myself i'd like to say im very new to STL, and i'm quite comfortable with not using it, what i didn't understand was the char* szSomeVar syntax. I was under the impression it's size was determined at run time, and then could not be changed after that.

And what do you mean by read the fucking manual? I wasn't asking how to use a function, im aware i need a c string.

[edited by - absolutdew on May 1, 2002 7:12:43 PM]

This topic is closed to new replies.

Advertisement