C ++中的malloc问题 [英] malloc problem in C++

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

问题描述

我继承了一些我曾被委任转换为C ++的旧C代码。

我遇到了内存分配方面的一些问题。


首先,使用malloc是否有任何问题?在C ++中?我知道使用

" new"是优选的,但是现在我必须使用提供的东西。


其次,FILE指针需要分配内存吗?


我的具体问题是:(所有代码示例都缩写为

清晰度)


我有一个班级


class TObjectDef

{

public:

char * cpFileName;

int iRecordLength;

int iCurrentRecord;

long lRecordCount;

FILE * tpFilePointer

char cpRecBuf [2048];

}


已初始化


gsPpk =新TObjectDef();


然后,很久以后在代码中(大约80k行后),一个float数组是从这个结构初始化的



(定义

struct DataSegments

{

char caDataId [6];

int iDataCount;

float * fpArr1;

float * fpArr2;

}




喜欢这样


void vSetFpArray(struct DataSegments * spDataSeg,float * fpArr1,float

* fpArr2)

{

int iDataCount;

int iDataIndex;


iDataCount = spDataSeg-> iDataCount;

spDataSeg-> fpArr1 =(float *)malloc(sizeof(float) )* iDataCount);

spDataSeg-> fpArr1 =(float *)malloc(sizeof(float)* iDataCount);


for(iDataIndex = 0 ; iDataIndex< iDataCount; iDataIndex ++)

{

spDataSeg-> fpArr1 [iDataIndex] = fpArr1 [iDataIndex];

spDataSeg-> fpArr1 [iDataIndex] = fpArr1 [iDataIndex];

}

返回;

}


该函数被调用为


vSetFpArray(& sDataSegments [currInd],floatArray1,floatArray2);


其中变量定义为

静态DataSegments sDataSegments [512];

float floatArray1 [1024];

float floatArray2 [1024];


上面的函数调用就好了318次。在第319次迭代中,当malloc时,
被称为,它覆盖了之前定义的

类的一部分(这是该函数的全局)。 tpFilePointer从

NULL变为1和1。在第一个malloc之后,和2在第一个malloc之后。在第二个之后。 (例如,

指向内存地址:00000002)。


任何人都可以看到任何明显的问题我可能有这个问题

被忽视了? (除了全球课程等明显的问题之外。)如果这个问题没有任何意义,我会道歉,我很乐意提供

清晰度。


- Jason

I''ve inherited some old C code that I''ve been tasked to convert to C++.
I''ve been running into some problems with memory allocation.

First of all, is there any problems using "malloc" in C++? I know using
"new" is preferable, but for now I have to use what is provided.

Second, do FILE pointers need to have memory allocated?

My specific problem is this: (all code examples are abbreviated for
clarity)

I have a class

class TObjectDef
{
public:
char *cpFileName;
int iRecordLength;
int iCurrentRecord;
long lRecordCount;
FILE *tpFilePointer
char cpRecBuf[2048];
}

which is initialized

gsPpk = new TObjectDef();

then, much later in the code (about 80k lines later), a float array is
initialized from this structure

(definition
struct DataSegments
{
char caDataId[6];
int iDataCount;
float *fpArr1;
float *fpArr2;
}
)

like so

void vSetFpArray(struct DataSegments *spDataSeg, float *fpArr1, float
*fpArr2)
{
int iDataCount;
int iDataIndex;

iDataCount = spDataSeg->iDataCount;
spDataSeg->fpArr1 = (float *)malloc(sizeof(float) * iDataCount);
spDataSeg->fpArr1 = (float *)malloc(sizeof(float) * iDataCount);

for (iDataIndex = 0; iDataIndex < iDataCount; iDataIndex++)
{
spDataSeg->fpArr1[iDataIndex] = fpArr1[iDataIndex];
spDataSeg->fpArr1[iDataIndex] = fpArr1[iDataIndex];
}
return;
}

The function is called like

vSetFpArray(&sDataSegments[currInd],floatArray1,floatArray2);

where the variables are defined as
static DataSegments sDataSegments[512];
float floatArray1[1024];
float floatArray2[1024];

The function above gets called just fine 318 times. On the 319th iteration,
when "malloc" is called, it is overwriting part of the previously defined
class (which is global to this function). The tpFilePointer goes from being
NULL to being "1" after the first malloc, and "2" after the second. (as in,
pointing to memory address :00000002).

Can anyone see any obvious problems with all this that I may have
overlooked? (Besides the obvious problems with global classes and such.) I
apologize if this question didn''t make any sense, I''m happy to provide
clarity.

- Jason

推荐答案

Jason写道:
Jason wrote:

我已经继承了一些旧的C代码,我有责任将其转换为

C ++。我在内存分配方面遇到了一些问题。


首先,使用malloc是否存在任何问题?在C ++中?我知道

使用new是优选的,但现在我必须使用提供的。
I''ve inherited some old C code that I''ve been tasked to convert to
C++. I''ve been running into some problems with memory allocation.

First of all, is there any problems using "malloc" in C++? I know
using "new" is preferable, but for now I have to use what is provided.



不,'malloc''没有问题,除了它没有

初始化它分配的内存。 />

No, there are no problems with ''malloc'', except that it does not
initialise the memory it allocates.


其次,FILE指针需要分配内存吗?
Second, do FILE pointers need to have memory allocated?



FILE指针是您通过

''fopen''函数调用从系统获得的指针。你不需要分配任何东西。

FILE pointers are those you obtain from the system by means of the
''fopen'' function calls. You need not allocate anything.


我的具体问题是:(所有代码示例都缩写为

清晰度)


我有一个班级


班级TObjectDef

{

public:

char * cpFileName;

int iRecordLength;

int iCurrentRecord;

long lRecordCount;

FILE * tpFilePointer

char cpRecBuf [2048];

}


已初始化


gsPpk = new TObjectDef();
My specific problem is this: (all code examples are abbreviated for
clarity)

I have a class

class TObjectDef
{
public:
char *cpFileName;
int iRecordLength;
int iCurrentRecord;
long lRecordCount;
FILE *tpFilePointer
char cpRecBuf[2048];
}

which is initialized

gsPpk = new TObjectDef();



假设''gsPpk''被声明为指向TObjectDef的指针。

Assuming that ''gsPpk'' is declared as a pointer to TObjectDef.


那么多稍后在代码中(大约80k行后),一个浮点数组是从这个结构初始化的



(定义

struct DataSegments

{

char caDataId [6];

int iDataCount;

float * fpArr1;

浮动* fpArr2;

}




喜欢这样


void vSetFpArray(struct DataSegments * spDataSeg,float * fpArr1,float

* fpArr2)

{

int iDataCount;

int iDataIndex;


iDataCount = spDataSeg-> iDataCount;

spDataSeg-> fpArr1 =(float *)malloc(sizeof( float)* iDataCount);

spDataSeg-> fpArr1 =(float *)malloc(sizeof(float)* iDataCount);
then, much later in the code (about 80k lines later), a float array is
initialized from this structure

(definition
struct DataSegments
{
char caDataId[6];
int iDataCount;
float *fpArr1;
float *fpArr2;
}
)

like so

void vSetFpArray(struct DataSegments *spDataSeg, float *fpArr1, float
*fpArr2)
{
int iDataCount;
int iDataIndex;

iDataCount = spDataSeg->iDataCount;
spDataSeg->fpArr1 = (float *)malloc(sizeof(float) * iDataCount);
spDataSeg->fpArr1 = (float *)malloc(sizeof(float) * iDataCount);



我相信你的意思


spDataSeg-> fpArr2 =

$ b $在这里,不是吗?否则你分配给同一个变量两次,

这意味着你泄漏内存并保持''fpArr2''不变。

I am sure you meant

spDataSeg->fpArr2 =

here, didn''t you? Otherwise you assign to the same variable twice,
which means you leak memory and leave ''fpArr2'' unchanged.


>

for(iDataIndex = 0; iDataIndex< iDataCount; iDataIndex ++)

{

spDataSeg-> fpArr1 [iDataIndex] = fpArr1 [iDataIndex] ;

spDataSeg-> fpArr1 [iDataIndex] = fpArr1 [iDataIndex];
>
for (iDataIndex = 0; iDataIndex < iDataCount; iDataIndex++)
{
spDataSeg->fpArr1[iDataIndex] = fpArr1[iDataIndex];
spDataSeg->fpArr1[iDataIndex] = fpArr1[iDataIndex];



同样,这两个语句是_exactly_the_same_。你的意思是

要做什么


spDataSeg-> fpArr2 [iDataIndex] = fpArr2 [iDataIndex];


在第二行?

Again, those two statements are _exactly_the_same_. Did you mean
to do

spDataSeg->fpArr2[iDataIndex] = fpArr2[iDataIndex];

on the second line?


}

返回;

}


这个函数叫做


vSetFpArray(& sDataSegments [currInd],floatArray1,floatArray2);


其中变量被定义为

静态DataSegments sDataSegments [512];

float floatArray1 [1024];

float floatArray2 [1024];


上面的函数被调用了318次。在第319次

迭代时,当malloc时,被称为,它覆盖了先前定义的类(这是该函数的全局)的一部分。

tpFilePointer从NULL变为1。在第一个

malloc之后,以及2在第二个之后。 (如,指向内存地址

:00000002)。
}
return;
}

The function is called like

vSetFpArray(&sDataSegments[currInd],floatArray1,floatArray2);

where the variables are defined as
static DataSegments sDataSegments[512];
float floatArray1[1024];
float floatArray2[1024];

The function above gets called just fine 318 times. On the 319th
iteration, when "malloc" is called, it is overwriting part of the
previously defined class (which is global to this function). The
tpFilePointer goes from being NULL to being "1" after the first
malloc, and "2" after the second. (as in, pointing to memory address
:00000002).



< shrug无法分辨那是什么。

<shrug Impossible to tell what that''s due.


任何人都可以看到任何明显的我可能有这一切的问题

被忽视了? (除了全球课程的明显问题和

之外。)如果这个问题没有任何意义,我很抱歉,我很高兴

提供清晰度。
Can anyone see any obvious problems with all this that I may have
overlooked? (Besides the obvious problems with global classes and
such.) I apologize if this question didn''t make any sense, I''m happy
to provide clarity.



是的,我已经指出了明显的问题。非显而易见的是

最有可能超出发布的代码。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Yes, the obvious problems I''ve pointed out. The non-obvious are
most likely beyond the posted code.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


" Victor Bazarov" < v。******** @ comAcast.netwrote in message

news:fa ********** @ news.datemas.de ...
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fa**********@news.datemas.de...

>> [...]
首先,使用malloc是否有任何问题?在C ++中?我知道
使用new是优选的,但现在我必须使用提供的。
>>[...]
First of all, is there any problems using "malloc" in C++? I know
using "new" is preferable, but for now I have to use what is provided.



不,'malloc''没有问题,除了它没有

初始化它分配的内存。


No, there are no problems with ''malloc'', except that it does not
initialise the memory it allocates.



但是_does_分配内存,因为它锁定了堆上的内存为那个

特定的指针?

But it _does_ allocate memory, as in it locks memory on the heap for that
specific pointer?


FILE指针是您通过

''fopen''函数调用从系统获得的指针。你不需要分配任何东西。
FILE pointers are those you obtain from the system by means of the
''fopen'' function calls. You need not allocate anything.



谢谢,这就是我所怀疑的。

Thanks, that''s what I suspected.


>> [...]
gsPpk = new TObjectDef();
>>[...]
gsPpk = new TObjectDef();



假设''gsPpk''被声明为TObjectDef的指针。


Assuming that ''gsPpk'' is declared as a pointer to TObjectDef.



正确。

Correct.


>> [... ]
spDataSeg-> fpArr1 =(float *)malloc(sizeof(float)* iDataCount);
spDataSeg-> fpArr1 =(float *)malloc(sizeof(float)* iDataCount);
>>[...]
spDataSeg->fpArr1 = (float *)malloc(sizeof(float) * iDataCount);
spDataSeg->fpArr1 = (float *)malloc(sizeof(float) * iDataCount);



我相信你的意思


spDataSeg-> fpArr2 =

$ b $在这里,不是吗?否则你分配给同一个变量两次,

这意味着你泄漏内存并保持''fpArr2''不变。


I am sure you meant

spDataSeg->fpArr2 =

here, didn''t you? Otherwise you assign to the same variable twice,
which means you leak memory and leave ''fpArr2'' unchanged.



是的,你是对的。我更改了此示例的变量名称,并将

复制/粘贴到代码中。错过了更新第二个参考资料,我为这个混淆道歉。旧的变量名称是

专有应用程序的一部分,需要根据我自己的工作安全性来更改它们。

Yes, you are correct. I changed the variable names for this example, and
copy/pasted it into the code. Missed updating the second reference, I
apologize for the confusion. The old variable names are part of a
proprietary application, needed to change them for my own job security.


再次,这两个语句是_exactly_the_same_。你的意思是

要做什么


spDataSeg-> fpArr2 [iDataIndex] = fpArr2 [iDataIndex];


在第二行?
Again, those two statements are _exactly_the_same_. Did you mean
to do

spDataSeg->fpArr2[iDataIndex] = fpArr2[iDataIndex];

on the second line?



再次,我为混乱而道歉。

Again, I apoligize for the confusion.


>上面的函数被调用了318次。在第319次迭代时,当malloc时,被称为,它覆盖了以前定义的类的一部分(这是该函数的全局)。
tpFilePointer从NULL变为1。在第一个malloc之后,和2在第二个之后。 (如,指向内存地址
:00000002)。
>The function above gets called just fine 318 times. On the 319th
iteration, when "malloc" is called, it is overwriting part of the
previously defined class (which is global to this function). The
tpFilePointer goes from being NULL to being "1" after the first
malloc, and "2" after the second. (as in, pointing to memory address
:00000002).



< shrug不可能告诉那是什么。


<shrug Impossible to tell what that''s due.



关于我在调试时可能要寻找的事情的任何建议?

没有错误,直到它在代码中进一步下降,并且尝试初始化并使用该文件指针。

Any suggestions on things I could be looking for while debugging? There are
no errors until it gets further down in the code, and an attempt is made to
initialize and use that file pointer.


>任何人都可以看到我可能忽略的所有这些明显的问题吗? (除了明显的全球课程问题和
这样的问题。)我很抱歉,如果这个问题没有任何意义,我很高兴提供清晰度。
>Can anyone see any obvious problems with all this that I may have
overlooked? (Besides the obvious problems with global classes and
such.) I apologize if this question didn''t make any sense, I''m happy
to provide clarity.



是的,我已经指出了明显的问题。不明显的是

最有可能超出发布的代码。


Yes, the obvious problems I''ve pointed out. The non-obvious are
most likely beyond the posted code.



除了我的错别字,一切看起来还不错?谢谢你的建议,那就是我需要知道的。我仍然是C ++环境的新手,

是一个老Delphi程序员,因此外部视角总是值得赞赏。


- Jason

Besides my typos, everything looks fine? Thanks for your advice, that''s
what I needed to know. I am still relatively new to the C++ environment,
being an old Delphi programmer, so outside perspective is always
appreciated.

- Jason


Jason写道:
Jason wrote:

" Victor Bazarov" < v。******** @ comAcast.netwrote in message

news:fa ********** @ news.datemas.de ...
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fa**********@news.datemas.de...

>> [...]
首先,使用malloc是否有任何问题?在C ++中?我知道
使用new是优选的,但是现在我必须使用提供的内容。
>>[...]
First of all, is there any problems using "malloc" in C++? I know
using "new" is preferable, but for now I have to use what is
provided.


不,'malloc''没有问题,除了它没有初始化它分配的内存。


No, there are no problems with ''malloc'', except that it does not
initialise the memory it allocates.



但是_does_分配内存,因为它会锁定堆上的内存以获得特定指针?


But it _does_ allocate memory, as in it locks memory on the heap for
that specific pointer?



当然。如果你看一些流行的''new''和

''new []''的实现,你会发现内部他们实际使用''malloc''。

Sure. If you look at some popular implementations of ''new'' and
''new[]'', you''ll see that internally they use ''malloc'', actually.


[..]
[..]

>>上面的函数被调用很好318次。在第319次迭代时,当malloc时,被称为,它覆盖了以前定义的类的一部分(这是该函数的全局)。
tpFilePointer从NULL变为1。在第一个malloc之后,和2在第二个之后。 (如,指向记忆地址
00000002)。
>>The function above gets called just fine 318 times. On the 319th
iteration, when "malloc" is called, it is overwriting part of the
previously defined class (which is global to this function). The
tpFilePointer goes from being NULL to being "1" after the first
malloc, and "2" after the second. (as in, pointing to memory
address
00000002).


<耸耸肩不可能告诉那是什么。


<shrug Impossible to tell what that''s due.



关于我在调试时可能要查找的内容的任何建议?


Any suggestions on things I could be looking for while debugging?



如果''malloc''以某种方式覆盖(步入)一些memore,你仍然会使用其他对象来使用
,它可能只有两件事:堆

已损坏(您的程序写入超出动态分配对象的界限,因此交叉到内存中并不是真的

属于它,或者你的以前定义的类的一部分是某种方式

从你的控制下转移它现在是堆的一部分

''malloc''可以免费重复使用。


你应该做的是获得允许你调试的工具

内存分配和访问.National Purify,BoundsChecker,请记住


If ''malloc'' somehow overrides (steps onto) some memore you''re still
using for some other object, it could be only two things: the heap
is corrupt (your program writes beyond the bounds of a dynamically
allocated object, thus crossing over to memory that doesn''t really
belong to it, OR your "part of previously defined class" was somehow
transferred from under your control and it''s now part of the heap
that ''malloc'' is free to reuse.

What you should do is to get the tool that would allow you to debug
memory allocations and access. Rational Purify, BoundsChecker, come
to mind.


没有错误,直到它进一步下降代码和

尝试初始化并使用该文件指针。
There are no errors until it gets further down in the code, and an
attempt is made to initialize and use that file pointer.



内存访问问题是最糟糕的,如果你把它们置于你的
控制之下,你会非常高兴。

Memory access troubles are the worst, and if you get them under your
control, you''re going to be very happy.


>>任何人都可以看到我可能忽略的所有这些明显的问题吗? (除了明显的全球课程问题和
这样的问题。)我很抱歉,如果这个问题没有任何意义,我很乐意提供清晰度。
>>Can anyone see any obvious problems with all this that I may have
overlooked? (Besides the obvious problems with global classes and
such.) I apologize if this question didn''t make any sense, I''m
happy to provide clarity.


是的,我已经指出了明显的问题。非显而易见的是很可能超出发布的代码。


Yes, the obvious problems I''ve pointed out. The non-obvious are
most likely beyond the posted code.



除了我的错别字,一切看起来还不错?感谢您的建议,

这就是我需要知道的。我仍然相对较新的C ++

环境,作为一个老的Delphi程序员,因此外部视角

总是受到赞赏。


Besides my typos, everything looks fine? Thanks for your advice,
that''s what I needed to know. I am still relatively new to the C++
environment, being an old Delphi programmer, so outside perspective
is always appreciated.



这是你可能想要考虑的事情:不要使用直接由你分配的动态内存,而是使用标准容器。 />
切换到''std :: vector< float>''而不是''malloc''ed''float *''。

更容易调试,当然。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Here is something you might want to consider: do NOT use dynamic
memory allocated directly by you, instead use standard containers.
Switch to using ''std::vector<float>'' instead of ''malloc''ed ''float*''.
Easier to debug, for sure.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


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

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