malloc()失败了 [英] malloc() fail

查看:124
本文介绍了malloc()失败了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿人,

我有一个非常奇怪的问题。

我正在开发一个字符串类,只是为了好玩。我还在写一个

应用程序,它使用字符串类,并使用了很多。但是,在大约三十个String string = new String(blahblahblah)之后的
,我得到了

以下错误:


*** glibc检测到*** ./myapp:malloc():内存损坏(快):

0x080641f8 ***

======= Backtrace:=========

/lib/tls/i686/cmov/libc.so.6[0xb7bea962]

/ lib / tls / i686 /cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad)

/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff)

.. / ka [0x804d3e4]

../ka[0x804b72a]

../ka(__gxx_personality_v0+0x3f4)[0x804b244]

/ lib /tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]


应用程序崩溃的代码:


String :: String(const char * other)

{

/ *

为实际字符串数据分配空间,数据结构

包含一个int,其中包含字符串的长度,以及一个指向短数组的

指针。

* /

d = (数据*)malloc(sizeof(数据));


/ *

分配新的数据空间,这是应用程序崩溃的地方

* /

d-> data =(short *)malloc(sizeof(短));


/ *

将字符串的第一个字节设置为零,终止0字节。

* /

d-> size = 1;

memset(d-> data,0,sizeof(short));


/ *

并追加另一个char数组,附加追加功能。

* /

追加(其他);

}


我完全不喜欢我弄错了。你们有谁知道我在做什么

错了吗?

Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get the
following error:

*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff]
../ka[0x804d3e4]
../ka[0x804b72a]
../ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]

The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string, and a
pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));

/*
Allocate new data space, this is where the application crashes
*/
d->data = (short*)malloc(sizeof(short));

/*
Set the fist byte of the string to zero, the terminating 0 byte.
*/
d->size = 1;
memset(d->data, 0, sizeof(short));

/*
And append the other char array, with the append function.
*/
append(other);
}

I totally don''t get my mistake. Does anyone of you knows what I am doing
wrong?

推荐答案

医生写道:
The Doctor wrote:

我完全没有弄错。你们有谁知道我在做什么

错了吗?
I totally don''t get my mistake. Does anyone of you knows what I am doing
wrong?



如果没有看到整个代码,可能很难说。可能

是你的内存处理,字符串大小调整或其他任何错误。

Without seeing the entire code it may be difficult to say. There may
be an error in your memory handling, string resizing, or anything.


On 2008-09-21 13:02,The医生写道:
On 2008-09-21 13:02, The Doctor wrote:

嘿人,

我有一个非常奇怪的问题。

我正在开发一个字符串上课,只为了好玩。我还在写一个

应用程序,它使用字符串类,并使用了很多。但是,在大约三十个String string = new String(blahblahblah)之后的
,我得到了

以下错误:


*** glibc检测到*** ./myapp:malloc():内存损坏(快):

0x080641f8 ***

======= Backtrace:=========

/lib/tls/i686/cmov/libc.so.6[0xb7bea962]

/ lib / tls / i686 /cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad)

/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff)

./ ka [0x804d3e4]

./ka[0x804b72a]

./ka(__gxx_personality_v0+0x3f4)[0x804b244]

/ lib / tls / i686 / cmov / libc.so.6(__ libc_start_main + 0xe0)[0xb7b94450]


应用程序崩溃的代码:


字符串: :String(const char * other)

{

/ *

为实际字符串数据分配空间,即数据结构

包含一个int,其中包含字符串的长度,以及一个指向短数组的

指针。

* /

d=(Data *)malloc(sizeof(Data));


/ *

分配新的数据空间,这是应用程序崩溃的地方

* /

d-> data =(short *)malloc(sizeof(short));
Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get the
following error:

*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff]
./ka[0x804d3e4]
./ka[0x804b72a]
./ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]

The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string, and a
pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));

/*
Allocate new data space, this is where the application crashes
*/
d->data = (short*)malloc(sizeof(short));



到目前为止,我看不出代码有什么问题,你需要发布(至少
)数据的定义(请参阅有关如何发布

问题的常见问题解答)。


但问题是,为什么要使用malloc而不是新的?


-

Erik Wikstr ?? m

I can''t see anything wrong with the code so far, you need to post (at
least) the definition of Data (please see the FAQ on how to post a
question).

A question though, why use malloc and not new?

--
Erik Wikstr??m


On Sun,2008年9月21日12:08:34 +0000, Erik Wikstr ?? m写道:
On Sun, 21 Sep 2008 12:08:34 +0000, Erik Wikstr??m wrote:

On 2008-09-21 13:02,The Doctor写道:
On 2008-09-21 13:02, The Doctor wrote:

>嘿人,
我有一个非常奇怪的问题。
我正在开发一个字符串类,只是为了好玩。我也在编写一个
应用程序,它使用字符串类,并使用了很多。但是,在大约三十个String string = new String(blahblahblah)之后,我得到了以下错误:

*** glibc检测到*** ./myapp :malloc():内存损坏(快):
0x080641f8 ***
======= Backtrace:=========
/ lib / tls / i686 / cmov / libc.so.6 [0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad)
/usr/lib/libLCore.so (_ZN7StringC1EPKc + 0x4b)[0xb7eeafff] ./ka[0x804d3e4]
./ka[0x804b72a]
./ka(__gxx_personality_v0+0x3f4)[0x804b244)
/ lib / tls / i686 / cmov / libc.so.6(__ libc_start_main + 0xe0)[0xb7b94450]

应用程序崩溃的代码:

String :: String(const char * other)
>Hey people,
I have a really weird problem.
I am developing a string class, just for fun. I am also writing an
application, which uses the string class, and uses a lot of them. But,
after about thirty String string = new String("blahblahblah"), I get
the following error:

*** glibc detected *** ./myapp: malloc(): memory corruption (fast):
0x080641f8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7bea962]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x8d)[0xb7bebcad]
/usr/lib/libLCore.so(_ZN7StringC1EPKc+0x4b)[0xb7eeafff] ./ka[0x804d3e4]
./ka[0x804b72a]
./ka(__gxx_personality_v0+0x3f4)[0x804b244]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7b94450]

The code where the app crashes:

String::String(const char* other)
{
/*
allocate space for the actual string data, the Data structure
contains an int, which contains the length of the string,




and a


>指向一个短数组的指针。
* /
d =(Data *)malloc(sizeof(Data));

/ *
分配新的数据空间,这个是应用程序崩溃的地方* /
d-> data =(short *)malloc(sizeof(short));
> pointer to a short array.
*/
d = (Data*)malloc(sizeof(Data));

/*
Allocate new data space, this is where the application crashes */
d->data = (short*)malloc(sizeof(short));



到目前为止,我看不出代码有什么问题,你需要发布(至少
)数据的定义(请参阅有关如何发布

问题的常见问题解答)。


但问题是,为什么要使用malloc而不是新的?


I can''t see anything wrong with the code so far, you need to post (at
least) the definition of Data (please see the FAQ on how to post a
question).

A question though, why use malloc and not new?



数据:

struct Data

{

int size;

短*数据

};

new和delete []会阻止这个吗?


发布整个代码是不可能的,因为它很大。

Data:
struct Data
{
int size;
short* data
};
Would new and delete[] have prevented this then?

It is impossible to post the whole code, since it is huge.


这篇关于malloc()失败了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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