我试图从文本文件中读取所有内容.这是我写的代码.
#include <stdio.h>
#include <stdlib.h>
#define PAGE_SIZE 1024
static char *readcontent(const char *filename)
{
char *fcontent = NULL, c;
int index = 0, pagenum = 1;
FILE *fp;
fp = fopen(filename, "r");
if(fp) {
while((c = getc(fp)) != EOF) {
if(!fcontent || index == PAGE_SIZE) {
fcontent = (char*) realloc(fcontent, PAGE_SIZE * pagenum + 1);
++pagenum;
}
fcontent[index++] = c;
}
fcontent[index] = '\0';
fclose(fp);
}
return fcontent;
}
static void freecontent(char *content)
{
if(content) {
free(content);
content = NULL; …Run Code Online (Sandbox Code Playgroud) 在接受采访时我问了这个问题,答案是"计算机记忆".但到底在哪里......?它是随机存取存储器还是硬盘?
我今天遇到了这个错误,原因是我在调用后使用了从我的DLL内部分配的字符串FreeLibrary().
这是重现崩溃的简单示例.这在DLL中:
void dllFunc(char **output)
{
*output = strdup("Hello"); // strdup uses malloc
}
Run Code Online (Sandbox Code Playgroud)
这是在加载DLL的EXE中:
void exeFunc()
{
char *output;
dllFunc(&output);
std::string s1 = output; // This succeeds.
FreeLibrary(dll);
std::string s2 = output; // This crashes with access violation.
}
Run Code Online (Sandbox Code Playgroud)
我阅读了文档,FreeLibrary()但是在调用之后我找不到任何关于内存变得无效的内容.
编辑
我刚刚意识到我在使用VS2010工具链进行EXE时使用VS2008工具链(我使用VS2010作为IDE的两者,但你可以从项目设置中选择工具链).为DLL设置工具链到VS2010也删除了崩溃.
我在我的代码中使用动态内存分配,并在尝试删除指向子类的指针时遇到问题.我发现当我使用delete关键字时,最初分配的内存不会被释放.该功能适用于原始基类.
这是一个问题,因为我在arduino上运行代码,RAM很快被吃掉然后崩溃.
这是一些示例代码:
class Base
{
public:
Base(){
objPtr = new SomeObject;
}
~Base(){
delete objPtr;
}
SomeObject* objPtr;
};
class Sub : public Base
{
public:
Sub(){
objPtr = new SomeObject;
}
};
// this works fine
int main()
{
for (int n=0;n<100;n++) // or any arbitrary number
{
Base* basePtr = new Base;
delete basePtr;
}
return 0;
}
// this crashes the arduino (runs out of RAM)
int main()
{
for (int n=0;n<100;n++) // or …Run Code Online (Sandbox Code Playgroud) 我刚开始使用C++,现在我有一个非常基本的问题.
我写了两节课:
坐标:
#include <stdio.h>
class Coordinate {
private:
int x;
int y;
public:
Coordinate(int a, int b) {
x = a;
y = b;
};
void printTest() {
printf("%d %d\n", x, y);
};
};
Run Code Online (Sandbox Code Playgroud)
测试:
class Test {
private:
int q;
Coordinate *point;
public:
Test(int a, int b, int c) {
q = a;
point = new Coordinate(b, c);
};
virtual ~Test() {
delete point;
}
};
Run Code Online (Sandbox Code Playgroud)
主功能:
int main() {
Test *test = new Test(1, 2, 3);
// …Run Code Online (Sandbox Code Playgroud) 有几次我偶然发现了我有一个需要复制的指针容器的场景.
假设我们有以下类层次结构:
学生(基础班)
StudentService
StudentService类有一个std::vector<Student*> students字段和以下构造函数:
StudentService::StudentService(std::vector<Student*> students) {
// code
}
Run Code Online (Sandbox Code Playgroud)
仅使用std::vector::operator=运算符和写入是不正确的this->students = students,因为这只会复制指针地址,因此如果外部某人删除了这些指针所指向的对象,那么StudentService类就会受到影响.
解决方案是遍历students参数中的每个指针并创建一个新的动态对象,如下所示:
for(int i = 0; i < students.size(); i++) {
this->students.at(i) = new Student(*students.at(i));
}
Run Code Online (Sandbox Code Playgroud)
但即使这样也不合适,因为它会创建ONLY Student对象.我们知道学生可以是新生,索菲尔,初中或高级.所以这是我的问题:这个问题的最佳解决方案是什么?
我想有一种方法是在每个Student类中放置一个私有枚举字段,并有4个if-else语句检查它是什么类型的Student,然后根据它创建一个新的动态对象:
for(int i = 0; i < students.size(); i++) {
if(students.at(i).getType() == FRESHMAN) {
this->students.at(i) = new Freshman(*students.at(i));
} else if(students.at(i).getType() == SOPHMORE) {
this->students.at(i) = new Sophmore(*students.at(i));
} else if {
// and so on... …Run Code Online (Sandbox Code Playgroud) 我研究了很多静态和动态内存分配,但仍有一个混乱:
int n, i, j;
printf("Please enter the number of elements you want to enter:\t");
scanf("%d", &n);
int a[n];
for (i = 0; i < n; i++)
{
printf("a[%d] : ", i + 1);
scanf("%d", &a[i]);
}
Run Code Online (Sandbox Code Playgroud)
是否int a[n]都在静态或动态内存分配?
c memory-management dynamic-memory-allocation static-memory-allocation
的手册页中realloc()说:
该
realloc()函数将指向的存储块的大小更改ptr为size字节。从区域开始到新旧大小的最小值之间的内容将保持不变。如果新的大小大于旧的大小,则不会初始化添加的内存。
但是,手册页没有说明如果新大小小于旧大小会发生什么。例如,如果我有以下代码:
ptr = realloc(ptr, nsize); // Where nsize < the original size and ptr is of type void **
Run Code Online (Sandbox Code Playgroud)
如果原始大小为size,是否表示ptr + nsize + 1仍包含分配的条目?
任何帮助表示赞赏。
我想将所有指向释放内存位置的指针设置为NULL,这样就不可能有悬空指针或双精度释放。这在C中可能吗?
例如,我具有以下结构:
struct B {
int *arr;
unsigned int len;
};
struct A {
struct B *b;
};
// Freeing and setting them to NULL:
bool test_safe_free() {
struct A *a = malloc(sizeof(struct A));
struct B *b = malloc(sizeof(struct B));
b->arr = malloc(100 * sizeof(int));
b->len = 100;
a->b = b;
safe_free_A(&a);
return a == NULL && b == NULL;
}
void safe_free_B(struct B **b_ref) {
if (*b_ref != NULL) free((*b_ref)->arr);
(*b_ref)->arr = NULL;
free(*b_ref);
*b_ref = NULL;
}
void …Run Code Online (Sandbox Code Playgroud) 在这段代码中,当第一个数字是二维数组的大小时,我从文件中获取数字。
在我的代码中,我定义
char *filename=new char;
Run Code Online (Sandbox Code Playgroud)
(我必须使用 char *filename,这是练习..)一切正常,直到我尝试使用delete. 双方delete并delete[]给了我错误和崩溃我的程序。
这是我的完整代码:
#include <iostream>
#include <fstream>
using namespace std;
double **readmat(char *filename, int *size)/////question 2
{
ifstream read(filename);
cout << filename << endl;
if (!read)
{
cout << "Can't open file!" << endl;
exit(1);
}
read >> *size;
double **mat = new double*[*size];
for (int i = 0; i < *size; i++)
{
mat[i] = new double[*size];
for (int j = 0; j < *size; j++) …Run Code Online (Sandbox Code Playgroud) c++ char new-operator dynamic-memory-allocation delete-operator
c++ ×6
c ×5
pointers ×2
char ×1
containers ×1
dll ×1
file-io ×1
inheritance ×1
new-operator ×1
realloc ×1
winapi ×1