比方说,我有一个struct RGB
,我想创建struct RGBA
,继承RGB
:
struct RGB {
unsigned char r;
unsigned char g;
unsigned char b;
};
struct RGBA: RGB {
unsigned char a;
};
Run Code Online (Sandbox Code Playgroud)
两者都将用于读取未压缩的图像数据:
RGBA *pixel=static_cast<RGBA *>(image->uncompressed_data);
Run Code Online (Sandbox Code Playgroud)
问题:关于内存布局,这样安全struct RGBA
吗?有人保证:
unsigned char a
之后RGB struct
(不是之前)struct RGB
和参数来自struct RGBA
?会有#pragma pack
帮助吗?这都是关于继承期间的内存布局.
我不明白C++ 11大括号初始化规则是如何工作的.有这个代码:
struct Position_pod {
int x,y,z;
};
class Position {
public:
Position(int x=0, int y=0, int z=0):x(x),y(y),z(z){}
int x,y,z;
};
struct text_descriptor {
int id;
Position_pod pos;
const int &constNum;
};
struct text_descriptor td[3] = {
{0, {465,223}, 123},
{1, {465,262}, 123},
};
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注意,该数组声明有3个元素,但只提供了2个初始值设定项.
然而,它编译没有错误,这听起来很奇怪,因为最后一个数组元素的引用成员将是未初始化的.实际上,它具有NULL值:
(gdb) p td[2].constNum
$2 = (const int &) @0x0: <error reading variable>
Run Code Online (Sandbox Code Playgroud)
现在是"神奇":我将Position_pod改为Position
struct text_descriptor {
int id;
Position_pod pos;
const int &constNum;
};
Run Code Online (Sandbox Code Playgroud)
成为这个:
struct text_descriptor { …
Run Code Online (Sandbox Code Playgroud) 我有以下内容:
void function_1()
{
//something (taking only 1 line)
}
->INSERT LINE HERE<-
//more code
Run Code Online (Sandbox Code Playgroud)
使用sed,我想在INSERT LINE HERE标签处插入行.最简单的方法应该是:
但是没有一种已知的sed选项可以胜任这项工作.
sed '/function_1/,3a new_text
Run Code Online (Sandbox Code Playgroud)
在'function_1'之后插入new_text
sed '/function_1/,+3a new_text
Run Code Online (Sandbox Code Playgroud)
在'function_1'后面的后3行中的每一行之后插入new_text
sed '/function_1/N;N;N; a new_text
Run Code Online (Sandbox Code Playgroud)
在多个位置插入new_text,与模式无关
谢谢.
我遇到了一个问题,将结构成员传递给模板函数。该函数的目标是获取成员的地址和大小。这是一个简单的例子:
这是结构。它具有打包属性。
struct TestStruct {
unsigned char elem1;
unsigned char elem2;
uint64_t elem3;
char buf[10000];
int elem4;
unsigned char elem5;
}
__attribute__ ((packed));
Run Code Online (Sandbox Code Playgroud)
这是模板函数,它应该获取成员的地址
template<typename T>
void addData(const T &val)
{
printf ("address inside func: %p \n",&val);
}
int main(int argc, char *argv[])
{
TestStruct testdata;
testdata.elem4 = 0;
printf ("struct address is: %p \n",&testdata);
printf ("elem4 address is: %p \n",&testdata.elem4);
addData(testdata.elem4);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题:当 属性((packed)); 设置(如在示例中)模板函数收到错误的成员地址:
输出:
struct address is: 0x7fff735bb4e0
elem4 address is: 0x7fff735bdbfa
address inside …
Run Code Online (Sandbox Code Playgroud) 因为我想跟踪一些变量以便看到它们如何变化,我想创建一个函数,它接收一个格式字符串,具体取决于变量的类型和指向值的指针.我希望,在格式字符串的帮助下,printf将正确地确定值.实际上它有效,但有一个例外 - 浮点值无法正确打印.
这是我的代码:
#include <stdio.h>
void pprint (char* fmtstr, void* p)
{
printf(fmtstr,*(long double *)p);
}
int main (int argc, char **argv)
{
char cval = 64;
int ival = -534;
unsigned int iuval = 535;
float fval = 534.64;
double dval = 53432.1;
long double ldval = 534321234.134567;
long long lval = -654321;
unsigned long long luval = 7654321;
pprint ("char: %hhd\n",&cval);
pprint ("int: %d\n",&ival);
pprint ("uint: %u\n",&iuval);
pprint ("float: %f\n",&fval);
pprint ("double: %f\n",&dval);
pprint ("long double: %Lf\n",&ldval); …
Run Code Online (Sandbox Code Playgroud) 以下示例演示了此问题:
#include <cstdio>
int main()
{
unsigned int remaining=1;
goto loop;
while(remaining) {
unsigned char tmp[remaining];
printf("&tmp: %p\n",tmp);
loop:
remaining = 512;//or something else;
}
}
Run Code Online (Sandbox Code Playgroud)
最初,"剩余"变量的初始化有点长,我曾经goto
在一行初始化它.但是,现在这个例子给出了printf
线路上的分段错误.
看起来数组没有正确初始化.
即使gdb也无法打印tmp数组的地址:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004005b8 in main () at test.cpp:11
11 printf("&tmp: %p\n",tmp);
(gdb) p tmp
$1 = 0xfffffffffffffe00 <error: Cannot access memory at address 0xfffffffffffffe00>
Run Code Online (Sandbox Code Playgroud)
我的gcc版本:
gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Run Code Online (Sandbox Code Playgroud)
编译:
g++ -o testc test.cpp
Run Code Online (Sandbox Code Playgroud)
如果我删除goto,或用固定数组替换可变参数数组,分段错误就消失了.实际上发生了什么?
这是一个gcc bug吗?如果goto
不允许组合和变量数组,那么应该有警告吗?
我想知道为什么下面的例子不能编译:我有一个 ImageContainer 类,它私下继承自 Image(因此,它的用户不应该知道它继承自 Image)。此类还包含图像列表并具有 AddImage 函数。
class ImageContainer: private Image {
public:
ImageContainer (){};
void addImage (const Image &img){
//adds image to the container
};
std::vector<Image> images;
};
class DerivedImageContainer: public ImageContainer {
public:
void init () {
addImage (Image (background, Position(960, 533), Align::MiddleCenter));
}
};
Run Code Online (Sandbox Code Playgroud)
我想从派生类 (DerivedImageContainer) 调用 addImage 函数,将新图像添加到列表中。
我很惊讶地看到这不能编译。错误是:
error: ‘class Image Image::Image’ is inaccessible within this context
Run Code Online (Sandbox Code Playgroud)
我正在创建一个与 ImageContainer 的基类完全无关的 Image 而且我什至没有接触 ImageContaner 的内部内容,我只是调用了一个公共函数。为什么编译器会抱怨?
这是否意味着,在派生函数中,我们不能在类层次结构中的某处使用与私有继承类具有相同类型的成员?
它不适用于 g++ 4.8.2 和 g++ 7.5.0
我无法理解这个三元运算符的转换逻辑(这里是一个例子):
#include <iostream>
#include <typeinfo>
#include <unistd.h>
#include <cxxabi.h>
#include <climits>
template<typename T>
struct singletime
{
private:
T value;
public:
T& operator()() {return this->value;}
operator const T& () const {return value;}
unsigned char flag_needed_for_all_types;
};
static void getvalue1 (uint64_t value, const char *call)
{
std::cout << call << ": \t" << value << std::endl << std::endl;
}
#define getvalue(x, str) \
std::cout << typeid(x).name() << std::endl; \
getvalue1(x, str);
int main (int argc, char *argv[])
{
bool flag = true; …
Run Code Online (Sandbox Code Playgroud)