LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 871|回复: 7

是否不可计算自定义namespace大小?

[复制链接]
发表于 2007-1-6 22:37:39 | 显示全部楼层 |阅读模式
#include <iostream>
namespace A {
    int a;
    int b;
    char c;
}

int main()
{
    std::cout<<sizeof(A)<<'\n';           //出错
    return(0);
}

请问有没有办法计算自定义namespace 大小?
发表于 2007-1-6 22:53:44 | 显示全部楼层
还从来没想过计算 namespace 的大小呢
namespace 是为了解决名字冲突的问题的,应该是不能计算大小的吧,而且 namespace 可以是不连续的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-1-7 10:59:32 | 显示全部楼层
但是,编译的时候,程序会给namespace A分配空间的啊!
怎么知道要分配多少空间,难道要“看”出来?
回复 支持 反对

使用道具 举报

发表于 2007-1-7 18:01:52 | 显示全部楼层
Post by hoho_zhao
但是,编译的时候,程序会给namespace A分配空间的啊!

如何得知 ?
回复 支持 反对

使用道具 举报

发表于 2007-1-7 19:48:01 | 显示全部楼层
你不用能Struct去理解namespace
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-1-7 20:26:27 | 显示全部楼层
哦,我还看不懂 -S 输出来的东东,待续。。。。
回复 支持 反对

使用道具 举报

发表于 2007-1-7 21:32:56 | 显示全部楼层
据我夜观星象,判断如下:
系统不会为namespace A 分配空间。
他们具有的是逻辑上的关系。
如果非要计算大小,那逻辑上应该是:>=(int a+int b+char c)的总和。
不过,我觉得 看一看g++的源代码,应该有结果....
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-1-7 22:22:18 | 显示全部楼层
//namespace.cpp
#include <iostream>

namespace A {
        int a=12;
        int b;
        char c;
}

using namespace A;

int main()
{
        return(0);
}


我把 g++ -S namespace.cpp 的代码贴出来了,谁看懂有没有分配空间?
        .file        "namespace.cpp"
        .section        .ctors,"aw",@progbits
        .align 4
        .long        _GLOBAL__I__ZN1A1aE
        .text
        .align 2
.globl main
        .type        main, @function
main:
.LFB1401:
        leal        4(%esp), %ecx
.LCFI0:
        andl        $-16, %esp
        pushl        -4(%ecx)
.LCFI1:
        pushl        %ebp
.LCFI2:
        movl        %esp, %ebp
.LCFI3:
        pushl        %ecx
.LCFI4:
        movl        $0, %eax
        popl        %ecx
        popl        %ebp
        leal        -4(%ecx), %esp
        ret
.LFE1401:
        .size        main, .-main
.globl __gxx_personality_v0
        .align 2
        .type        _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB1408:
        pushl        %ebp
.LCFI5:
        movl        %esp, %ebp
.LCFI6:
        subl        $24, %esp
.LCFI7:
        movl        %eax, -4(%ebp)
        movl        %edx, -8(%ebp)
        cmpl        $1, -4(%ebp)
        jne        .L7
        cmpl        $65535, -8(%ebp)
        jne        .L7
        movl        $_ZSt8__ioinit, (%esp)
        call        _ZNSt8ios_base4InitC1Ev
        movl        $__dso_handle, 8(%esp)
        movl        $0, 4(%esp)
        movl        $__tcf_0, (%esp)
        call        __cxa_atexit
.L7:
        leave
        ret
.LFE1408:
        .size        _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
        .align 2
        .type        _GLOBAL__I__ZN1A1aE, @function
_GLOBAL__I__ZN1A1aE:
.LFB1410:
        pushl        %ebp
.LCFI8:
        movl        %esp, %ebp
.LCFI9:
        subl        $8, %esp
.LCFI10:
        movl        $65535, %edx
        movl        $1, %eax
        call        _Z41__static_initialization_and_destruction_0ii
        leave
        ret
.LFE1410:
        .size        _GLOBAL__I__ZN1A1aE, .-_GLOBAL__I__ZN1A1aE
        .align 2
        .type        __tcf_0, @function
__tcf_0:
.LFB1409:
        pushl        %ebp
.LCFI11:
        movl        %esp, %ebp
.LCFI12:
        subl        $8, %esp
.LCFI13:
        movl        $_ZSt8__ioinit, (%esp)
        call        _ZNSt8ios_base4InitD1Ev
        leave
        ret
.LFE1409:
        .size        __tcf_0, .-__tcf_0
.globl _ZN1A1aE
        .data
        .align 4
        .type        _ZN1A1aE, @object
        .size        _ZN1A1aE, 4
_ZN1A1aE:
        .long        12
.globl _ZN1A1bE
        .bss
        .align 4
        .type        _ZN1A1bE, @object
        .size        _ZN1A1bE, 4
_ZN1A1bE:
        .zero        4
.globl _ZN1A1cE
        .type        _ZN1A1cE, @object
        .size        _ZN1A1cE, 1
_ZN1A1cE:
        .zero        1
        .local        _ZSt8__ioinit
        .comm        _ZSt8__ioinit,1,1
        .weakref        _Z20__gthrw_pthread_oncePiPFvvE,pthread_once
        .weakref        _Z27__gthrw_pthread_getspecificj,pthread_getspecific
        .weakref        _Z27__gthrw_pthread_setspecificjPKv,pthread_setspecific
        .weakref        _Z22__gthrw_pthread_createPmPK14pthread_attr_tPFPvS3_ES3_,pthread_create
        .weakref        _Z22__gthrw_pthread_cancelm,pthread_cancel
        .weakref        _Z26__gthrw_pthread_mutex_lockP15pthread_mutex_t,pthread_mutex_lock
        .weakref        _Z29__gthrw_pthread_mutex_trylockP15pthread_mutex_t,pthread_mutex_trylock
        .weakref        _Z28__gthrw_pthread_mutex_unlockP15pthread_mutex_t,pthread_mutex_unlock
        .weakref        _Z26__gthrw_pthread_mutex_initP15pthread_mutex_tPK19pthread_mutexattr_t,pthread_mutex_init
        .weakref        _Z26__gthrw_pthread_key_createPjPFvPvE,pthread_key_create
        .weakref        _Z26__gthrw_pthread_key_deletej,pthread_key_delete
        .weakref        _Z30__gthrw_pthread_mutexattr_initP19pthread_mutexattr_t,pthread_mutexattr_init
        .weakref        _Z33__gthrw_pthread_mutexattr_settypeP19pthread_mutexattr_ti,pthread_mutexattr_settype
        .weakref        _Z33__gthrw_pthread_mutexattr_destroyP19pthread_mutexattr_t,pthread_mutexattr_destroy
        .section        .eh_frame,"a",@progbits
.Lframe1:
        .long        .LECIE1-.LSCIE1
.LSCIE1:
        .long        0x0
        .byte        0x1
        .string        "zP"
        .uleb128 0x1
        .sleb128 -4
        .byte        0x8
        .uleb128 0x5
        .byte        0x0
        .long        __gxx_personality_v0
        .byte        0xc
        .uleb128 0x4
        .uleb128 0x4
        .byte        0x88
        .uleb128 0x1
        .align 4
.LECIE1:
.LSFDE3:
        .long        .LEFDE3-.LASFDE3
.LASFDE3:
        .long        .LASFDE3-.Lframe1
        .long        .LFB1408
        .long        .LFE1408-.LFB1408
        .uleb128 0x0
        .byte        0x4
        .long        .LCFI5-.LFB1408
        .byte        0xe
        .uleb128 0x8
        .byte        0x85
        .uleb128 0x2
        .byte        0x4
        .long        .LCFI6-.LCFI5
        .byte        0xd
        .uleb128 0x5
        .align 4
.LEFDE3:
.LSFDE5:
        .long        .LEFDE5-.LASFDE5
.LASFDE5:
        .long        .LASFDE5-.Lframe1
        .long        .LFB1410
        .long        .LFE1410-.LFB1410
        .uleb128 0x0
        .byte        0x4
        .long        .LCFI8-.LFB1410
        .byte        0xe
        .uleb128 0x8
        .byte        0x85
        .uleb128 0x2
        .byte        0x4
        .long        .LCFI9-.LCFI8
        .byte        0xd
        .uleb128 0x5
        .align 4
.LEFDE5:
.LSFDE7:
        .long        .LEFDE7-.LASFDE7
.LASFDE7:
        .long        .LASFDE7-.Lframe1
        .long        .LFB1409
        .long        .LFE1409-.LFB1409
        .uleb128 0x0
        .byte        0x4
        .long        .LCFI11-.LFB1409
        .byte        0xe
        .uleb128 0x8
        .byte        0x85
        .uleb128 0x2
        .byte        0x4
        .long        .LCFI12-.LCFI11
        .byte        0xd
        .uleb128 0x5
        .align 4
.LEFDE7:
        .ident        "GCC: (GNU) 4.1.0 20060304 (Red Hat 4.1.0-3)"
        .section        .note.GNU-stack,"",@progbits
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表