Память (символьного) массива освобождена путем выхода из объема?

Я не думаю так. Вы могли однако изменить Вас SQL для возврата столбца "CustomSort", который является результатом оператора выбора:

select
    (case when f = 'a' then 0 else 1 end) as CustomSort
from MyTable
11
задан Community 23 May 2017 в 12:22
поделиться

8 ответов

In this case no you do not need to call free. The value "str" is a stack based value which will be cleaned up when that particular method / scope is exited.

You only need to call free on values which are explicitly created via malloc.

18
ответ дан 3 December 2019 в 02:20
поделиться

Yes, it is "freed." (Not free()'ed, though.)

Since str is an automatic variable, it will only last as long as its scope, which is until the end of the function block.

Note that you only free() what you malloc().

3
ответ дан 3 December 2019 в 02:20
поделиться

It is automatically freed. If you didn't malloc it, you don't need to free it. But this has nothing to do with it being a "simple array of primitive types" - it would be freed if it was an array of structures. It is freed because it is a local variable.

Given that you are asking these very basic questions, I have to ask which C textbook are you using. Personally, I don't believe that you can usefully learn C without reading Kernighan & Ritchie's The C Programming Language, which explains all this stuff very clearly.

9
ответ дан 3 December 2019 в 02:20
поделиться

Yes, the memory is freed automatically once method1 returns. The memory for str is allocated on the stack and is freed once the method's stack frame is cleaned up. Compare this to memory allocated on the heap (via malloc) which you must explicitly free.

2
ответ дан 3 December 2019 в 02:20
поделиться

No, local variables of this sort are allocated on the stack, so when you return from the procedure the memory is available for the next function call, which will use the memory for its stack frame.

If you use malloc() the space is allocated on the heap, which must be explicitly freed.

1
ответ дан 3 December 2019 в 02:20
поделиться

I think it's freed not because it's primitives but that it's a local variable and that will be allocated on the stack not the heap. If you don't malloc it then you can't free it as far as I remember.

0
ответ дан 3 December 2019 в 02:20
поделиться

I'm a bit rusty in C/C++ lately, but I think you're right. As long as you didn't dynamically allocate that memory, you should be fine.

0
ответ дан 3 December 2019 в 02:20
поделиться

Yes, it is "freed" when it goes out of scope.
No, you don't have to explicitly free it.

The char array is allocated on the stack, so when you return from the function, that stack space is re-usable. You do not need to explicitly free the memory.

Good rule of thumb: if you malloc, you must free.

0
ответ дан 3 December 2019 в 02:20
поделиться
Другие вопросы по тегам:

Похожие вопросы: