Is there any environment where “int” would cause struct padding?

后端 未结 5 1214
再見小時候
再見小時候 2021-01-07 18:30

Specifically, this came up in a discussion:

Memory consuption wise, is there a possibility that using a struct of two ints t

5条回答
  •  梦毁少年i
    2021-01-07 19:03

    I know this is not what you asked for, it's not in the spirit of your question (as you probably have standard layout classes in mind), but strictly answering just this part:

    Memory consuption wise, is there a possibility that using a struct of two ints take more memory than just two ints?

    the answer is kinda... yes:

    struct S
    {
        int a;
        int b;
    
        virtual ~S() = default;
    };
    

    with the pedantic note that C++ doesn't have structs, it has classes. struct is a keyword that introduces the declaration/definition of a class.

提交回复
热议问题