I want to assign a char*
to char*
, if I use strcpy
I get several run time memory problem so I fix it by simple assignment using = operator
. Can any one explain what should prepare before using strcpy
to avoid memory issues.
我想给char*分配一个char*,如果我使用strcpy,我就会得到几个运行时内存问题,所以我用=操作符来固定它。在使用strcpy来避免内存问题之前,谁能解释一下应该准备些什么。
EDIT:
编辑:
int Function(const char* K,char* FileIN,char* FileOut,int DC)
{
char *fic_in,*n_fic,*fic_out,*fic_dec;
unsigned char k[17];
fic_in = (char*)malloc(60*sizeof(char));
strcpy((char*)k,K);
//strcpy(fic_in,FileIN); //I remove this one
fic_in=FileIN; //and replace it by this
...
4 个解决方案
#1
4
const char *a = "string literal";
const char *b;
a
is a pointer that points to a string literal. b
is a pointer that doesn't point anywhere in particular (it is uninitialized).
a是指向字符串文字的指针。b是一个指针,它不指向任何地方(它没有初始化)。
b = a;
b
now points to the same memory that a
points to.
b现在指向相同的记忆点。
char c[100];
c
is now an array of 100 chars. That area of memory is completely separate from everything else so far.
c现在是100个字符数组。到目前为止,这一记忆区域与其他所有区域完全分离。
strcpy(c, a);
The contents of c
(to be specific, the first 15 bytes) now hold the same values as the memory pointed to by a
. So now there are two regions of memory that contain the same string data.
c的内容(具体地说,前15个字节)现在拥有与a所指向的内存相同的值,所以现在有两个内存区域包含相同的字符串数据。
So as you can see, assigning pointers has pretty much nothing in common with strcpy
. If you want to assign a char*
to a char*
, then you shouldn't be anywhere near strcpy
.
正如您所看到的,分配指针与strcpy几乎没有任何相同之处。如果要将char*分配给char*,那么就不应该在strcpy附近。
It's essential that you read a book about C++, but the basic requirement for strcpy
is that the destination pointer must point to a region of memory with enough space for the string that the source pointer points to. Probably your "run time memory problems" were because you didn't ensure that, which is why you need to read a book.
读一本关于c++的书是非常必要的,但是strcpy的基本要求是,目标指针必须指向一个内存区域,该区域有足够的空间用于源指针指向的字符串。可能你的“运行时记忆问题”是因为你没有确保,这就是为什么你需要读一本书。
#2
3
Short answer: You shouldn't use strcpy
at all, but rather strncpy
. The latter function does not rely on a trailing \0
and requires you to provide a maximum length for copying.
简短的回答:您不应该使用strcpy,而是使用strncpy。后一个函数不依赖于尾随\0,并且要求您提供复制的最大长度。
Furthermore, for pointer assignment you do not necessarily need strncpy
at all. Do you want to copy the memory or simply reassign the pointers? For reassignment, the =
operator is perfectly fine.
此外,对于指针赋值,您并不一定需要strncpy。您想要复制内存还是仅仅重新分配指针?对于重新赋值,=运算符是完全可以的。
#3
2
char*
is a pointer to the character of your string. To make a copy, you need to first allocate memory for it and the run the copy. Something like
char*是指向字符串的字符的指针。要复制一个副本,首先需要为它分配内存和运行副本。类似的
size_t l = strlen(src);
char *dst = malloc(l+1);
memmove(dst,src,l+1);
(and do some error checking if malloc succeeded, etc).
(如果malloc成功了,也做一些错误检查等等)。
Though, taking into account your question is tagged c++
, I'd recommend that you use a std::string
class and do some reading meanwhile. It really helps to understand how things actually work.
但是,考虑到您的问题被标记为c++,我建议您使用std::string类,同时进行一些阅读。它确实有助于理解事物的实际运作方式。
#4
1
You need to make sure that the receiving buffer is large enough to fit the source string, including the terminating '\0'
-character at the end. That's really it.
您需要确保接收缓冲区足够大,以适合源字符串,包括在结束时终止“\0”字符。这是真的。
If you can represent strings as just char *
pointers that you can copy, then that's great but it's not always possible.
如果你可以将字符串表示为你可以复制的char *指针,那很好,但这并不总是可行的。
#1
4
const char *a = "string literal";
const char *b;
a
is a pointer that points to a string literal. b
is a pointer that doesn't point anywhere in particular (it is uninitialized).
a是指向字符串文字的指针。b是一个指针,它不指向任何地方(它没有初始化)。
b = a;
b
now points to the same memory that a
points to.
b现在指向相同的记忆点。
char c[100];
c
is now an array of 100 chars. That area of memory is completely separate from everything else so far.
c现在是100个字符数组。到目前为止,这一记忆区域与其他所有区域完全分离。
strcpy(c, a);
The contents of c
(to be specific, the first 15 bytes) now hold the same values as the memory pointed to by a
. So now there are two regions of memory that contain the same string data.
c的内容(具体地说,前15个字节)现在拥有与a所指向的内存相同的值,所以现在有两个内存区域包含相同的字符串数据。
So as you can see, assigning pointers has pretty much nothing in common with strcpy
. If you want to assign a char*
to a char*
, then you shouldn't be anywhere near strcpy
.
正如您所看到的,分配指针与strcpy几乎没有任何相同之处。如果要将char*分配给char*,那么就不应该在strcpy附近。
It's essential that you read a book about C++, but the basic requirement for strcpy
is that the destination pointer must point to a region of memory with enough space for the string that the source pointer points to. Probably your "run time memory problems" were because you didn't ensure that, which is why you need to read a book.
读一本关于c++的书是非常必要的,但是strcpy的基本要求是,目标指针必须指向一个内存区域,该区域有足够的空间用于源指针指向的字符串。可能你的“运行时记忆问题”是因为你没有确保,这就是为什么你需要读一本书。
#2
3
Short answer: You shouldn't use strcpy
at all, but rather strncpy
. The latter function does not rely on a trailing \0
and requires you to provide a maximum length for copying.
简短的回答:您不应该使用strcpy,而是使用strncpy。后一个函数不依赖于尾随\0,并且要求您提供复制的最大长度。
Furthermore, for pointer assignment you do not necessarily need strncpy
at all. Do you want to copy the memory or simply reassign the pointers? For reassignment, the =
operator is perfectly fine.
此外,对于指针赋值,您并不一定需要strncpy。您想要复制内存还是仅仅重新分配指针?对于重新赋值,=运算符是完全可以的。
#3
2
char*
is a pointer to the character of your string. To make a copy, you need to first allocate memory for it and the run the copy. Something like
char*是指向字符串的字符的指针。要复制一个副本,首先需要为它分配内存和运行副本。类似的
size_t l = strlen(src);
char *dst = malloc(l+1);
memmove(dst,src,l+1);
(and do some error checking if malloc succeeded, etc).
(如果malloc成功了,也做一些错误检查等等)。
Though, taking into account your question is tagged c++
, I'd recommend that you use a std::string
class and do some reading meanwhile. It really helps to understand how things actually work.
但是,考虑到您的问题被标记为c++,我建议您使用std::string类,同时进行一些阅读。它确实有助于理解事物的实际运作方式。
#4
1
You need to make sure that the receiving buffer is large enough to fit the source string, including the terminating '\0'
-character at the end. That's really it.
您需要确保接收缓冲区足够大,以适合源字符串,包括在结束时终止“\0”字符。这是真的。
If you can represent strings as just char *
pointers that you can copy, then that's great but it's not always possible.
如果你可以将字符串表示为你可以复制的char *指针,那很好,但这并不总是可行的。