Pointers in For loops

Quick question: I'm a C # guy debugging a C ++ application, so I'm not used to memory management.

In the following code:

for(int i = 0; i < TlmMsgDB.CMTGetTelemMsgDBCount(); i++)
  {
    CMTTelemetryMsgCls* telm = TlmMsgDB.CMTGetTelemetryMsg(i);
    CMT_SINT32_Tdef id = telm->CMTGetPackingMapID();

    ManualScheduleTables.SetManualMsg(i,id);
    ManualScheduleTables.SetManExec(i,false);
  }

Am I losing memory on every b / c iteration CMTTelemetryMsgCls* telm = TlmMsgDB.CMTGetTelemetryMsg(i);? The CMTGetTelemetryMsg (int) method returns a pointer.

Do I need a " delete telm;" at the end of each iteration?

+3
source share
7 answers

There is no global answer to this question.

It really depends on how the person implemented the function with the return value.

  • It can return the variable allocated on the heap and expect you to delete it.
  • It can even return a pointer to an element variable in a heap
  • , TlmMsgDB

, : TlmMsgDB.CMTGetTelemetryMsg

+4

. TlmMsgDB.CMTGetTelemetryMsg , . - ,

+2

TlmMsgDB.CMTGetTelemetryMsg(i) CMTTelemetryMsgCls, . , delete , , , . , , - , .

+1

. , CMTGetTelemetryMsg, , , ... . , , , . TlmMsgDB, .

+1

. TlmMsgDB.CMTGetTelemetryMsg(i).

( "new Something()" ), , , - . , "get" - , , . , , , .

, .:) .;)

+1

, . , delete, - -, , . :

1) , , , , (, malloc, mmap, brk) , . , , , , CMTGetPackingMapID.

2) , " , ", - sysems strace/ltrace. , , , .

3) , ,

, ++ - , , . , , 10 , . (), malloc - .

+1

Try using a profiler. It can provide you with some useful memory management information inside the process that will be profiled. Or, even better, you can use something like SoftICE, this allows you to set a breakpoint on any function that allocates memory, such as HeapAllocEx. Using such a tool, you will definitely determine the actual behavior of the 3rd party code, even without sources.

0
source

Source: https://habr.com/ru/post/1739500/