strcpy将随机数添加到空字符串(strcpy adding random numbers to empty string)
我试图通过使用
strcpy(buffer, "")
来清除char
缓冲区。strcpy()
似乎在字符串中放入随机数。 在gdb中,我看到缓冲区(received_message
):在
strcpy()
调用之前:(gdb) print received_message $6 = "9210070627\000\000\000\000\000\000\000\000\000"
在
strcpy()
调用之后:(gdb) print received_message $8 = "\000\062\061\060\060\067\060\066\062\067\000\000\000\000\000\000\000\000\000"
其中
\060
...\067
= 48 ... 65。我在代码中的
strcpy()
调用只是strcpy(received_message, "");
所以我不确定发生了什么。我已经解决了实际问题,只是放入一个空终止符,而不是试图使字符串为空,因为我知道字符串的长度,但我仍然很好奇这里发生了什么。
编辑:似乎有些人想要更多的背景知道为什么我这样做。 我这样做是因为我正在使用zmq,它发送和接收没有null终止符的字符串。 我在测试中遇到了一个问题,我在做以下事情:
Send 1234 Receive 1234 Send 123 Receive 1234 Send 12345 Receive 12345 Send 1234 Receive 12345
似乎正在发生的事情是我正在重用我的缓冲区来接收消息(
received_message
),如果前一个字符串长于正在接收的字符串,则保留值。为了解决这个问题,我想“刷新”缓冲区,这意味着我想将它全部设置为空字符。 通过阅读其他一些答案,看起来
strcpy(received_message, "")
可以做到这一点,recived_message[0] = 0
。 但是,这些都不起作用,因为它们只将第一个字符设置为null。 我知道“刷新”缓冲区的memset()
方法,但是读到它比strcpy(received_message, "")
东西要慢一些,所以没有使用它。 我想出的解决方案(并在下面显示)避免使用memset()
设置整个数组,所以我对它有点开心,尽管它可能完全没有任何区别。令我感到困惑的是,这个
strcpy()
调用用前缀为\06
的单个数字替换了字符串中原来的数字,我误认为是\060
等ASCII字符而不是前缀为\06
0。我已经修复了接收消息的问题,因为
zmq_recv()
返回没有空终止符的字符串长度,所以要在接收消息的末尾放置一个空字符,我们只需要做int received_length = zmq_recv(request_socket, received_message, 20, 0); received_message[received_length] = 0;
在这种情况下,
received message
是一个20元素的char
数组,我担心收到比这种情况更长的消息。解决问题之后,我仍然对我的
strcpy()
调用发生了什么感到好奇,尽管我没有正确理解发生了什么。 我仍然很好奇为什么这些数字都以\06
为前缀。I am trying to flush out a
char
buffer by usingstrcpy(buffer, "")
.strcpy()
seems to be putting random numbers in the string. In gdb, I see the buffers (received_message
):Before
strcpy()
call:(gdb) print received_message $6 = "9210070627\000\000\000\000\000\000\000\000\000"
After
strcpy()
call:(gdb) print received_message $8 = "\000\062\061\060\060\067\060\066\062\067\000\000\000\000\000\000\000\000\000"
Where
\060
...\067
= 48 ... 65.My
strcpy()
call in the code is simplystrcpy(received_message, "");
, so I am not sure what is going on.I have solved the actual issue by just putting in a null terminator rather than trying to make the string empty since I know the length of the string, but I am still very curious about what is going on here.
EDIT: It seems that some people want some more background on why I am doing this. I was doing this as I am using zmq, which sends and received strings without the null terminator. I was running into a problem in a test where I was doing the following:
Send 1234 Receive 1234 Send 123 Receive 1234 Send 12345 Receive 12345 Send 1234 Receive 12345
What seemed to be happening is that I was reusing my buffer for receiving messages (
received_message
), which was retaining values if the previous string was longer than the one that was being received.To fix this, I wanted to "flush" the buffer, meaning I wanted to set it all to null characters. From reading some other answers, it seemed that
strcpy(received_message, "")
would do the trick, as wouldrecived_message[0] = 0
. However, neither of these would work as they only set the first character to null. I knew of thememset()
method of "flushing" the buffer, but read that it was a bit slower than something such asstrcpy(received_message, "")
, so did not use it. The solution I figured out (and show below) avoids setting the whole array withmemset()
, so I am a bit happier with it, although it likely makes no difference at all.What confused me is the fact that this
strcpy()
call replaced what was originally a number in a string by the individual numbers prefixed with\06
, which I has mistaken for ASCII characters such as\060
rather than 0 prefixed by\06
.I have fixed the issue with receiving the message since
zmq_recv()
returns the string length without the null terminator, so to put a null character at the end of the received message, we only need to doint received_length = zmq_recv(request_socket, received_message, 20, 0); received_message[received_length] = 0;
In this case,
received message
is a 20-element array ofchar
and I'm worried about receiving messages longer than that in this case.After solving the problem, I was still curious about what was going on with my
strcpy()
call, although I was not correctly understanding what was happening. I am still curious why the numbers were being prefixed with\06
though.
原文:https://stackoverflow.com/questions/30460241
最满意答案
是的,这绝对没有问题。 您甚至可以在同一台机器上同时安装32位和64位Java的多个版本。
其实我自己有这样的设置。
Yes, it is absolutely no problem. You could even have multiple versions of both 32bit and 64bit Java installed at the same time on the same machine.
In fact, i have such a setup myself.
相关问答
更多-
您需要正确设置路径才能运行java二进制文件。 从用于Microsoft Windows的JDK安装 : Updating the PATH Environment Variable If you do not set the PATH variable, you need to specify the full path to the executable file every time you run it, such as: C:\> "C:\Program Files\Java\jdk1.8.0 ...
-
是否可以在Windows 7上安装32位和64位Java?(Is it possible to install both 32bit and 64bit Java on Windows 7?)[2024-02-07]
是的,这绝对没有问题。 您甚至可以在同一台机器上同时安装32位和64位Java的多个版本。 其实我自己有这样的设置。 Yes, it is absolutely no problem. You could even have multiple versions of both 32bit and 64bit Java installed at the same time on the same machine. In fact, i have such a setup myself. -
除了安装32位JRE / JDK,无论您的要求如何,您都不需要做任何事情。 我一直这样做。 使用64位版本的唯一真正原因是如果您的应用程序需要能够访问超过4GB的RAM(或者一些程序化的lib依赖项) 确保你安装了32位版本并将JAVA_HOME环境变量指向安装目录,所以如果你安装了JRE 1.6,JAVA_HOME应该是类似于 C:\Program Files (x86)\Java\jre1.6.0_XX 另外,在你的Path环境变量中添加%JAVA_HOME%\bin到最后,这将使所有的java可执行文 ...
-
带有nginx的PHP通常作为fastcgi应用程序运行( https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/ )。 这将通过Windows上的TCP套接字进行通信 - 因此无论是远端的64位还是32位PHP都无关紧要。 在PHP方面,如果性能是一个关键驱动因素,那么你的新功能 - 你会考虑考虑使用php 7,因为它在php5系列上有很多性能提升。 php 7的开发在他们页面底部的谷歌文档电子表格上有一些基准测试https: ...
-
很可能您下载的文件已损坏 。 检查是否完全下载,或尝试使用其他浏览器下载安装程序。 不幸的是,JRE下载页面上没有校验和。 同时检查数字签名是否有效并且不显示警告:打开安装程序的属性对话框,然后单击数字签名选项卡。 Most probably the file that you downloaded is corrupted. Check that it downloaded completely, or try to use another browser to download the installe ...
-
当然是。 java程序是一个中间字节代码,而不依赖于平台。 没有像“64位java程序”这样的东西。 但是运行java程序的JVM当然是依赖于平台的。 在32位机器上,您必须使用32位JRE或JDK,反之亦然。 另请参见java-32-bit-vs-64-bit-compatibility Yes, of course. A java program is an intermediate byte code and not platform dependant. There is no such thing ...
-
在Windows 7 64位计算机上使用Visual Studio 2005(32位)(Using Visual Studio 2005 (32bit) on a Windows 7 64bit machine)[2022-11-26]
答案是1)您只需要确保目标处理器是32位。 您可以在64位机器上开发32位机器,反之亦然。 看了一下之后看起来你必须拥有最新的服务包。 如果您想了解更多信息,请阅读http://msdn.microsoft.com/en-us/vstudio/aa948853.aspx 。 The answer is 1) you just need to make sure that you target processor is 32bit. You can develop for 32bit machine on a ... -
Windows bat文件确定Java安装是32位还是64位?(Windows bat file determining if Java install is 32 bit or 64 bit?)[2024-03-20]
这是一个小的Windows脚本,可以帮助您入门,确定Java32 vs Java64与JavaNotInstalled。 根据需要调整...... @echo off java -d64 -version >nul 2>&1 if errorlevel 1 goto maybe32bit echo Java is 64 bit goto EXIT :maybe32bit where java >nul 2>&1 if errorlevel 1 goto nojava echo Java is 32 bit ... -
我强烈建议: 1)安装64位JDK(多个不同的JDK可以在同一主机上共存) 2)启动Eclipse,将项目配置为使用64位Java作为其构建和运行时路径......并查看会发生什么。 它应该工作正常。 除非您的程序碰巧使用SWT(而不是Swing),或者碰巧有其他32/64位依赖项。 3)尝试绝对没有害处。 恕我直言... PS:值得注意的是,您也可以在同一主机上共存多个版本的Eclipse。 您所需要的只是大量的磁盘空间。 在两个版本中重新安装插件和附件的麻烦。 I would strongly reco ...
-
您可以在eclipse.ini指定VM 。 对于Windows,这看起来像这样: -vm C:\Java\JDK\1.6\bin\javaw.exe 是的,它们应该分成两行。 请务必在任何-vmargs参数之前指定,因为之后的所有内容都被解释为VM的参数( 有关详细信息,请参阅此问题 )。 You can specify the VM in eclipse.ini. For Windows this would look something like this: -vm C:\Java\JDK\1.6\b ...