char *中的垃圾(摆脱它??) [英] Garbage in a char* (getting rid of it??)

查看:66
本文介绍了char *中的垃圾(摆脱它??)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我使用动态字符数组,我正试图摆脱

中的垃圾。让我告诉你代码,然后我会详细解释。


--------------------- -------------------------------------------------- -----------------------------

CFile oFile(" User.tcx",CFile :: modeRead);

CRijndael oDecrypt;

长尺寸;

char * buffer,* buf;


size = oFile.GetLength();


buffer = new char [size];

memset(buffer,0,size);


oFile.Read(缓冲区,大小);

oFile.Close();


buf = new char [size ];

int size_t = strlen(buf);

memset(buf,0,size);


oDecrypt.Decrypt (缓冲区,buf,大小,CRijndael :: CBC);


CFile iFile(test_r.xml,CFile :: modeCreate | CFile :: modeWrite);

iFile.Write(buf,strlen(buf));

iFile.Close();

------------ -------------------------------------------------- --------------------------------------


好 所以上面代码的主要思想是:


1.读取一个Rijndael编码文件的User.tcx,并在一个缓冲区中加载所有

文件的大小。

2.将缓冲区丢给Decrypt函数,该函数将使用第二个
缓冲区(* buf)来写入结果。 * buf变量具有相同的大小

作为*缓冲区和加密文件。

3.使用strlen将结果以* .buf写入XML文件(在
中为了避免写入太多的字符。


之后,我可以将整个XML文件看到源文件中,但是

XML不会出现在浏览器中。所有代码都在那里,但我总是在文件的末尾得到

垃圾:


????? yyyy


奇怪的是strlen(缓冲区),strlen(buf)给了我2864

作为缓冲区的大小。但是,变量大小是指变量。这是文件大小

的大小= oFile.GetLength();正在显示2848.我不知道

如果这与垃圾有关,但我可以向你保证

我没有调整我的数组大小。<事实上,如果我这样做:


buffer = new char [2848];

int len = strlen(缓冲区);


它仍然会给我2864作为strlen(缓冲区)。当然,如果我使用

memset,长度会下降到0,但事实是,即使我将
硬编码我的动态数组到2848,它们的大小也会达到2864再说一遍,我不知道这是否与垃圾有关,但是我很好b $ b好​​奇。

最重要的是,如何将垃圾清除到我的书面文件中?我不想在缓冲区中使用它,只要它在XML输出文件中不是
。而且我必须使用动态数组,因为并非所有

文件都具有相同的大小。


感谢您的帮助:)


Guillaume

解决方案

le *************** @ gmail.com 写道:


[哎呀,我意外地回复了你的电子邮件帐户..抱歉...

buffer = new char [2848];
int len = strlen(buffer);
<它仍然会给我2864作为strlen(缓冲区)。当然,如果我使用memset,长度会降到0,但事实是即使我将动态数组硬编码到2848,它们的大小也会达到2864.再次,我
我不知道这是否与垃圾有关,但我很好奇。




''缓冲''是一个数组随机字符值。现在这就是

strlen

的作用:它检查每个元素(值仍然是垃圾!)和

看起来

为0的字符。因为你的数组包含随机垃圾,所以它可能是
可能

(在你的情况下)将永远不会在你的数组中找到它。所以它在

的内存中读取了你甚至不拥有的内存。长话短说:不要在

数组上使用strlen,

如果你不确定你的数组中有0。你需要在char-array和C-string之间区分
。当且仅当char数组包含0时,C字符串是包含在char数组中的


如果

不,你没有C字符串,可能(必须!)不使用str *函数



吧。


在memset为0之后strlen返回0的原因是:strlen在开头找到

0

- > C字符串的长度为0.


您可能还要查看std :: string和std :: vector。


hth

-

jb


(rot13的回复地址,先解读)


le ************** *@gmail.com 写道:

我使用动态字符数组,我正试图摆脱
它的垃圾。



最好先清除代码中的垃圾。

内存泄漏太多,废话太多。


不使用像strlen这样的C东西是个好主意。


le *************** @ gmail.com 写道:


奇怪的是,strlen(缓冲区),strlen(buf)给了我2864
作为缓冲区的大小。但是,变量大小是指变量。这是文件大小
的大小= oFile.GetLength();正在显示2848.我不知道
这是否与垃圾有关,但我可以向你保证
我不会调整阵列的大小。

事实上,如果我这样做:

buffer = new char [2848];
int len = strlen(buffer);


strlen适用于C样式字符串(空终止字符数组),而不是
任意未初始化字符数组。

它将会仍然给我2864作为strlen(缓冲区)。


在我的平台上它不会崩溃。

当然,如果我使用
memset,长度会降到0,但事实是,即使我将动态数组硬编码为2848,它们的大小也会达到2864.


你的数组不会大小到2864,只是因为你使用函数错误地使用了
。它们仍然是2848长,能够存储一个字符串

的2847个字符(最后是一个空字符)。访问任何东西

超出此范围(通过使用strlen)是未定义的行为。

再次,我不知道这是否与垃圾有关,但我很好奇。


当然这与垃圾有关。你实际上是在使用

未初始化的字符串。

底线,如何将垃圾清除到我的书面文件中?


总是初始化你的变量。

我不是真的关心它是否在缓冲区中,只要它''在XML输出文件中没有
。我必须使用动态数组,因为并非所有
文件都具有相同的大小。




用std :: string拧什么?如果你不知道何时使用strlen和

如何初始化你的C风格字符串,你将真的很难用

动态长度C风格的更复杂方面字符串。事实上,我认为你根本无法做到这一点。


不要感觉不好,std ::字符串是为了这个

的原因而发明的。在几乎所有情况下,C风格的琴弦都很难而且没必要。


Ben Pope

-

我不只是一个号码。对很多人来说,我被称为字符串...


Hi guys,

I use dynamic char arrays and I''m trying to get rid of the garbage in
it. Let me show you the code and then I''ll explain more in details.

----------------------------------------------------------------------------------------------------
CFile oFile("User.tcx", CFile::modeRead);
CRijndael oDecrypt;
long size;
char* buffer,* buf;

size = oFile.GetLength();

buffer = new char[size];
memset(buffer, 0, size);

oFile.Read(buffer,size);
oFile.Close();

buf = new char[size];
int size_t = strlen(buf);
memset(buf, 0, size);

oDecrypt.Decrypt(buffer, buf, size, CRijndael::CBC);

CFile iFile("test_r.xml", CFile::modeCreate|CFile::modeWrite);
iFile.Write(buf, strlen(buf));
iFile.Close();
----------------------------------------------------------------------------------------------------

Ok so the big idea of the code above is :

1. Reads User.tcx which is a Rijndael encoded file and load everything
in a buffer which has the size of the file.
2. Throw the buffer to the Decrypt function which will use a second
buffer (*buf) to write the results. The *buf variable has the same size
as the *buffer and the encrypted file.
3. Write the results in *.buf to an XML file by using strlen(buf) in
order to avoid too many caracters to be written.

After that, I can see my whole XML file into the source file, but the
XML won''t show up in a browser. All the code is there but I always get
garbage like this at the end of the file :

yyyy

The weird thing is that strlen(buffer), strlen(buf) are giving me 2864
as the size of the buffers. But, the variable "size" which is the size
of the file in size = oFile.GetLength(); is showing 2848. I don''t know
if this has something to do with the garbage, but I can assure you that
I do not resize my arrays.

In fact, if I do this :

buffer = new char[2848];
int len = strlen(buffer);

it will still gives me 2864 as strlen(buffer). Of course, if I use
memset the length goes down to 0, but the fact is that even if I
hardcode my dynamic arrays to 2848, they will size to 2864. Again, I
don''t know if this has something to do with the garbage, but I''m
curious.
Bottom line, How can I get rid of the garbage into my written file ? I
don''t really care having it in the buffer or not, as long as it''s not
in the XML output file. And I must use dynamic arrays, since not all
files has the same size.

Thanks for any help :)

Guillaume

解决方案

le***************@gmail.com wrote:

[whoops, i accidently replied to your email account .. sorry about that]

buffer = new char[2848];
int len = strlen(buffer);

it will still gives me 2864 as strlen(buffer). Of course, if I use
memset the length goes down to 0, but the fact is that even if I
hardcode my dynamic arrays to 2848, they will size to 2864. Again, I
don''t know if this has something to do with the garbage, but I''m
curious.



''buffer'' is an array of random character values. Now this is what
strlen
does: it checks each element (where the value is garbage still!) and
looks
for the character that is 0. Since you array contains random junk, it
might
(and in your case will) never find it inside your array. So it reads on
in
memory you do not even own. Long story short: do not use strlen on an
array,
if you are not *sure* that there is a 0 inside your array. You need to
differenciate between a char-array and a C-string. A C-string is be
contained in a char-array, if and only if the char array contains a 0.
If
not, you do not have a C-string and may (must!) not use str* functions
on
it.

The reason strlen returns 0 after memset with 0 is: strlen finds the
0
right at the beginning -> length of the C-string is 0.

You might also have a look into std::string and std::vector.

hth
--
jb

(reply address in rot13, unscramble first)


le***************@gmail.com wrote :

I use dynamic char arrays and I''m trying to get rid of the garbage in
it.



Better get rid of the garbage in your code first.
Too many memory leaks and too much nonsense.

Not using C stuff like strlen would be a good idea.


le***************@gmail.com wrote:


The weird thing is that strlen(buffer), strlen(buf) are giving me 2864
as the size of the buffers. But, the variable "size" which is the size
of the file in size = oFile.GetLength(); is showing 2848. I don''t know
if this has something to do with the garbage, but I can assure you that
I do not resize my arrays.

In fact, if I do this :

buffer = new char[2848];
int len = strlen(buffer);
strlen works on C style strings (null terminated char arrays), not
arbitrary arrays of uninitialised chars.
it will still gives me 2864 as strlen(buffer).
On my platform it doesn''t, it crashes.
Of course, if I use
memset the length goes down to 0, but the fact is that even if I
hardcode my dynamic arrays to 2848, they will size to 2864.
Your arrays do not "size" to 2864, just because you are incorrectly
using a function. They are still 2848 long, capable of storing a string
of 2847 characters (and a null char at the end). Accessing anything
beyond this (by using strlen) is undefined behaviour.
Again, I
don''t know if this has something to do with the garbage, but I''m
curious.
Of course it is to do with the garbage. You''re effectively using an
uninitialised string.
Bottom line, How can I get rid of the garbage into my written file ?
Always initialise your variables.
I
don''t really care having it in the buffer or not, as long as it''s not
in the XML output file. And I must use dynamic arrays, since not all
files has the same size.



What''s wring with std::string? If you don''t know when to use strlen and
how to initialise your C style strings, you will really struggle with
the more complicated aspects of dynamic length C style strings. In
fact, I don''t think you will be able to do it at all.

Don''t feel bad though, std::string was invented for precisely this
reason. C style strings are hard and unnecessary in almost all cases.

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


这篇关于char *中的垃圾(摆脱它??)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆