English 中文(简体)
在复制插图时,我必须注意什么?
原标题:What do I have to pay attention to when copying strings?
  • 时间:2011-11-21 17:03:28
  •  标签:
  • c
  • string
  • copy

在复制C号插图时,我必须注意哪些因素? 什么是错的?

我认为,在复制插图之前,我需要保留足够的记忆,并且必须保证我有足够的特权来书写记忆(避开一般保护ault),但是,在复制插图时,我还必须注意什么? 是否有其他潜在的错误?

最佳回答
  • Make sure that you have sufficient buffer space in the destination (i.e. know ahead how many bytes you re copying). This also means making sure that the source string is properly terminated (by a null character, i.e. 0 = ).
  • Make sure that the destination string gets properly terminated.
  • If your application is character-set aware, bear in mind that some character sets can have embedded null characters, while some can have variable-length characters.
问题回答

Cstrings是非零用 by体的常规阵列,最后是零件。 与他们打交道的例行公事可以到一旁(从那边起就进入阵列)。 你们应当注意保留地的位置。 学习strcpystrcat及其约束的对应部分strncpystrncat。 另见<代码>strdup。 不要忘记将逐字确定为零(特别是在达到<条码>载荷/代码>等的约束时)。 一些图书馆(例如德国技术合作公司Glib)提供冰电功能(例如:g_strdup_printf)

Read the documentation of all the functions I mentioned.

You need to:

  1. 为目的地分配足够的记忆(资料来源+1)

  2. • 确保来源不是全国扫盲联合会

  3. deal with unicode issues (string length might be less than length in bytes)

You have to pay attention to the definition of a string in C: they are a sequence of characters ended with a null terminating character ( ).

All the functions in string.h will work with this assumption. Work with those functions and you should be fine.





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签