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 8867693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:06:18+00:00 2026-06-14T17:06:18+00:00

I would like to know why in the following code the difference between pointers

  • 0

I would like to know why in the following code the difference between pointers
aptr and amemTab is not equal the size of allocated array (10*sizeof(A)) but 64 bytes (sizeof(A) is 4).

in the debug mode:

aptr 0x00395e38

amemTab 0x00395e78

Win XP Home Edition,MSVS2010,x86 Intel 1.86

I guess it has something to do with padding? (I haven’t deleted the code of base and derived class, as I want to show exactly what I am testing, but it is redundant here,
I am talking only about two lines:

A * aptr=static_cast<A*>(amem);
void * amemTab= operator new[](10*sizeof(A));

my full example:

// exercise
//

#include "stdafx.h"
#include <algorithm>

void func(const int &i){printf("%d\n",i);}

class A{
public:
    int i;
};
class B{
public:
    int i;
private:
    int j;
};
class base{
public: 
    void f(void){printf("base f not virtual\n");}
    virtual void g(void){printf("base g virtual\n");}
    void h(void){printf("base h not virtual\n\n");}
    int i_;
    base():i_(123){}
    base(int):i_(12345){}
};
class derived:public base{
public: 
    void f(void){printf("derived f not virtual\n");}
    virtual void g(void){printf("derived g virtual\n");}
};

int _tmain(int argc, _TCHAR* argv[])
{
    int ij;
    A a;/*a.i is not initialized*/
    A * aprimprim=new A;/*i is not initialized (but ctor has been called)*/
    A aprim=A();/*aprim.i is 0 initialized as it is public variable 
                and A has only public part (A is POD type) and () is written*/
    A * ap=new A();/*int is 0 initialized*/
    B b;/*b.i is not initialized and b.j is not initialized*/
    B bprim=B();/*bprim.i is not initialized and bprim.j is not initialized
                as A has public AND also private part*/
    B * bp=new B();/*ints are both 0 initialized*/

    void * amem= operator new (sizeof(A));/*uninitialized memory, only allocate*/
    A * aptr=static_cast<A*>(amem);//cast pointer to void to pointer to A

    void * amemTab= operator new[](10*sizeof(A));/*uninitialized memory, only 
                                            allocate for 10 objects of A size*/
    A * aptrtab=static_cast<A*>(amemTab);/*cast pointer to void to pointer to 
                                         A. now it is possible to iterate through
                                         this area of indexed memory:*/
    for(int i=0;i<10;i++){
        new(&aptrtab[i])A();//initialize each A object 
    }
    int s=sizeof(A);
    /*------------------------------*/
    int myarray[5];/*ints are uninitialized*/
    *(1+myarray)=13;/*pointer addition is commutative*/
    2[myarray]=4;/*subscript operator is commutative*/

    std::for_each(myarray,myarray+5,func);

    /*---------------*/
    int *what_here=const_cast<int*>(myarray-6600);
    printf("what_here: %d\n",*what_here);

    return 0;
}
  • 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-06-14T17:06:19+00:00Added an answer on June 14, 2026 at 5:06 pm

    Strictly speaking, nothing definite can be said about the relationship between pointers returned by two consecutive heap allocations. It would not be inconceivable for the allocator to return pointers from two completely different regions of memory (for example, it could use different sub-heaps depending on how many bytes are requested).

    What is likely happening in your case is that:

    1. your allocator manages memory in chunks larger than one byte;
    2. the allocator uses a few bytes adjacent to the allocated memory for its internal data structures.

    Both of these incur overheads.

    Also, memory allocations have to satisfy certain alignment requirements. This can in general lead to further overheads.

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

Sidebar

Related Questions

I would like to know if the following code are valid. The original intension
I would like to know if the following code would be a good pattern
I would like to know if it is safe to use the following code
Basically I would like to know the difference between Int32^ i = gcnew Int32();
I would like to know following: I added the folder Graphics into my project
I would like to know if the following is possible in any of the
i would like to know whether the following query is optimized? select SUM(payments.paid_amt) as
I would like to know if the following is possible in Scala (but I
Being new to OpenCL i would like to know if the following scenario is
I would like to know what will happen in the following hypothetical situation. Let's

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.