做这样的事情是否安全:
char* charArray = new char[10];
strcat(charArray, "qwertyuiop");
charArray[3] = '\0';
delete [] charArray;
Run Code Online (Sandbox Code Playgroud)
一切都会被删除吗?或者之后的事情\0不会是什么?我不知道我是否要离开垃圾.
编辑:应该是 strcpy
我有这个从infile到outfile的复制代码,问题是在outfile的末尾添加了很多垃圾
ssize_t nread;
int bufsize=512;
char buffer[bufsize];
while ( (nread=read(infile, buffer, bufsize)>0))
{
if( write(outfile, buffer, bufsize)<nread )
{
close(outfile); close(infile); printf("error in write loop !\n\n");
return (-4);
}
}
if( nread == -1) {
printf ("error on last read\n"); return (-5);
}//error on last read /
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?
我知道如何通过将其引用变量设置为null来为垃圾收集器留下信号以删除对象:
Player player_reference = new Player();
player_reference = null;
// Now the Garbage collector knows to release all the memory related to this object.
Run Code Online (Sandbox Code Playgroud)
但是如何通过对象的类将引用变量设置为null?
class Player {
public void doSomthing() {
if(condition) {
// some code which set the reference variable to null.
}
}
}
Run Code Online (Sandbox Code Playgroud) 当我们不在宏替换中给出值时.
#define YES
printf("%d",YES)
Run Code Online (Sandbox Code Playgroud)
没有给出YES的值,它默认是0还是垃圾?
如何在垃圾收集器收集之前手动删除特定对象?例如,我想删除requestToken对象.我怎样才能做到这一点 ?
我正在使用Boost :: Asio库制作C++ Client/Server TCP的模拟器.
这是我的代码的一部分:
客户:
std::string option;
std::getline(std::cin, option);
option.push_back('\r');
option.push_back('\n');
boost::asio::write(client_socket, boost::asio::buffer(option));
Run Code Online (Sandbox Code Playgroud)
服务器:
boost::asio::streambuf received;
boost::asio::read_until(socket, received, "\r\n");
char number[7];
recieved.sgetn(number, 7);
std::cout << "The entered number is: " << number << std::endl;
Run Code Online (Sandbox Code Playgroud)
(我只包括有问题的代码)
我的问题是垃圾值包含在内char[] number,如下输入1234567:

我该如何解决这个问题?
声明我喜欢的局部变量时:
int i;
Run Code Online (Sandbox Code Playgroud)
并且您使用此变量,您将得到未定义的行为,因为我尚未初始化.但我持有一个值,一个"垃圾"值,这个值来自哪里?它来自记忆中的随机位置吗?
我编写了一个C程序,用于使用指针显示数组的值.这是代码:
#include <stdio.h>
int main()
{
int a[] = {1, 1, 1, 1, 1};
int *ptr = a;
for (int i = 0 ; i < 5; i++)
printf("%d ", *ptr++);
printf("%d", *ptr);
}
Run Code Online (Sandbox Code Playgroud)
正如您在终止循环后所看到的那样,指针将数值的内存地址保存在数组之外.因为它没有初始化最后一个输出,它应该是一个垃圾值.但是,每次显示5是数组的大小.然后,我认为数组的已分配内存的下一个内存地址包含数组的大小.但是,双类型数组不会发生这种情况.
Output for int array : 1 1 1 1 1 5
Output for double array : 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.000000
Run Code Online (Sandbox Code Playgroud)
有人会解释输出吗?
我有一个关于使用C中的MPI库进行通信的问题.这是我的示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main (int argc, char **argv) {
// Initialize the MPI Environment
MPI_Init (&argc, &argv);
int world_size;
MPI_Comm_size (MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank (MPI_COMM_WORLD, &world_rank);
int workers = world_size - 1;
if (world_rank == 0) {
}
else if (world_rank == 1) {
int to_recv;
int to_send = world_rank;
MPI_Request request1, request2;
MPI_Irecv (&to_recv, sizeof (int), MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD, &request1);
MPI_Isend (&to_send, sizeof (int), MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD, …Run Code Online (Sandbox Code Playgroud) 我如何防止或绕过我的变量中的垃圾valus malloc put?附上代码和输出!谢谢!
#include <stdio.h>
#include "stdlib.h"
#include <string.h>
int main() {
char* hour_char = "13";
char* day_char = "0";
char* time = malloc(strlen(hour_char)+strlen(day_char)+2);
time = strcat(time,day_char);
time = strcat(time,"-");
time = strcat(time,hour_char);
printf("%s",time);
free(time);
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
á[?0-13
Run Code Online (Sandbox Code Playgroud)