简体   繁体   English

C ++字符串指针

[英]C++ String pointers

In my previous app I had an object like this: 在我以前的应用程序中,我有一个这样的对象:

class myType
{
public:
 int a;
 string b;
}

It had a lot of instances scattered everywhere and passed around to nearly every function. 它有很多实例分散在各处,并传递给几乎所有的功能。

The app was slow. 该应用程序很慢。 Profiling said that 95% of time is eaten by the string allocator function. 分析说,95%的时间被字符串分配器功能吃掉。

I know how to work with the object above, but not how to work with string pointers. 我知道如何使用上面的对象,但不知道如何使用字符串指针。

class myType
{
public:
 int a;
 string* b;
}

They told me to use pointers as above. 他们告诉我如上所述使用指针。

  • How much faster is it with a string pointer? 字符串指针多快多快?
  • What is copied when I copy the object? 复制对象时复制的内容是什么?

  • How to the following using the class with the pointer: 如何使用带有指针的类:

Access the string value 访问字符串值

Modify the string value without modifying the one in the object (copy?) 修改字符串值而不修改对象中的值(复制?)

General things that change if I use string pointers? 如果我使用字符串指针会改变一般的东西?

It will actually probably be slower - you still need to create and copy the strings, but now you have the overhead of dynamic allocation on top. 它实际上可能会更慢 - 你仍然需要创建和复制字符串,但现在你有动态分配的开销。 My guess is that you are copying your objects around too much - whenever you call a function, your myType objects should be passed as const references, wherever possible, not by value: 我的猜测是你正在复制你的对象太多 - 每当你调用一个函数时,你的myType对象应该作为const引用传递,尽可能,而不是值:

void f( const myType & mt ) {
    // stuff
}

If you actually need to change mt, you would have used a non-const reference - this is also less expensive than passing a value and returning a new value with modified fields. 如果你真的需要更改mt,你会使用非const引用 - 这比传递一个值并使用修改后的字段返回一个新值要便宜。

I think using a pointer like this is a bad idea. 我认为使用像这样的指针是一个坏主意。 Instead, look at how your myType is being used instead. 相反,请查看如何myType In particular, instead of this: 特别是,而不是这个:

void foo(myType a)
{
    // ...
}

Consider this: 考虑一下:

void foo(myType const &a)
{
    // ...
}

In the former case, a copy of myType needs to be created to pass to the function foo(), in the second, no copy is needed, since a reference is passed instead (it's marked as const so that you can be sure foo() doesn't try to modify it - giving you (almost)the same behaviour as the first method). 在前一种情况下,需要创建myType的副本以传递给函数foo(),在第二种情况下,不需要复制,因为传递了引用(它被标记为const因此您可以确定foo()不会尝试修改它 - 给你(几乎)与第一种方法相同的行为)。

There are probably other things you could change, but my guess is that doing this would give you the most bang for your buck (and it's a pretty mechanical change, so hopefully not too much chance of problems being introduced) 可能还有其他一些你可以改变的东西,但我的猜测是,这样做可以让你获得最大的收益(这是一个非常机械的变化,所以希望没有太多机会引入问题)

To add to the other answers, you also should be making sure any member functions of that class that pass a string are passing by const reference. 要添加到其他答案,您还应该确保传递字符串的该类的任何成员函数都通过const引用传递。 For example, say your class constructor definition looks like this: 例如,假设您的类构造函数定义如下所示:

myType::myType(int a, string b)

Use this instead: 请改用:

myType::myType(int a, const string& b)

So basically, go through all your function parameters throughout your project, and change string to const string& , and myType to const myType& . 所以基本上,在整个项目中遍历所有函数参数,并将string更改为const string& ,将myType更改为const myType& This alone should fix the majority of your performance issues. 仅这一点就可以解决您的大多数性能问题。

Note: About dynamically allocating the string and passing as a pointer: This is not a good idea, as though it will lighten the performance load somewhat, you're going to be extremely vulnerable to memory leaks, which makes debugging a nightmare (in addition to being much more haphazardly destructive to your performance than running slow). 注意:关于动态分配字符串并作为指针传递:这不是一个好主意,好像它会稍微减轻性能负担,你将极易受到内存泄漏的影响,这使得调试成为一场噩梦(另外对你的表现更加随意破坏而不是慢跑。 As a general rule, I highly discourage passing naked pointers. 作为一般规则,我强烈反对通过裸指针。 There's almost always a better, safer alternative. 几乎总有一种更好,更安全的替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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