char * d[] 比 char d[][] 更节省空间吗?

john 2015-03-13 03:34:20
一个很大的字符串表,例如字典。用如下的两个说明形式表示,哪一个更节省空间呢?

二维数组形式:
char dictionary[][MAX_STRING_LENGTH] =
{
"a",
"b" ,
"zone",
"zymurgy"
};

指针数组形式:
char * dictionary[] =
{
"a",
"b" ,
"zone",
"zymurgy"
};

从编译之后文件的大小看,似乎是二维数组形式更节省空间。当然 MAX_STRING_LENGTH 需要恰当选取。
真的这样的吗?实际情况到底哪一个表示形式更节省空间?哪一个表示形式处理速度更快?
...全文
284 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
john 2015-03-16
  • 打赏
  • 举报
回复
最终的EXE文件(debug/release),没有出现背离的现象。似乎表明,一维指针的方法,更节省空间。
jiht594 2015-03-16
  • 打赏
  • 举报
回复
引用 6 楼 JohnPhan 的回复:
各位老师说的都对。但编译出的obj文件却显示出相反的结果。包含指针数组 extern const wchar_t * const dict2[] = { L"a", L"b", L"c", 的obj文件非常大。16132KB。而且编译时还提示(VS2013)文件过大,需要添加/bigobj编译选项。 而保存同样字典数据,包含用二维数组字符串表示的文件obj,文件大小仅仅为3617KB。 部分的参考代码: #define LATIN_WORD_LENGTH_MAX (25) 文件1: extern const wchar_t dict1[][LATIN_WORD_LENGTH_MAX] = { L"a", L"b", L"c", ............................................................. L"www.worldofwarcraft.com", L"www.youtube.com", }; unsigned long dict1_total() { return sizeof(dict1) / sizeof(dict1[0]); } 文件2: //fatal error C1128: number of sections exceeded object file format limit : compile with /bigobj extern const wchar_t * const dict2[] = { L"a", L"b", L"c", ............................................................. L"www.worldofwarcraft.com", L"www.youtube.com", }; unsigned long dict2_total() { return sizeof(dict2) / sizeof(dict2[0]); } 主程序: int _tmain(int argc, _TCHAR* argv[]) { unsigned long dict_total = dict1_total(); // =73998 unsigned long total = 0; cout << "-----------------------------------" << endl; cout << "this is dictionary1" << endl; cout << " total : " << dict_total << endl; for (unsigned long i = 0; i < dict_total; ++i) { total += LATIN_WORD_LENGTH_MAX; } cout << " space sum : " << total << endl; cout << "-----------------------------------" << endl; total = 0; dict_total = dict2_total(); // = 73998 cout << "this is dictionary2" << endl; cout << " total : " << dict_total << endl; for (unsigned long i = 0; i < dict_total; ++i) { total += sizeof(dict2[i]); total += 2 * (wcslen(dict2[i] + 1 )); } cout << " space sum : " << total << endl; cout << "-----------------------------------" << endl; system("pause"); return 0; } 程序结果与各位老师一致,显示二维数组表示方法,占用更多的空间: ----------------------------------- this is dictionary1 total : 73998 space sum : 1849950 ----------------------------------- this is dictionary2 total : 73998 space sum : 1383970 ----------------------------------- 但obj文件大小: dict1 : 3617 KB (用二维数组) dict2 :16132 KB (用一维数组) 请各位老师帮助解释一下这种背离的现象。
我不太清楚影响obj大小的是什么. 但是obj是存在硬盘上的, 和运行时存在内存的数组肯定没关系. 而且我不信就因为你贴的代码, 差了10兆的大小.
john 2015-03-16
  • 打赏
  • 举报
回复
各位老师说的都对。但编译出的obj文件却显示出相反的结果。包含指针数组 extern const wchar_t * const dict2[] = { L"a", L"b", L"c", 的obj文件非常大。16132KB。而且编译时还提示(VS2013)文件过大,需要添加/bigobj编译选项。 而保存同样字典数据,包含用二维数组字符串表示的文件obj,文件大小仅仅为3617KB。 部分的参考代码: #define LATIN_WORD_LENGTH_MAX (25) 文件1: extern const wchar_t dict1[][LATIN_WORD_LENGTH_MAX] = { L"a", L"b", L"c", ............................................................. L"www.worldofwarcraft.com", L"www.youtube.com", }; unsigned long dict1_total() { return sizeof(dict1) / sizeof(dict1[0]); } 文件2: //fatal error C1128: number of sections exceeded object file format limit : compile with /bigobj extern const wchar_t * const dict2[] = { L"a", L"b", L"c", ............................................................. L"www.worldofwarcraft.com", L"www.youtube.com", }; unsigned long dict2_total() { return sizeof(dict2) / sizeof(dict2[0]); } 主程序: int _tmain(int argc, _TCHAR* argv[]) { unsigned long dict_total = dict1_total(); // =73998 unsigned long total = 0; cout << "-----------------------------------" << endl; cout << "this is dictionary1" << endl; cout << " total : " << dict_total << endl; for (unsigned long i = 0; i < dict_total; ++i) { total += LATIN_WORD_LENGTH_MAX; } cout << " space sum : " << total << endl; cout << "-----------------------------------" << endl; total = 0; dict_total = dict2_total(); // = 73998 cout << "this is dictionary2" << endl; cout << " total : " << dict_total << endl; for (unsigned long i = 0; i < dict_total; ++i) { total += sizeof(dict2[i]); total += 2 * (wcslen(dict2[i] + 1 )); } cout << " space sum : " << total << endl; cout << "-----------------------------------" << endl; system("pause"); return 0; } 程序结果与各位老师一致,显示二维数组表示方法,占用更多的空间: ----------------------------------- this is dictionary1 total : 73998 space sum : 1849950 ----------------------------------- this is dictionary2 total : 73998 space sum : 1383970 ----------------------------------- 但obj文件大小: dict1 : 3617 KB (用二维数组) dict2 :16132 KB (用一维数组) 请各位老师帮助解释一下这种背离的现象。
john 2015-03-13
  • 打赏
  • 举报
回复
也许不是那么简单。
赵4老师 2015-03-13
  • 打赏
  • 举报
回复
顶2楼!
    #define MAX_STRING_LENGTH 8
    char dictionary[][MAX_STRING_LENGTH] = { "a", "b", "zone", "zymurgy" };
    // "a"       -> MAX_STRING_LENGTH字节
    // "b"       -> MAX_STRING_LENGTH字节
    // "zone"    -> MAX_STRING_LENGTH字节
    // "zymurgy" -> MAX_STRING_LENGTH字节
    //合计32字节

    char * dictionary2[] = { "a", "b", "zone", "zymurgy" };
    // dictionary2[0] -> sizeof(char*)字节 32位系统下为4
    // dictionary2[1] -> sizeof(char*)字节
    // dictionary2[2] -> sizeof(char*)字节
    // dictionary2[3] -> sizeof(char*)字节
    // "a"       -> 2字节
    // "b"       -> 2字节
    // "zone"    -> 5字节
    // "zymurgy" -> 8字节
    //合计33字节
john 2015-03-13
  • 打赏
  • 举报
回复
编译之后,obj文件的大小比较,不能作为分析的依据吗?从obj文件比较,二唯数组表示形式文件小了许多。
seiaisidien 2015-03-13
  • 打赏
  • 举报
回复

	char dictionary[][MAX_STRING_LENGTH] = { "a", "b", "zone", "zymurgy" };
	
	// "a"       -> MAX_STRING_LENGTH字节
	// "b"       -> MAX_STRING_LENGTH字节
	// "zone"    -> MAX_STRING_LENGTH字节
	// "zymurgy" -> MAX_STRING_LENGTH字节

	char * dictionary2[] = { "a", "b", "zone", "zymurgy" };

	// dictionary2[0] -> sizeof(char*)字节
	// dictionary2[1] -> sizeof(char*)字节
	// dictionary2[2] -> sizeof(char*)字节
	// dictionary2[3] -> sizeof(char*)字节
	// "a"       -> 2字节
	// "b"       -> 2字节
	// "zone"    -> 5字节
	// "zymurgy" -> 8字节
注意:如果数据量太大,建议在堆上开辟空间存储。
i2016 2015-03-13
  • 打赏
  • 举报
回复
直接定义数组使用的是有可能的最大空间,当然比指针类型按需分配空间要浪费一些。 在数据量小的适合,这个差别很小吧? 另外,指针形式能那么直接初始化吗?

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧