- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试制作一个结构集合,其中一些成员是一种字符串类型,并且由于字符串处理起来“昂贵”,我正在尝试通过使用指针来最大化性能。
我尽力从教程中学习,但 C++ 中的字符串种类繁多。
如果值的类型是 char*
,设置 strVal
的最快方法是什么?
DataCollection.h
extern "C" __declspec(dllexport) void GetContCollection(int CollectionLength,int StrLength, DataContainer** Collection);
typedef struct {
int iValue;
char* strValue;
}DataContainer;
DataCollection.cpp
extern "C" __declspec(dllexport) void GetContCollection(int CollectionLength,int StrLength, DataContainer** Collection)
{
*Collection = (DataContainer*)LocalAlloc(0, CollectionLength * sizeof(DataContainer));
// say i need to get a record from database returning a char array
// and i use current datatype
*DataContainer CurElement = *Collection;
// iteration on each element of the collection
for(int i=0, i< CollectionLength; i++, CurElement++)
{
char* x = getsomeValuefromSystemasChar();
//.... how to assign CurElement->strValue=?
CurElement->strValue =// kind of Allocation is needed or ....just assign
//next, do i have to copy value or just assign it ?
CurElement->strValue = x or strcpy(dest,source)// if copying must take place which function would be the best?
}
}
设置 CurElement
的正确且最优化的方法是什么?
最佳答案
对该问题的评论和编辑已使该答案的大部分内容过时。我纯粹为可能查看编辑历史的任何人保留它。仍然有效的部分位于此答案的末尾。
如果正如问题所说,结构中的所有
char *
都将引用字符串文字,那么您不需要分配内存,也不需要在分配时的特殊步骤方式。
字符串文字具有静态存储持续时间,因此您只需将其地址分配给指针即可,一切都会好起来的。但是,您不想让任何人不小心写入字符串文字,因此您通常希望使用指向 const
的指针:
typedef struct {
int iValue;
char const * strValue;
} DataContainer;
然后需要赋值的时候赋值即可:
extern "C" __declspec(dllexport) void GetContCollection(int CollectionLength,int StrLength, DataContainer** Collection)
{
// ...
CurElement->strValue = "This is a string literal";
}
您可以(绝对)计算具有静态存储持续时间的字符串文字,因此毫无疑问这会起作用。由于您只分配一个指针,因此它也会很快。
不幸的是,它也有些脆弱——如果有人在这里分配字符串文字以外的东西,它很容易损坏。
这给我们带来了下一个问题:您是否真的在处理字符串文字根本。尽管您特别询问了字符串文字,但您显示的演示代码看起来根本不像是在处理字符串文字——如果不是,像上面这样的代码将会崩溃。
如果你必须处理那个,
我会说只使用 std::string
。如果您坚持自己这样做,则很有可能会产生损坏的东西,而您在不损坏任何东西的情况下获得实质性速度优势的机会很小(几乎没有)。
关于c++ - 在 struct[] 中分配 char* 指针值(谁是 struct 成员)的最优化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33245923/
我有一个数组 items[] items[] 中的每一项都是一个结构体。 item 有键 id、date、value(即 item.id、item.date、item.value) 我想使用 Stru
我想存储 100 名员工。 RollNo,姓名,工资,时间(各种数据,我无法在这里解释,但你可以看下面的代码片段来理解 main() { struct day { int hour
这个问题在这里已经有了答案: storage size of ‘names’ isn’t known (3 个答案) 关闭 5 年前。 我正在尝试蓝牙编程,遇到了这个我不明白的问题。基本上,当我使用
这是一个奇怪的事情: 我有一个结构,它包含指向相同类型结构的指针和指向其他类型结构的指针,以及一些其他值。 struct animal { struct animal * father;
我有一个结构定义如下(名称不同) struct str1 { int field1; struct str2; } 我在一个函数中有一个*str1。我想要一个指向 str2 的指针。 所以
DISK_DETECTION_INFO is defined as有什么原因吗? typedef struct _DISK_DETECTION_INFO { DWORD Size
我正在尝试打包一个字符串和一个字符串的长度。 fmt = '
我在创建结构时遇到问题。 我的结构: public struct Device: Codable { let data: DeviceData let meta: Meta? } pu
struct Item { var name:String? var type:String? var value:Int? var tag:Int? } ... ..
// NewReaderSize returns a new Reader whose buffer has at least the specified 43 // size. If the ar
这个问题在这里已经有了答案: Sorting a vector of custom objects (14 个答案) 关闭 3 年前。 在下面的 C++ 片段中, 如何基于 TwoInts 结构中的
#include struct Header { unsigned long long int alignment; }; int main(void) { struct Heade
我有一个目前看起来像这样的结构(缩写为仅显示基本部分): typedef struct { uint32_t baudrate; ... some other internally u
对此没有太多解释,这就是我所拥有的: public struct PACKET_HEADER { public string computerIp; publi
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
struct S1 { char c; int i; }; struct S3 { char c1; struct S1 s; double c2; }; 我正
我有一个名为 Parameter 的协议(protocol): protocol Parameter { var name: String { get } var unit: Unit
有 2 个 struct 定义 A 和 A。我知道 struct A 可以包含指向 struct A 的 POINTER 但我不明白为什么 struct A 不能包含struct A(不是指针) 最佳
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
为了说明这一点,这里有一个小的不可变结构和一个更新它的函数: (struct timeseries (variable observations) #:transparent) (define (ad
我是一名优秀的程序员,十分优秀!