我正在进行内存分配/释放功能,
这很简单
inline void safedealloc ( void *mem )
{
if ( mem ) { free( mem ); mem = NULL; }
}
Run Code Online (Sandbox Code Playgroud)
它工作正常..然而,在使用它几次与程序后,
我注意到它在类似的东西中调用它
safedealloc( (char *)name );
safedealloc( (char *)name );
Run Code Online (Sandbox Code Playgroud)
导致错误.. 第一次调用后名称应该为null.但它没有,第二次使用无效指针ofc导致错误..为什么不将null分配给名称,因为它应该?
PS:使用malloc正确分配名称,并使用有效的大小和内容
我有一个代码,其中字符数组由整数填充(转换为char数组),并由另一个函数读取,该函数将其重新转换回整数.我使用以下函数来转换为char数组:
char data[64];
int a = 10;
std::string str = boost::lexical_cast<std::string>(a);
memcpy(data + 8*k,str.c_str(),sizeof(str.c_str())); //k varies from 0 to 7
Run Code Online (Sandbox Code Playgroud)
并使用以下方法重新转换回字符:
char temp[8];
memcpy(temp,data+8*k,8);
int a = atoi(temp);
Run Code Online (Sandbox Code Playgroud)
这一般工作正常,但是当我尝试将其作为涉及qt(ver 4.7)的项目的一部分时,它编译得很好并且在尝试使用memcpy()读取时给出了分段错误.请注意,分段故障仅在读取循环中发生,而不是在写入数据时发生.我不知道为什么会这样,但我希望通过任何方法完成它.
那么,还有其他任何我可以使用的函数可以接受字符数组,第一位和最后一位并将其转换为整数.然后我根本不必使用memcpy().我想要做的是这样的事情:
new_atoi(data,8*k,8*(k+1)); // k varies from 0 to 7
Run Code Online (Sandbox Code Playgroud)
提前致谢.
假设我有ABC*类型的指针和另一个XYZ*类型的指针,它们都来自一个公共父类.
如果我通过显式地将XYZ*分配给ABC*,那么如果我打电话会发生什么
删除abc; // abc的类型为XYZ*
我会得到任何例外或它会正常工作吗?
我已经尝试了上面的代码,它不会崩溃.那么有谁能告诉我在什么情况下会删除抛出异常/故障/崩溃等?
delet'ing指针崩溃程序的情况是什么?如果它们都定义了自定义析构函数,它们会崩溃吗?
编辑:这是我的测试代码,没有任何崩溃
class ABC
{
public:
int a;
int b;
int c;
};
class XYZ
{
public:
double a;
double b;
double c;
};
int main()
{
ABC* abc = new ABC();
XYZ* xyz = (XYZ*)abc;
delete xyz;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
PS:我在Windows平台上,如果有帮助的话.
EDIT2:好的,所以在阅读之后,我将问题改为,何时删除指针会导致崩溃(不包括未定义的行为)?
编辑3:调用删除时会发生什么?会叫谁的析构函数?
我只是在c语言上试验一下你能回答我关于我写的程序的问题
void main()
{
char *p,; // created a pointer pointing to string
p = (char *) malloc(50); // dynamically create 50 bytes.
strcpy(p, "this code is written about the dynamic allocation");
p += 20;
free(p);
}
Run Code Online (Sandbox Code Playgroud)
现在有人能告诉我free(p)语句的效果是什么,将释放最后30个字节并用于将来的内存分配.什么是输出?
我试图用c ++实现一个简单的链表.我可能在某处犯了一个愚蠢的错误.通过这个我想学习C++中的类和指针.对于代码
#include <iostream>
using namespace std;
class node
{
public:
node* next;
int data;
node(int d);
void append(int d);
};
node::node(int d)
{
data = d;
next = NULL;
}
void node::append(int d)
{
node nw = node(d);
if(next==NULL)
next = &nw;
else
{
node *n = next;
while((*n).next!=NULL)
{
n = (*n).next;
}
(*n).next = &nw;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到81作为1旁边的节点.
int main()
{
node n = node(1);
n.append(3);
n.append(2);
n.append(81);
n = *(n.next);
cout<< n.data << '\n';
}
Run Code Online (Sandbox Code Playgroud)
请帮我弄清楚我在哪里弄错了.
我的程序中出现了一个非常令人困惑的错误.我想我可能有两个同一类的不同对象,我认为我有相同的对象.这很令人困惑,因为我正在处理一个非常大的框架,在这个框架中获取指向我需要的对象的指针并不简单.
我的问题是,如果我有一个继承自Base的Derived类,并且我有一个指向Derived对象的指针,我怎样才能从派生对象中获取Base对象的地址?我正在使用基类的源代码,并在Base中打印出"this"的地址.在我的代码的另一部分中,检索指向Derived的指针.我需要能够通过Derived对象打印Base对象的地址,以确定我是否有指向我需要的特定Derived对象的指针.
我可能对地址如何在继承中使用C++有很大的误解.也许它们只是一个对象,而不是链接到派生对象的基础对象?
非常感谢你
编辑:我想这样做的原因纯粹是为了调试.问题是我使用的代码库不包含很多接口或受保护的成员,所以我不得不编辑源代码来访问某些信息.但是,当我调用我使用特定的Derived指针添加到Base类的方法时,我的程序崩溃了.我需要能够在这种情况下打印基础对象的地址,以便我可以确定这是否是正确的对象或我是否收到此错误,因为我实际上有一个指向错误对象的指针.我意识到我可以将代码添加到派生类中以使其打印其地址,但我只是想知道是否有可能在不编辑源代码的情况下获取地址.谢谢
我想要做的是有一个类包含第二个类的函数指针的映射,但第二个类的名称应该无关紧要(不能硬编码到第一个类)我真的希望能够实现这没有使用宏.我已经跟随了关于函数指针的learncpp.com中的示例,但是当在类之间传递它们时,我真的迷路了!我的尝试如下:
#include <map>
class Class1;
typedef double(Class1::*memFunc)();
class Class1
{
private:
std::map<std::string, memFunc> funcMap;
public:
void addFunc(std::string funcName, memFunc function)
{
funcMap.insert(std::pair<std::string, memFunc>(funcName, function));
}
};
class MyClass
{
public:
MyClass()
{
//How do I add member function getValue() to Class1?
class1.addFunc("new function", getValue());
}
double getValue()
{
return 0;
}
private:
Class1 class1;
};
Run Code Online (Sandbox Code Playgroud) 之前boost::shared_ptr,从函数返回堆分配指针被认为是一种不好的做法,因为调用者需要记住free()该对象吗?
或者,它被认为是"正常"?
我今天遇到了一个关于局部变量的问题.我了解到......
int * somefunc()
{
int x = 5;
return &x;
}
int * y = somefunc();
//do something
Run Code Online (Sandbox Code Playgroud)
是坏的,不安全的等等.我想这个案子是一样的......
int * somefunc()
{
int * x = new int;
x = 5;
return x;
}
int * y = somefunc();
//do something
delete y;
Run Code Online (Sandbox Code Playgroud)
我一直认为这是最安全的印象,因为当它返回时x的地址保持在范围内.但是,我现在有了第二个想法,我认为这会导致内存泄漏和其他问题,就像第一个例子那样.有人可以帮我确认吗?
我已经在objective-c中编程了一段时间,但遗憾的是我从未深入研究过C和内存指针,尽管我对它们有一些基本的了解.我正在使用一系列CLLocationCoordinate2D结构,我正在试图找出如何附加到数组.首先,我得到了
NSString *aString; //a bunch of coordinates
CLLocationCoordinate2d *coordinates;
int length;
doSomethingCool(aString, &coordinates, &length);
Run Code Online (Sandbox Code Playgroud)
在我做了一些很酷的事情之后,我想把它保存在一个类变量中.如果我只是做类似的事情
points = newPoints
Run Code Online (Sandbox Code Playgroud)
points包含适当的内容.但是,如果我尝试做这样的事情:
points = malloc(sizeof(CLLocationCoordinate2D) * length);
points[0] = *newPoints;
Run Code Online (Sandbox Code Playgroud)
点与来自不同内容结束newPoints.
最终,我的目标是能够追加到分根据长度,但我不会是能够做到这一点,如果我不能得到上面的代码工作.我究竟做错了什么?