假设我分配了一些内存来存储 int 值,如下所示:
int *p=new int;
Run Code Online (Sandbox Code Playgroud)
在这里,我使用运算符创建了所需的内存new,并分配了该内存块的地址,以便我可以访问该内存块。
现在我可以控制存储在该内存块中的内容。
但是当我写下这样的声明时:
delete p;
Run Code Online (Sandbox Code Playgroud)
我们说我已经删除了动态分配的内存。
但是,如果我真的delete释放了该内存,那么在delete?但我能够使用相同的指针变量访问该内存块。那么如果删除后还能访问该内存块,那么删除该内存块的目的是什么呢?
这是一些示例代码:
#include <iostream>
using namespace std;
int main(void)
{
int *p;
p=new int;
*p=10;
cout << *p << endl;
delete p;
//here look we can still access that block of memory using the same pointer variable
//But we did not created the memory block after deletetion
cout << *p << endl;
*p=20;
cout << *p << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,“删除/释放动态分配的内存”这句话意味着什么?
c++ memory-management undefined-behavior dynamic-memory-allocation delete-operator
我目前被困在我的 c 项目上,我试图用 C 语言在内存中运行一个程序,但我一直都有分段错误。
首先我有一个简单的hello_worl.c,我编译它,我有输出hello_world。
第二,我hello_world像这样转换为 C 标头:
xxd -i hello_world > hello.h
(hello.h 的内容)
unsigned char hello[] = {
...
}
unsigned int hello_len = 16696;
Run Code Online (Sandbox Code Playgroud)
最后,我创建了程序main.c,看起来像这样:
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include "hello.h"
int main ()
{
int (*my_hello) () = NULL;
// allocate executable buffer
my_hello = mmap (0, sizeof(hello), PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
// copy code to buffer
memcpy (my_hello, hello, sizeof(hello));
__builtin___clear_cache (my_hello, my_hello + …Run Code Online (Sandbox Code Playgroud) 据我所知,.reserve()为向量保留内存而不实际修改其大小。但这是如何实现的呢?如何只保留内存而不分配内存呢?
编辑:我具体询问如何保留内存而不分配内存,而不是std::vector一般如何工作
C++ 理论上,如果我创建了一个名为Box. 我已经完成了所有变量和服务等。如果我被要求创建一个程序,我需要在其中创建一个动态数组的指针类变量。
为什么我需要使用 ex: Box **boxes= nullptr;?
另外,如果我被问到除了声明 Box 名称框之外,我还需要声明一个等于 0 的整数变量大小:
int size = 0;
Run Code Online (Sandbox Code Playgroud)
如何void addBox(Box** box, int &size)完成一个被调用的函数,以便每次调用它时我都会添加 box 的参数,并将另一个 box 添加到动态数组中?
仅供参考,我是编码新手,我需要将这个概念应用到课堂上的项目中。
在 K&R 书的第 8 章中,解释了malloc()如何在空闲内存中创建一个块列表,每个块都指向下一个块(粗略地说),并且不需要是连续的。另一方面,每个人都声称 malloc() 以连续的方式分配内存,我看到指针算术被大量使用,并且该网站上也有类似的问题。但是,始终没有任何官方消息来源。
我阅读了 C 参考资料,发现没有提及有关连续性的内容,我发现最好的内容是在 Linux 和 Windows 手册页上,他们在其系统上确保了此属性。
因此:malloc()是否仅在规范的现代系统上提供连续内存(例如,使指针算术合法),或者它是由 > C89 标准管辖的规则,而我天真地忽略了这一点?请提供官方参考。谢谢。
PS:这不仅仅是一个理论问题。我正在为旧的 DOS 系统编写一些代码,我需要确定malloc的正确用法。
编辑:我现在明白我的错误了,谢谢。也就是说,我仍然无法找到一个官方资源,其中明确指出单个malloc()调用返回连续内存(为什么此信息不简单地包含在标准库中的函数描述下方...... ?)例如,这里没有任何痕迹(https://en.cppreference.com/w/c/memory/malloc)。
我正在做一个程序,这里用户将输入一个字符串,如果用户输入“END”则程序将终止,否则它会要求另一个字符串和另一个但是字符串的大小是未知的,所以我必须使用动态内存分配以获取未知大小的字符串 这是我尝试过的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
bool on = true;
char *string;
while(on)
{
string = malloc(sizeof(char));
int i;
for (i = 0;; i++)
{
int n;
n = getchar();
if (n == '\n')
{
break;
}
else
{
string = realloc(string, sizeof(char));
string[i] = (char)n;
}
}
string[i] = '\0';
if (strcmp(string, "END") == 0)
{
on = false;
}
printf("Len: [%i],String = %s\n",i+1,string);
free(string);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我输入一个超过 25 左右的字符串,程序就会崩溃,它给了我这个 …
int *arr = new int(10);
int *arr = new int[10];
Run Code Online (Sandbox Code Playgroud)
是c++中动态内存分配的代码。但我不明白这两者之间有什么区别。
我试图学习 C++ 中的动态内存分配。我的程序可以编译并运行,但 Visual Studio 向我抛出这些警告。
他们的意思是什么?
Warning C28193 'ptr' holds a value that must be examined.
Warning C28182 Dereferencing NULL pointer. 'ptr' contains the same NULL value as
'new(1*4, nothrow)'
Run Code Online (Sandbox Code Playgroud)
我的代码:
#include <iostream>
#include <cstdint>
int main()
{
int* ptr = nullptr;
if (!ptr) {
ptr = new (std::nothrow) int32_t;
*ptr = 10;
}
std::cout << *ptr << "\n";
}
Run Code Online (Sandbox Code Playgroud) c++ warnings pointers new-operator dynamic-memory-allocation
如果我们重写 malloc 和 new 函数,我们可以跟踪第一个创建的指针。但是我如何跟踪和计算使用此分配的内存的其他指针和函数?(下面代码中的q指针)我应该覆盖赋值运算符和函数调用吗?如果是的话怎么办?如果解决方案是对类、构造函数和析构函数使用引用计数,我必须将所有普通指针定义更改为类类型?我不想更改源代码,只想包含一个库并计算指向已分配内存的指针。
\nint \xe2\x88\x97 p = (int\xe2\x88\x97)malloc(10 \xe2\x88\x97 sizeof (int)); \nint * q= p; \nRun Code Online (Sandbox Code Playgroud)\n c++ pointers memory-leaks reference-counting dynamic-memory-allocation
我看到回复告诉你使用Vec::with_capacity(some_size);,但感觉很奇怪。
为什么需要使用 Vector 来获取切片?我不想稍后增加切片,我只想使用普通切片,但大小在运行时是已知的,因为它来自某些程序输入。
我当前的解决方案是:
let mutable_zeroed_slice: &mut [u8] = &mut vec![0; size][..];
Run Code Online (Sandbox Code Playgroud)
有没有更好、更惯用且不费解的?
更新:您不能在包装在函数中之前使用该技巧,因为返回时您将丢失切片引用。
更新2:正如第一个回复中所指出的,Vec::with_capacity(size)[..]将为您提供一个具有给定容量的不可变空切片,这不是很有用。