简体   繁体   中英

most optimized approach to assign a char* pointer value (who is a struct member) in struct[]

i am trying to make a collection of structs that some of it's members are a type of string, and as strings are 'expensive' to handle i am trying to maximize performance by using pointers.

i tried my best to learn from tutorials but there are so many different kinds of strings in c++.

what is the fastest way to set strVal , if the type of value is char* ?

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?
   }
}

what is the correct and most optimized way to set CurElement ?

Comments and edits to the question have rendered a great deal of this answer obsolete. I'm preserving it purely for anybody who might look through the edit history. The part that remains valid is at the end of this answer.

If, as the question says, all the char * in the struct will ever refer to is string literals, then you don't need to allocate memory, or take much in the way of special steps when assigning.

A string literal has static storage duration, so you can just assign its address to the pointer, and all will be fine. You don't want to allow anybody to accidentally write to the string literal though, so you usually want to use a pointer to const :

 
 
 
  
  typedef struct { int iValue; char const * strValue; } DataContainer;
 
 

Then when you need to assign, just assign:

 
 
 
  
  extern "C" __declspec(dllexport) void GetContCollection(int CollectionLength,int StrLength, DataContainer** Collection) { // ... CurElement->strValue = "This is a string literal"; }
 
 

You can count (absolutely) upon string literals having static storage duration, so there's no question this will work. Since you're only assigning a pointer, it'll also be fast.

Unfortunately, it's also somewhat fragile--if somebody assigns something other than a string literal here, it can break very easily.

That brings us to the next question: whether you're really dealing with string literals at all . Although you specifically ask about string literals, the demo code you show doesn't look like it's dealing with string literals at all--and if it's not, code like the above will break horribly.

If you have to deal with that,

I'd say just use std::string . If you insist on doing this yourself, there's an excellent chance you'll produce something broken, and very little (almost no) chance you'll gain a substantial speed advantage without breaking anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM