在C ++中,内存中的类函数放在哪里? [英] In C++, where in memory are class functions put?

查看:169
本文介绍了在C ++中,内存中的类函数放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过创建一个大的对象数组来理解什么样的内存命中。我知道每个对象 - 创建时 - 将在HEAP中为成员变量赋予空间,我认为属于该类型对象的每个函数的所有代码都永久存在于内存的代码段中。



是这样吗?



所以如果我在C ++中创建100个对象,我可以估计我需要空间为对象拥有的所有成员变量乘以100(这里可能的对齐问题),然后我需要空间在代码段的单个副本的代码为该类型的对象的每个成员函数(不是



什么是虚拟函数,多态性,继承因素?关于动态链接库的对象?我假设dll有自己的堆栈,堆,代码和数据段。



简单的例子(可能在语法上不正确):

  //父类
class Bar
{
public:
Bar(){};
〜Bar(){};

//纯虚函数
virtual void doSomething()= 0;

protected:
//受保护的变量
int mProtectedVar;
}

//我们的对象类,我们将创建多个实例
class Foo:public Bar
{
public:
Foo(){};
〜Foo(){};

//实现纯虚函数
void doSomething(){mPrivate = 0; }

//一对公共函数
int getPrivateVar(){return mPrivate; }
void setPrivateVar(int v){mPrivate = v; }

//一对公共变量
int mPublicVar;
char mPublicVar2;

private:
//一对私有变量
int mPrivate;
char mPrivateVar2;
}

关于多少内存应该动态分配Foo类型的对象包括空间代码和所有变量?

解决方案

每个对象创建时,成员变量。您创建的每个对象将为其成员变量在某处占用一些非零空间,但在哪里取决于如何分配对象本身。如果对象有自动(堆栈)分配,那么它的数据成员也是如此。如果对象在自由存储(heap)上分配,那么它的数据成员也是如此。毕竟,一个对象而不是其数据成员的分配是什么?



如果堆栈分配的对象包含指针或其他类型,然后用于



对于具有虚函数的对象,每个对象都有一个分配为如果它是类中的一个明确声明的数据成员。



对于成员函数,这些代码可能与自由功能代码没有区别它进入可执行映像。毕竟,一个成员函数基本上是一个自由函数,其隐式this指针作为它的第一个参数。



继承不会改变任何东西。



我不知道你对DLL有什么意思,得到自己的堆栈。 DLL不是一个程序,并且不需要堆栈(或堆),因为它分配的对象总是在一个具有自己的堆栈和堆的程序的上下文中分配。在DLL中会有代码(文本)和数据段有意义,虽然我不是在Windows上执行这样的事情(我假设你使用给定的术语)的专家。


I'm trying to understand what kind of memory hit I'll incur by creating a large array of objects. I know that each object - when created - will be given space in the HEAP for member variables, and I think that all the code for every function that belongs to that type of object exists in the code segment in memory - permanently.

Is that right?

So if I create 100 objects in C++, I can estimate that I will need space for all the member variables that object owns multiplied by 100 (possible alignment issues here), and then I need space in the code segment for a single copy of the code for each member function for that type of object( not 100 copies of the code ).

Do virtual functions, polymorphism, inheritance factor into this somehow?

What about objects from dynamically linked libraries? I assume dlls get their own stack, heap, code and data segments.

Simple example (may not be syntactically correct):

// parent class
class Bar
{
public:
    Bar()  {};
    ~Bar() {};

    // pure virtual function
    virtual void doSomething() = 0;

protected:
    // a protected variable
    int mProtectedVar;
}

// our object class that we'll create multiple instances of
class Foo : public Bar
{
public:
    Foo()  {};
    ~Foo() {};

    // implement pure virtual function
    void doSomething()          { mPrivate = 0; }

    // a couple public functions
    int getPrivateVar()         { return mPrivate; }
    void setPrivateVar(int v)   { mPrivate = v; }

    // a couple public variables
    int mPublicVar;
    char mPublicVar2;

private:
    // a couple private variables
    int mPrivate;
    char mPrivateVar2;        
}

About how much memory should 100 dynamically allocated objects of type Foo take including room for the code and all variables?

解决方案

It's not necessarily true that "each object - when created - will be given space in the HEAP for member variables". Each object you create will take some nonzero space somewhere for its member variables, but where is up to how you allocate the object itself. If the object has automatic (stack) allocation, so too will its data members. If the object is allocated on the free store (heap), so too will be its data members. After all, what is the allocation of an object other than that of its data members?

If a stack-allocated object contains a pointer or other type which is then used to allocate on the heap, that allocation will occur on the heap regardless of where the object itself was created.

For objects with virtual functions, each will have a vtable pointer allocated as if it were an explicitly-declared data member within the class.

As for member functions, the code for those is likely no different from free-function code in terms of where it goes in the executable image. After all, a member function is basically a free function with an implicit "this" pointer as its first argument.

Inheritance doesn't change much of anything.

I'm not sure what you mean about DLLs getting their own stack. A DLL is not a program, and should have no need for a stack (or heap), as objects it allocates are always allocated in the context of a program which has its own stack and heap. That there would be code (text) and data segments in a DLL does make sense, though I am not expert in the implementation of such things on Windows (which I assume you're using given your terminology).

这篇关于在C ++中,内存中的类函数放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆