C ++应用程序在调用memcpy()时崩溃 [英] C++ Application gets crashed on calling memcpy()

查看:717
本文介绍了C ++应用程序在调用memcpy()时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以找到解决方案。我真的对此感到困惑。 Plz帮助我。提前致谢。如果您需要更多详细信息,请通过回复告诉我。



我的应用程序是在Linux平台下开发的,它在调用memcpy()或者在调用时崩溃了在m_string.cpp中分配内存



我在上面提到的文件中实现了以下内容。但是,应用程序再次崩溃,来自gdb的日志同样指向以下函数,memcpy()或_int_malloc(),_ int_free()。能够找到崩溃的部分。



Can anyone plz find a solution for this. I'm really get confused over this. Plz help me out. Thanks in advance. Also plz let me know by replying, if you need more details regarding this.

My Application is developed under Linux platform, it gets crashed while calling memcpy() or on allocating memory in m_string.cpp

I have implemented the following in the above mentioned file. But again the application gets crashed and the logs from gdb points to the following functions, memcpy() or on _int_malloc(),_int_free() likewise. could able to find the part of crash.

void* operator new(size_t size) 
{       void *p=malloc(size);
//      Here we are checking whether p == 0 , if it is nothing will be done here 
//      after we are returning p over here
}


void *p = malloc(size); 
#define malloc(X) my_malloc( X, __FILE__, __LINE__, __FUNCTION__)



void *ptr = memcpy(p,q,size);
#define memcpy(X,Y,N) my_memcpy( X,Y,N, __FILE__, __LINE__, __FUNCTION__)



谢谢,

Ragu


Thanks,
Ragu

推荐答案

#define 是双重的:



A)您可能无意中更改了您不想要的系统部分并且无意更改



B)当 #define d代码的部分内发生错误时,您将无法获得指出确切信息的有用信息错误的位置



显然你正在遭受后者的痛苦,但它可能是由前者引起的:我的猜测是问题是由你的轻率造成的code> #defin
远离系统功能( memcpy ),不能少!



最简单的解决方案是:



I)不要使用 #define



如果由于某种原因你无法做到这一点,那么明智的替代品就是



II)如果你必须使用 #define ,请确保你使用的符号是唯一的,不要与全球符号冲突命名空间。根据经验,使用ALL_UPPERCASE符号而不是lower_case或CamelCase。并使用LONG_VARIABLE_NAMES!



如果这没有帮助,你也可以



III)尝试和找到该错误的实际原因:

1.编写一个包含对memcpy的调用的最小测试程序

2.替换所有 #define 该代码中的d符号与您打算在那里看到的实际代码。

3.删除或评论 #define s

4.编译并运行

5.a)如果不再有错误,那么#defines就是问题!

5.b)如果在运行时仍然存在错误,现在您可以看到它发生的位置!请发布错误消息以及导致它的实际代码
The problem with #define is twofold:

A) you may inadvertently change parts of the system you didn't want to and aren't meant to change

B) when an error occurs within parts of the #defined code, you won't get helpful information pointing out the exact location of the error

Apparently you're suffering from the latter, but it may be caused by the former: my guess is that the problem is caused by your thoughtless #defining away of a system function (memcpy), no less!

The easiest solution is:

I) don't use #define

If for whatever reason you cannot do that, a sensible alternate is

II) if you must use #define, make sure the symbols you use are unique and don't clash with symbols in the global namespace. As a rule of thumb use ALL_UPPERCASE symbols rather than lower_case or CamelCase. And use LONG_VARIABLE_NAMES!

If this doesn't help ,you could also

III) try and locate the actual cause of that error:
1. Write a minimal test program that contains a call to memcpy
2. Replace all #defined symbols in that code with the actual code you intend to see there.
3. remove or comment the #defines
4. compile and run
5.a) if there is no longer an error, then you #defines were the problem!
5.b) If there still is an error at runtime, now you can see where it happens! Please post the error message and the actual code that causes it


重新定义内置于语言,库或编译器中的内容......是天使们担心的地方。



话虽如此,如果你想让你的#define定义做我认为你打算做的事,你需要在你使用这些函数的地方之前移动它们。由于你正在搞乱系统头文件中定义的东西,你希望它们在你的任何代码之前都是#includes。



另外,如果你有不止一个编译单元(.cpp文件),然后可能需要在每个编译单元中进行相同的定义。



祝你好运 - 这是一个麻烦解决你要解决的任何问题。
Redefining things that are built into the language, libraries or compiler ... is a place where angels fear to tread.

Having said that, if you want your "#define" definitions to do what I think you intend, you need to move them before the place where you use the functions. Since you are messing with things that are defined in system headers, you want them to be after all #includes but before any of your code.

Also, if you have more than one compilation unit (.cpp file), then the definitions probably need to be made, the same, in each compilation unit.

Good luck - this is a messy solution to whatever problem you are trying to address.


除了用 #define 做的坏事之外,我注意到了你提到了一个变量 m_string 。如果这是一个类的成员变量,那么您可能已经引入了另一个问题:全局运算符new 的实现不会调用构造函数!这意味着,当您分配任何类的新实例时,所有成员变量都将处于未定义状态,并且尝试使用它们可能会导致未定义的行为。更糟糕的是,如果类包含虚函数,则不会初始化虚函数表指针。因此,对虚拟函数的任何调用都可能损坏您的堆栈并最终导致程序崩溃。
Aside from the bad things you did with #define, I've noticed you mentioned a variable m_string. If that is a member variable of a class, then you may have introduced another issue: your implementation of the global operator new does not call a constructor! That means, when you allocate a new instance of any class, all your member variables will be in an undefined state, and trying to use them may lead to undefined behaviour. What's worse, if the class contains virtual functions, the virtual function table pointer will not be initialized. As a result, any call to a virtual function may corrupt your stack and eventually crash your program.


这篇关于C ++应用程序在调用memcpy()时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆