使用auto关键字初始化char数组

时间:2019-02-07 20:31:44

标签: c++ auto

我一直在测试auto关键字,发现对我来说很奇怪。每个字母占用1个字节(字符类型),并且使用自动说明符,无论如何(我没有测试很长的字符串),auto变量的大小均为4个字节。怎么解释?

char carray[] = "Some test output";
auto variable = "Some test output";

cout<<"carray: "<<sizeof(carray)<<endl;
cout<<"auto: "<<sizeof(variable);

2 个答案:

答案 0 :(得分:4)

由于数组到指针的衰减,import numpy as np import cv2 from utils import CFEVideoConf, image_resize cap = cv2.VideoCapture(0) save_path = 'saved-media/watermark.mp4' frames_per_seconds = 24 config = CFEVideoConf(cap, filepath=save_path, res='720p') out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims) img_path = 'images/logo/cfe-coffee.png' logo = cv2.imread(img_path, -1) watermark = image_resize(logo, height=50) #watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2GRAY) #watermark = cv2.cvtColor(watermark, cv2.COLOR_GRAY2BGR) watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2BGRA) # grayscale watermark # cv2.imshow('watermark', watermark) #print(watermark.shape) while(True): # Capture frame-by-frame ret, frame = cap.read() # print(frame[50, 150]) # numpy array # start_cord_x = 50 # start_cord_y = 150 # color = (255, 0, 0) #BGR 0-255 # stroke = 2 # w = 100 # h = 200 # end_cord_x = start_cord_x + w # end_cord_y = start_cord_y + h # cv2.rectangle(frame, (start_cord_x, start_cord_y), (end_cord_x, end_cord_y), color, stroke) # print(frame[start_cord_x:end_cord_x, start_cord_y:end_cord_y]) frame_h, frame_w, frame_c = frame.shape #print(frame.shape) # overlay with 4 channels BGR and Alpha overlay = np.zeros((frame_h, frame_w, 4), dtype='uint8') #overlay[100:250, 100:125] = (255, 255, 0, 1) # B, G, R, A #overlay[100:250, 150:255] = (0, 255, 0, 1) # B, G, R, A #overlay[start_y:end_y, start_x:end_x] = (B, G, R, A) #cv2.imshow("overlay", overlay) watermark_h, watermark_w, watermark_c = watermark.shape for i in range(0, watermark_h): for j in range(0, watermark_w): #print(watermark[i,j]) if watermark[i,j][3] != 0: #watermark[i, j] # RBGA offset = 10 h_offset = frame_h - watermark_h - offset w_offset = frame_w - watermark_w - offset overlay[h_offset + i, w_offset+ j] = watermark[i,j] cv2.addWeighted(overlay, 0.25, frame, 1.0, 0, frame) #frame.addimage(watermark) frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR) out.write(frame) # Display the resulting frame cv2.imshow('frame',frame) if cv2.waitKey(20) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() out.release() cv2.destroyAllWindows() 变成了variable(大小为4,这对我来说有点令人惊讶-您的平台是什么?)。

如果您希望const char*保留字符数组,则可以使用variable,例如

decltype(auto)

答案 1 :(得分:1)

auto variable = "Some test output";

这声明了一个指向字符的指针。该大小将与机器字的大小相同(在32位计算机上为4个字节,在64位计算机上通常为8个字节)。