|
It's undefined if you try to call a member function on a NULL pointer whether virtual or not. If you want to check inside the function if the pointer is NULL, make it a static member function or non-member function and pass in a pointer to the object.
|
# ? Dec 15, 2008 04:56 |
|
|
# ? May 18, 2024 07:48 |
|
Avenging Dentist posted:Quoth the Standard: Many thanks, you standard-diver you. theg sprank fucked around with this message at 04:59 on Dec 15, 2008 |
# ? Dec 15, 2008 04:56 |
|
theg sprank posted:I have a question for C++ language nazis. Do you mean if its a run-time or compile time error to call methods on a NULL pointer? I think in both cases they are run-time error. I believe that virtual methods are contained in some virtual function table list, and compiler links to them at run-time so for a null pointer, it would still have a type, and the address of the function you are trying to call would still be able to be determined at compile time. So, The answer to your question is that in both cases it would be a run-time error to call methods on a NULL pointer.
|
# ? Dec 15, 2008 05:16 |
|
hexadecimal posted:Do you mean if its a run-time or compile time error to call methods on a NULL pointer? I think in both cases they are run-time error. I believe that virtual methods are contained in some virtual function table list, and compiler links to them at run-time so for a null pointer, it would still have a type, and the address of the function you are trying to call would still be able to be determined at compile time. I'm not sure what you are attempting to say here, but it is almost certainly wrong.
|
# ? Dec 15, 2008 05:33 |
|
Avenging Dentist posted:I'm not sure what you are attempting to say here, but it is almost certainly wrong. Hexadecimal giving a stupidly wrong answer to a question which someone has already answered? No way!
|
# ? Dec 15, 2008 07:07 |
|
ultra-inquisitor posted:appendandclear() seems to be the simplest thing to change. First you need to check whether the list you're appending onto is empty. If it is, pop_front_push_elsewhere the first element. Then, just loop through and do the rest normally: Ahhh, recursion on the brain was killing how I should even done a function normally. I had some other problems unrelated to everything I've been posting, but managed to work them out and now it works! Thanks again!
|
# ? Dec 15, 2008 09:58 |
|
code:
Perhaps I misread or misunderstood the person who asked about this. vvvvvvvvvvvvvvvv ok, admittedly I am not an expert on how virtual functions are implemented in C++. Thank you for your explanation, and sorry for giving the wrong one to the original person asking about this. hexadecimal fucked around with this message at 16:25 on Dec 15, 2008 |
# ? Dec 15, 2008 15:49 |
|
hexadecimal posted:What is wrong about it? quote:I believe that virtual methods are contained in some virtual function table list, and compiler links to them at run-time so for a null pointer, it would still have a type, and the address of the function you are trying to call would still be able to be determined at compile time. This part is pretty wrong. Virtual functions are (er, usually, in almost all implementations of C++) looked up using a vtable pointer which is a part of the actual object itself as opposed to using the type of the pointer. Obviously NULL isn't going to have a vtable pointer (because there's no object there at all). Also 'links' probably isn't a good word to use there, since it sounds like you're talking about linking like a linker does. But don't take it to heart, flowenol and AD are horrible trolls (how about being HELPFUL as well as right, jerks????)
|
# ? Dec 15, 2008 16:07 |
|
hexadecimal posted:
If you didn't dereference this in bar, then on many compilers, you would get away with doing the call with no harm done. However, it's obviously not good practice, as it's compiler specific, but most compilers (read: the 4-5 that I use anyway)behave the same way for this case.
|
# ? Dec 15, 2008 17:38 |
|
digibawb posted:If you didn't dereference this in bar, then on many compilers, you would get away with doing the call with no harm done. However, it's obviously not good practice, as it's compiler specific, but most compilers (read: the 4-5 that I use anyway)behave the same way for this case. I actually tried this, and it worked. That is i just printed something in bar without using any member variables. I am confused though, what object is this being called on when i call it on NULL pointer? Does it just treat it as static method?
|
# ? Dec 15, 2008 17:44 |
|
hexadecimal posted:I actually tried this, and it worked. That is i just printed something in bar without using any member variables. I am confused though, what object is this being called on when i call it on NULL pointer? Does it just treat it as static method? Calling a non-virtual member function is functionally no different from calling a nonmember function that takes a pointer to the type as a parameter. If you aren't dereferencing it, realistically no harm will come. It's still technically undefined behavior, however (by authority of the standard, not because it can't be forced to work reliably).
|
# ? Dec 15, 2008 17:55 |
|
Bah, nevermind. I had misinterpreted a compiler error.
Lexical Unit fucked around with this message at 20:55 on Dec 15, 2008 |
# ? Dec 15, 2008 19:31 |
|
That Turkey Story posted:If you aren't dereferencing it, realistically no harm will come. I had a weird crash once on Visual C++ calling a function that was something like this: code:
|
# ? Dec 15, 2008 20:38 |
|
Painless posted:I had a weird crash once on Visual C++ calling a function that was something like this: Well that's explained as simply that the "real" dereference in the code Visual C++ generates happens on the "bar += baz" expression, although the behavior is undefined as soon as you dereferenced the null pointer to get the "null reference".
|
# ? Dec 15, 2008 20:45 |
|
floWenoL posted:Well that's explained as simply that the "real" dereference in the code Visual C++ generates happens on the "bar += baz" expression, although the behavior is undefined as soon as you dereferenced the null pointer to get the "null reference". Quoted for truth. Dereferencing and assigning to a reference has undefined results if the pointer's value is NULL (just like all NULL pointer deferences), but internally, it's generally just copying the pointer value so you're probably not going to get a crash until you actually read from or write to your reference.
|
# ? Dec 15, 2008 20:47 |
|
That Turkey Story posted:Calling a non-virtual member function is functionally no different from calling a nonmember function that takes a pointer to the type as a parameter. If you aren't dereferencing it, realistically no harm will come. It's still technically undefined behavior, however (by authority of the standard, not because it can't be forced to work reliably). What exactly do you mean by a "function that takes a pointer to the type as a parameter". Is it something like this? code:
code:
|
# ? Dec 15, 2008 22:23 |
|
hexadecimal posted:What exactly do you mean by a "function that takes a pointer to the type as a parameter". Yes, where obj is like this and where the pointer is const.
|
# ? Dec 15, 2008 22:28 |
|
That Turkey Story posted:Yes, where obj is like this and where the pointer is const. Why does the pointer have to be const? Would it only have to be const if the function itself is const?
|
# ? Dec 15, 2008 22:34 |
|
hexadecimal posted:Why does the pointer have to be const? Would it only have to be const if the function itself is const? I mean the pointer itself is const, not that it's pointing to const. In other words, you can't ever modify "this" in a member function call, only what "this" points to.
|
# ? Dec 15, 2008 22:36 |
|
Say I have a void pointer that points to some random object. If I do the following, will it free up the right amount of memory?code:
|
# ? Dec 17, 2008 18:42 |
|
Bitruder posted:Say I have a void pointer that points to some random object. If I do the following, will it free up the right amount of memory? Yes.
|
# ? Dec 17, 2008 18:58 |
|
The answer is probably "no," but I thought I'd ask anyway: Is there a way to tell Visual Studio's linker, when it encounters a multiply defined symbol, which one is the "right" one? The scenario is sort of asinine. Our company's application is MFC-based, v2.5 I believe, and we're having trouble throwing exceptions through it. Most of the time this is handled by a redefined AfxWinMain that handles our application's exception class - which is not derived from CException. We're getting some odd activation context exceptions in certain scenarios when an exception is thrown from a window's OnCreate handler. The architect working with me on this thinks the band-aid solution is to redeclare AfxCallWndProc to be able to handle our exceptions as well as CExceptions, but that is proving difficult because AfxCallWndProc isn't declared as extern by MFC. AfxWinMain is one of only a handful of externs in MFC, as far as I can tell, and none of the others have dick to do with the problem. So yeah, any way to force the linker to accept a specific location of a symbol as The Symbol?
|
# ? Dec 17, 2008 19:46 |
|
csammis posted:The answer is probably "no," but I thought I'd ask anyway: Is there a way to tell Visual Studio's linker, when it encounters a multiply defined symbol, which one is the "right" one? Have you tried decorating the symbol with __declspec(selectany)?
|
# ? Dec 17, 2008 19:57 |
|
ehnus posted:Have you tried decorating the symbol with __declspec(selectany)? The symbol here is a method, AfxCallWndProc(CWnd*, HWND, UINT, WPARAM, LPARAM). selectany seems to only apply to data - trying it, I get "'selectany' can only be applied to data items with external linkage"
|
# ? Dec 17, 2008 20:11 |
|
What's the best cross-platform software for C++ development besides Eclipse?
|
# ? Dec 17, 2008 20:43 |
|
almostkorean posted:What's the best cross-platform software for C++ development besides Eclipse? Depends what you want to do. A lot of people swear by vim or emacs, and a lot of people swear by Visual Studio (of course, that's not cross-platform). Eclipse is good stuff too - you should just try a bunch of different things until you're comfortable with your editor. You should be careful asking a question like that around here too - may start a war.
|
# ? Dec 17, 2008 21:14 |
|
Ari posted:You should be careful asking a question like that around here too - may start a war.
|
# ? Dec 17, 2008 21:27 |
|
csammis posted:The answer is probably "no," but I thought I'd ask anyway: Is there a way to tell Visual Studio's linker, when it encounters a multiply defined symbol, which one is the "right" one? This may or may not work for, but here goes: 1. Make a .lib from a .def file with only AfxCallWndProc in it. The name of the .def will determine the name of the DLL it will link with. Example: code:
2. Make a DLL that exports a symbol named AfxCallWndProc, but declare the wrapper function as something else, and use the module definition file to redefine the symbol name. Example: code:
|
# ? Dec 18, 2008 04:47 |
|
Is there a way to (quasi-)portably detect how many cores the machine has, either at runtime or compile time? If this has been asked before, I apologize.
|
# ? Dec 18, 2008 06:45 |
|
theg sprank posted:Is there a way to (quasi-)portably detect how many cores the machine has, either at runtime or compile time? No.
|
# ? Dec 18, 2008 06:52 |
|
rjmccall posted:No. Indeed. Ever notice how top doesn't show the number of cores?
|
# ? Dec 18, 2008 06:56 |
|
theg sprank posted:Is there a way to (quasi-)portably detect how many cores the machine has, either at runtime or compile time? Yes, if your compiler supports OpenMP. Specifically omp_get_num_procs, as specified in the OpenMP specification. Of course that assumes you don't care about the difference between "core" and "processor".
|
# ? Dec 18, 2008 07:07 |
|
almostkorean posted:What's the best cross-platform software for C++ development besides Eclipse? (Although the best option is to use Visual Studio and cross-compile instead )
|
# ? Dec 18, 2008 10:42 |
|
csammis posted:Ever notice how top doesn't show the number of cores? Press '1'. top man page posted:<1> :Toggle_Single/Separate_Cpu_States -- On/Off
|
# ? Dec 18, 2008 10:42 |
|
csammis posted:Indeed. Ever notice how top doesn't show the number of cores? This probably horrible, but: Count the number of entries in /proc/cpuinfo . I noticed that when I ran the POV-Ray 1.37 benchmark script on two different machines, it launched, by default, 4 and 8 threads, respective of the number of cores on each machine (this was in linux). I don't know what it did to figure that out though. Question of my own: Does g++ 4.3.2 treat func(const string var); differently from func(const string &var);? Since it's const, I think it might pass by reference anyway? Thanks
|
# ? Dec 18, 2008 16:44 |
|
Bitruder posted:Question of my own:
|
# ? Dec 18, 2008 16:58 |
|
Bitruder posted:Question of my own: Almost definitely it will always pass by value if you say to pass by value unless it can determine that based on the implementation of the function they are functionally the same. The standard is very strict about this.
|
# ? Dec 18, 2008 17:52 |
|
if anybody is interested (you probably aren't), you can get the number of cores available quasi-portably among unixes with sysconf(_SC_NPROCESSORS_ONLN) (both function and macro from unistd.h)
|
# ? Dec 18, 2008 23:16 |
|
I need to create a temporary file and I would highly prefer to use streams on it. Does boost offer anything in this way? There's mkstemp but that doesn't use streams. I only really need this to work on Linux so it doesn't have to be 100% portable.
|
# ? Dec 19, 2008 17:17 |
|
|
# ? May 18, 2024 07:48 |
|
Bitruder posted:I only really need this to work on Linux so it doesn't have to be 100% portable. libstdc++ has an extension that might help you out there, if you are willing to take that total loss in portability: http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch38.html
|
# ? Dec 19, 2008 17:58 |