首页 \ 问答 \ strcpy将随机数添加到空字符串(strcpy adding random numbers to empty string)

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 using strcpy(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 simply strcpy(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 would recived_message[0] = 0. However, neither of these would work as they only set the first character to null. I knew of the memset() method of "flushing" the buffer, but read that it was a bit slower than something such as strcpy(received_message, ""), so did not use it. The solution I figured out (and show below) avoids setting the whole array with memset(), 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 do

int 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 of char 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
更新时间:2022-05-10 18:05

最满意答案

是的,这绝对没有问题。 您甚至可以在同一台机器上同时安装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 ...
  • 是的,这绝对没有问题。 您甚至可以在同一台机器上同时安装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 ...
  • 答案是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脚本,可以帮助您入门,确定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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)