根据下面的代码,我得到一个字符指针的大小是 8 个字节。然而这个site char 指针的大小为 1 个字节。
#include <stdio.h>
int main(void ){
char *a = "saher asd asd asldasdas;daksd ahwal";
printf(" nSize = %d \n", sizeof(a));
return 0;
}
总是这样吗?我正在为我正在实现的一个简单数据库编写一个连接器,并希望将 mysql 的 TEXT 字段读入我的数据库。由于 TEXT 具有可变大小,我想知道我的列类型/元数据是否可以具有 8 个字节的固定大小,我将指针存储在内存中的字符串 (char *) 中?
最佳答案
per the following code, I get the size of a character pointer is 8 bytes. Yet this site has a size of 1 byte for the char pointer.
它是实现定义的。在 64 位 Intel 系统上通常为 8 个,在 32 位 Intel 系统上通常为 4 个。不要依赖它的任何特定尺寸。
I am writing a connector for a simple database I am implementing and want to read TEXT field of mysql into my database. Since TEXT has variable size, I was wondering if my column can have a fixed size of 8 bytes where I store the pointer in memory to the string (char *)?
将指针存储到数据库的内存中毫无意义。数据库用于持久数据。另一方面,存储在内存中的数据很容易在进程退出(或系统重启)时消失。
关于c++ - C/C++ 中 char *(字符指针)的大小会有所不同吗? - 用于数据库列固定大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22056689/