strange operator scope?

This is a discussion on strange operator scope? within the C++ Programming forums, part of the General Programming Boards category; I've defined a new new and delete operator in an include called "defines.h". However, suddenly all files of my project, ...

  1. #1
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314

    Question strange operator scope?

    I've defined a new new and delete operator in an include called "defines.h". However, suddenly all files of my project, even those that don't include "defines.h" use the new new and delete operators.
    This wouldn't be so much of a problem if not even the memory track functions I use "in" those operators would be affected.

    Is there any way how I can request that the default new and delete operators are used?
    [code]

    your code here....

    [/code]

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Maybe define your new operators under some namespace... that way you could always use the scope-resolution operator to control what version of new and delete you are trying to call.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Thanks for that idea!

    Did I understand this right that I'll have to change my exsiting code to use something like myspace::delete ?

    I tried std::delete before, but that gave me a compiler error. Sure namespaces apply to operators at all ?
    Also, does anyone know why my own delete and new operators are known to cpp's that don't even include the header? Is that correct C++ behaviour? Just wondering because it "seems" strange.
    [code]

    your code here....

    [/code]

  4. #4
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Well, it is not that strange. The operators are known of all C++ sources because it is in the standard.
    Therefore, when you define a new operator at global scope, it is used. The same applies to delete. They are global operators.
    Anyway, you should have got an error from your linker saying that the function is already defined in another object file.

  5. #5
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    mhm, thanks for your help.

    I kind of "solved" the problem with the help of global variables. I just set a boolean while I'm in the "new" new - operator and make it not track mem allocation while this boolean is set. So I prevent the tracking of allocations of my own memory tracker (which lead to infinite recursion).

    It's pretty ugly though and I better not even think of what happens if threads are used. Anyway, I'm getting accustomed to this kind of code in C
    [code]

    your code here....

    [/code]

  6. #6
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    I might have missed something, but why didn't you use a namespace after it was suggested?
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  7. #7
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by HybridM
    I might have missed something, but why didn't you use a namespace after it was suggested?
    Because I didn't want to change my existing code. These operators are merely debug tools. They keep track of allocations and log stuff like double deletions or undeleted memory. If I used namespaces, I'd need to change my entire code once I want to compile a "release" version.
    At the moment, I just need to include a file to enable debugging features.
    [code]

    your code here....

    [/code]

  8. #8
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    After some thinking, I'm not sure it would work if you put it in a namespace...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  2. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  3. Strange response from MSVC
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 04-17-2004, 07:40 AM
  4. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 01:56 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21