Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1096371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:16:31+00:00 2026-05-17T00:16:31+00:00

I am working on some strange piece of code ,For me its not good

  • 0

I am working on some strange piece of code ,For me its not good piece of code.

PIP_ADAPTER_INFO pAdapterInfo=(PIP_ADAPTER_INFO)new  
                               char[sizeof(IP_IP_ADAPTER_INFO)];

.
.
.
delete []pAdapterInfo;

Here PIP_ADAPTER_INFO is pointer to struct IP_IP_ADAPTER_INFO , size of IP_IP_ADAPTER_INFO is 640.

I was expecting crash in delete []pAdapterInfo call.But there is no crash.I wrote an small
test code.

        class TestClass
        {

        public:
        /*  TestClass()
            {
            }
            ~TestClass()
            {
            }*/

        public:
            int array[10];
        };

        int main (int ac, char **av)

        {
            TestClass *myptr=(TestClass*) new char[10];
            delete []myptr;
            return 0;
        }

What i see :

  1. If i un-comment the c’tor and d’tor, test code crashes (assert fails)
  2. If i keep it commented nothing fails.

Even if i see disassemble , it is different in both case above

/*****************************************************************/
/********Compiler provided c'tor and d'tor ***********************/
/*****************************************************************/
28:       TestClass *myptr=(TestClass*) new char[10];
00401268   push        0Ah
0040126A   call        operator new (004082d0)
0040126F   add         esp,4
00401272   mov         dword ptr [ebp-8],eax
00401275   mov         eax,dword ptr [ebp-8]
00401278   mov         dword ptr [ebp-4],eax
29:       delete []myptr;
0040127B   mov         ecx,dword ptr [ebp-4]
0040127E   mov         dword ptr [ebp-0Ch],ecx
00401281   mov         edx,dword ptr [ebp-0Ch]
00401284   push        edx
00401285   call        operator delete (004060d0)
0040128A   add         esp,4
30:
/*****************************************************************/
/********User provided c'tor and d'tor ***********************/
/*****************************************************************/
28:       TestClass *myptr=(TestClass*) new char[10];

28:       TestClass *myptr=(TestClass*) new char[10];
00401278   push        0Ah
0040127A   call        operator new (004083e0)
0040127F   add         esp,4
00401282   mov         dword ptr [ebp-8],eax
00401285   mov         eax,dword ptr [ebp-8]
00401288   mov         dword ptr [ebp-4],eax
29:       delete []myptr;
0040128B   mov         ecx,dword ptr [ebp-4]
0040128E   mov         dword ptr [ebp-10h],ecx
00401291   mov         edx,dword ptr [ebp-10h]
00401294   mov         dword ptr [ebp-0Ch],edx
00401297   cmp         dword ptr [ebp-0Ch],0
0040129B   je          main+4Ch (004012ac)
0040129D   push        3
0040129F   mov         ecx,dword ptr [ebp-0Ch]
004012A2   call        @ILT+0(TestClass::`vector deleting destructor') (00401005)
004012A7   mov         dword ptr [ebp-14h],eax
004012AA   jmp         main+53h (004012b3)
004012AC   mov         dword ptr [ebp-14h],0

Please help me with your expertise to learn this feature of C++ .

Thanks in advance.

Sat

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:16:31+00:00Added an answer on May 17, 2026 at 12:16 am

    I’m assuming here that by IP_IP_ADAPTER_INFO you mean Windows’ IP_ADAPTER_INFO structure. Even if not, the gist of this is the same: your code is leading to undefined behaviour, and it’s the fault of whoever wrote it. Fix it immediately.

    You allocated an array of char with new, but then free that memory as if it were an array of IP_ADAPTER_INFO. C++ doesn’t know that you’re lying to it, so it goes and tries to treat your char array as an IP_ADAPTER_INFO array, and then dies horribly when it finds out the awful truth.

    Now, this works sometimes because VC++ records enough info about the allocated memory that delete[] doesn’t care about the pointer’s type, but this is evil evil wrong bad illegal get-you-taken-out-back-and-shot code.

    It may work on your particular compiler, but that’s entirely a fluke. You should instead be doing:

    PIP_ADAPTER_INFO pAdapterInfo = new IP_ADAPTER_INFO;
    //DoSomethingToAdapterInfo(pAdapterInfo);
    delete pAdapterInfo;
    

    But even then, unless you need to hold onto that structure in a global scope, which itself points to bad design, you really shouldn’t be using new and delete here at all. You should be doing something closer to this:

    IP_ADAPTER_INFO adapterInfo;
    //DoSomethingToAdapterInfo(&adapterInfo);
    

    Letting C++ handle allocation and deletion (on the stack) for you. If you need to return the structure, return it rather than a pointer to it (so your caller doesn’t need to worry about memory management.)

    If there’s some obscure or unique reason why you are using heap allocation instead of stack allocation, then you may be justified in doing so–but even then, casting new char[...] to PIP_ADAPTER_INFO is bad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://churchsafeplan.com/overview.php?content=0201#intro For some strange, strange reason, the function accordian is not working. Seems as
For some strange reason, the getline() function is not working as it should. I
I've been having some strange errors lately. I have a working install of Git,
I'm working on a heavily data-bound Win.Forms application where I've found some strange behavior.
I'm trying out some bean validation and I'm stumbling upon 'strange' behavior. I'm working
I'm working some code that inserts csv rows into an SQLite database using Python.
I've been re-working some code due to a memory leak. The code is part
Working on some code for a machine problem; we've just started working with pointers,
After working on some test code for open ID authentication, I had a break
Whilst working on some generally horrible Javascript code this morning, I came across the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.