Thread: how to delete void* pointer

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Agreed. There's really no reason to use void* in C++ since we have templates.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by CornedBee View Post
    You have to deallocate with the operation that matches the allocation operation. When you allocate with new, you have to deallocate with delete. You have to match new[] with delete[]. You have to match malloc() with free(). And you have to match operator new with operator delete. Everything else is always undefined behaviour.

    In other words: if using ::operator delete() on the void* works on your platform for PODs, delete or delete[] will probably work just as well. Doesn't change the fact that both are undefined behaviour.

    The allocator objects used in the STL still follow this guideline. std::allocator uses ::operator new and ::operator delete to allocate and deallocate memory, then uses placement new and explicit destructor calls for construction and destruction.
    Ok, so you use ::operator new(size_t x) to allocate, and ::operator delete( void *ptr) to deallocate. There's still no reason to go back to using malloc and free.

    Also, I'm curious, do you happen to have a link that will explicitly corroborate the claim that something allocated by new cannot be deleted using a combination of a direct destructor calls and operator delete? Not just something that sais that new must match delete, but something that explicitly mentions direct calls to operators new and delete. Cause from a language design point of view, there is no reason to disallow that kind of behavior.
    Last edited by King Mir; 12-20-2007 at 05:45 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    You have to deallocate with the operation that matches the allocation operation.
    Which is the critical insight on this whole problem. If you could do this:

    Code:
    void *x;
    delete x;
    That would necessarily imply that you had allocated x like this:

    Code:
    x = new void;
    Which is obviously meaningless.

  4. #19
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Elysia View Post
    Agreed. There's really no reason to use void* in C++ since we have templates.
    You really like throwing out items from languages that you personally don't use, don't you?

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by King Mir View Post
    Ok, so you use :perator new(size_t x) to allocate, and :perator delete( void *ptr) to deallocate. There's still no reason to go back to using malloc and free.
    True. Not that it really matters.

    Also, I'm curious, do you happen to have a link that will explicitly corroborate the claim that something allocated by new cannot be deleted using a combination of a direct destructor calls and operator delete? Not just something that sais that new must match delete, but something that explicitly mentions direct calls to operators new and delete. Cause from a language design point of view, there is no reason to disallow that kind of behavior.
    Interesting question. I could point out how fragile a design that relies on this is, but that's not the point, is it?

    You can't do it without knowing the type, anyway. How would you call the destructor?

    I can't find anything that really says you can't do it for single objects. The standard is actually pretty explicit in describing not just the effects, but what is actually done. First, the destructor is called, then the deallocation function is called. Which is :perator delete for primitives. For non-primitives, it may be a custom deallocation function, so you'd have to be very careful there.

    For arrays, I lack the hard evidence to back me up, but I'm quite sure that the trick would fail on MS compilers. The reason is this: the compiler stores the length of the array before its start and overallocates to hold the count. Which means that the address returned by the allocation function (which is the one you must pass to the deallocation function) isn't necessarily the same as the address returned by the new operator (the one you actually have). Because there is no way you can reliably know the implementation-specific offset between those two addresses, you can't portably call the deallocation function yourself.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    You really like throwing out items from languages that you personally don't use, don't you?
    I have to admit there's not much use for void* in C++. Even in C, the only benefit of void* was that any pointer type could automatically convert from/to void* without a cast. In C++ this feature has been removed, which renders the whole point of void*, well... "void."

  7. #22
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Question.

    If a feature has been made void, but there is no such type in C++ as void, does the feature really exist?

  8. #23
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by MacGyver View Post
    Question.

    If a feature has been made void, but there is no such type in C++ as void, does the feature really exist?
    Actually void is a type in C++. It just can't be instantiated or referenced. You can pass void as a template parameter, for example.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MacGyver View Post
    You really like throwing out items from languages that you personally don't use, don't you?
    I like to avoid things that usually have little use and usually invokes undefined behavior and removes type safety and checking. Remember, I'm a C++ dev, and I do so loathe C's behavior of allow the most of absurd things like calling a function without a prototype, to pass in a non-pointer where a pointer is required, etc.

    It wouldn't surprise me if you could actually dereference or get the type type of a void* pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by King Mir View Post
    Actually void is a type in C++. It just can't be instantiated or referenced. You can pass void as a template parameter, for example.
    Programmers that pedantically tear apart jokes should be virtually shot with the proverbial C/C++ gun.

    Quote Originally Posted by Elysia View Post
    I like to avoid things that usually have little use and usually invokes undefined behavior and removes type safety and checking. Remember, I'm a C++ dev, and I do so loathe C's behavior of allow the most of absurd things like calling a function without a prototype, to pass in a non-pointer where a pointer is required, etc.
    C99 doesn't allow you to use a function without a prototype afaik. I have no idea what you mean by saying that you can "pass in a non-pointer where a pointer is required". For my own amusement, I wish you had provided more details.

    Quote Originally Posted by Elysia View Post
    It wouldn't surprise me if you could actually dereference or get the type type of a void* pointer.
    This is a non-sensical statement if referring to C.

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MacGyver View Post
    C99 doesn't allow you to use a function without a prototype afaik. I have no idea what you mean by saying that you can "pass in a non-pointer where a pointer is required". For my own amusement, I wish you had provided more details.
    Sure, consider:
    Code:
    void foo(int* n)
    {
    	*n = 70;
    }
    
    int main()
    {
    	int n = 0;
    	foo(n); /* Legal in C! */
    	return 0;
    }
    This is a non-sensical statement if referring to C.
    I always refer to C with such things - it's illegal in C++, so what else could it be?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Elysia View Post
    Sure, consider:
    Code:
    void foo(int* n)
    {
    	*n = 70;
    }
    
    int main()
    {
    	int n = 0;
    	foo(n); /* Legal in C! */
    	return 0;
    }
    Code:
    : In function `main':
    :9: warning: passing arg 1 of `foo' makes pointer from integer without a cast
    Try again.

    Quote Originally Posted by Elysia View Post
    I always refer to C with such things - it's illegal in C++, so what else could it be?
    This just displays your ignorance of C.

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MacGyver View Post
    Code:
    : In function `main':
    :9: warning: passing arg 1 of `foo' makes pointer from integer without a cast
    Try again.
    Yes, that's a WARNING, but not a compile ERROR, is it? Then it compiles fine and runs.

    This just displays your ignorance of C.
    Well, forgive me for not liking C!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Elysia View Post
    Yes, that's a WARNING, but not a compile ERROR, is it? Then it compiles fine and runs.
    Oh my. So everything that generates a warning instead of an error is considered legal C?

    OK, so since this doesn't make the compiler barf, I guess it's legal C++ on a 32-bit Windows XP machine.

    Code:
    #include <iostream>
    
    int main(int a, double b, char c)
    {
    	std::cout << "Hello world!" << std::endl;
    	std::cout << "a = " << a << std::endl;
    	std::cout << "b = " << b << std::endl;
    	std::cout << "c = " << c << std::endl;
    	
    	return 0;
    }
    Oh, look. It even runs without crashing.

    Code:
    Hello world!
    a = 1
    b = 1.62266e-307
    c =
    Quote Originally Posted by Elysia View Post
    Well, forgive me for not liking C!
    This has nothing to do with liking C, and I wouldn't demand it of you. I just hate blatant ignorance mingled with silly statements. Do you really think C allows for undefined behavior in dereferencing void pointers or were you just making it up to emphasize your disdain for the language?

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MacGyver View Post
    Oh my. So everything that generates a warning instead of an error is considered legal C?
    Technically yes, since most newbies in C tend to ignore those warnings and run the code anyway.

    OK, so since this doesn't make the compiler barf, I guess it's legal C++ on a 32-bit Windows XP machine.

    Code:
    #include <iostream>
    
    int main(int a, double b, char c)
    {
    	std::cout << "Hello world!" << std::endl;
    	std::cout << "a = " << a << std::endl;
    	std::cout << "b = " << b << std::endl;
    	std::cout << "c = " << c << std::endl;
    	
    	return 0;
    }
    Oh, look. It even runs without crashing.

    Code:
    Hello world!
    a = 1
    b = 1.62266e-307
    c =
    Sure it's legal, but that doesn't mean it's good.
    main doesn't have a prototype and doesn't use one, so you could basically define as whatever you want.

    Do you really think C allows for undefined behavior in dereferencing void pointers or were you just making it up to emphasize your disdain for the language?
    I wouldn't know if it would allow or not, but since it allows silly things like passing a non-pointer to where a pointer is expected, do you really think I think of C as safe? Passing a non-pointer where a pointer is expected - crash right away and it's so easy to do it, rather than in C++, where you'd get a compile error.

    And even though void is undefined, the compiler could just assume you're dereferencing int*, just as it assumes every function returns int if it has no prototype.

    I didn't try - no, I admit that, and I also admit that I didn't know, but I wouldn't be surprised if it WAS allowed. Whether it is or not, is an entirely different question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Inventory tracking of dynamically allocated items
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 07-23-2006, 05:39 PM
  3. delete pointer inside ?
    By black in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2004, 12:19 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM