由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)
相关主题
这个怎么allocate memory?effective C++里的memory pool 一问:
C++ Interview Question问个简单的memory allocation 的问题。
什么是OS Memory management and heap structure?array allocation in c
a=(char **)malloc(12*sizeof(char *)) 是什么意思?VC++ 中的 memory leak problem
再问一个free()的问题菜鸟请教C问题
C里面一个被分配了内存的指针如何知道分配了多少?why do we still use dynamic allocation?
一个函数指针的问题[合集] 谁给个stack-based allocation 的C++的例子?
内存管理的问题sizeof(string)
相关话题的讨论汇总
话题: ptr话题: int话题: malloc话题: memory话题: free
进入Programming版参与讨论
1 (共1页)
q*******i
发帖数: 353
1
【 以下文字转载自 CS 讨论区 】
发信人: qqwuweiyi (娓娓), 信区: CS
标 题: 在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的设计?
发信站: BBS 未名空间站 (Mon Mar 21 23:19:53 2011, 美东)
比如子函数(void)内用主函数通过参数传过来得指针malloc了空间,然后做
了一些操作之后,返回主函数,在主函数里面再free这个空间。这样做理论上应
该没问题,但是是不是风险很大?是很糟糕的设计?比较好的方法是什么呢?
(用指针的指针传递?)
d****p
发帖数: 685
2
In C I think it is OK.
In C++, this is not good design since the pointer may be casted to other
types and thus a subsequent free operation may under/over free memory.
C++'s rationale is when you acquire resource, you should immediately make
arrangement to let it be released sometime later, ie RAII. If you really
want a function to allocate memory which should be released in outter scope,
you may consider using shared pointer.

【在 q*******i 的大作中提到】
: 【 以下文字转载自 CS 讨论区 】
: 发信人: qqwuweiyi (娓娓), 信区: CS
: 标 题: 在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的设计?
: 发信站: BBS 未名空间站 (Mon Mar 21 23:19:53 2011, 美东)
: 比如子函数(void)内用主函数通过参数传过来得指针malloc了空间,然后做
: 了一些操作之后,返回主函数,在主函数里面再free这个空间。这样做理论上应
: 该没问题,但是是不是风险很大?是很糟糕的设计?比较好的方法是什么呢?
: (用指针的指针传递?)

H***a
发帖数: 735
3
Just one question - how does the main function know how to free the memory allocated in a black-box ?
p***o
发帖数: 1252
4
Most C code I read simply follow OO principles - allocation/deallocation are
managed by a pair of functions, e.g. if there is a function alloc_matrix,
then there should be free_matrix that release all related resources, and
there is usually a struct matrix holding everything.

scope,

【在 d****p 的大作中提到】
: In C I think it is OK.
: In C++, this is not good design since the pointer may be casted to other
: types and thus a subsequent free operation may under/over free memory.
: C++'s rationale is when you acquire resource, you should immediately make
: arrangement to let it be released sometime later, ie RAII. If you really
: want a function to allocate memory which should be released in outter scope,
: you may consider using shared pointer.

z****e
发帖数: 2024
5
OS知道。但是程序员得不到。

allocated in a black-box ?

【在 H***a 的大作中提到】
: Just one question - how does the main function know how to free the memory allocated in a black-box ?
q*******i
发帖数: 353
6
多谢,是不是这个还和编译器有关?另外如果没有shared pointer,比如在纯
C的环境下,用什么方法比较好

casted to other
free memory.
immediately make
If you really
released in outter scope,

【在 d****p 的大作中提到】
: In C I think it is OK.
: In C++, this is not good design since the pointer may be casted to other
: types and thus a subsequent free operation may under/over free memory.
: C++'s rationale is when you acquire resource, you should immediately make
: arrangement to let it be released sometime later, ie RAII. If you really
: want a function to allocate memory which should be released in outter scope,
: you may consider using shared pointer.

d****p
发帖数: 685
7
If it is C, then stick to your previous design with due care :-)
Unlike C++, C is not that emphasizing type safety and it is coder's
responsibility to get things right.

【在 q*******i 的大作中提到】
: 多谢,是不是这个还和编译器有关?另外如果没有shared pointer,比如在纯
: C的环境下,用什么方法比较好
:
: casted to other
: free memory.
: immediately make
: If you really
: released in outter scope,

d****p
发帖数: 685
8
That's error prone,still.
The thing is you need to pair the new/delete calls in every execution path,
really annoying. In C++, we could ensure the pointer will be released either
in local scope or in global scope. One reason I like C++ is its RAII
semantics.

Most C code I read simply follow OO principles - allocation/deallocation are
managed by a pair of functions, e.g. if there is a function alloc_matrix,
then there should be free_matrix that release all related resources, and
there is usually a struct matrix holding everything.
scope,

【在 p***o 的大作中提到】
: Most C code I read simply follow OO principles - allocation/deallocation are
: managed by a pair of functions, e.g. if there is a function alloc_matrix,
: then there should be free_matrix that release all related resources, and
: there is usually a struct matrix holding everything.
:
: scope,

H***a
发帖数: 735
9
Here is what I mean:
void foo( int ***ptr)
{
//malloc a 2D array
}
int main()
{
int **my_ptr;
foo(&my_ptr);
//How to free? you need to know the column size, right?
}

【在 z****e 的大作中提到】
: OS知道。但是程序员得不到。
:
: allocated in a black-box ?

D*******a
发帖数: 3688
10
if you don't know column size, how to use the array?

【在 H***a 的大作中提到】
: Here is what I mean:
: void foo( int ***ptr)
: {
: //malloc a 2D array
: }
: int main()
: {
: int **my_ptr;
: foo(&my_ptr);
: //How to free? you need to know the column size, right?

相关主题
C里面一个被分配了内存的指针如何知道分配了多少?effective C++里的memory pool 一问:
一个函数指针的问题问个简单的memory allocation 的问题。
内存管理的问题array allocation in c
进入Programming版参与讨论
H***a
发帖数: 735
11
Can also use like:
/** 1D **/
int * foo( int size )
{
return malloc( sizeof(int) * size);
}
/** 2D **/
int ** foo( int row, int col )
{
int ** ptr = (int **) malloc( sizeof(int *) * row);
int i = 0;
for (i=0; i < row; ++i)
{
ptr[i] = (int *) malloc( sizeof(int) * col);
}
return ptr;
}

【在 q*******i 的大作中提到】
: 多谢,是不是这个还和编译器有关?另外如果没有shared pointer,比如在纯
: C的环境下,用什么方法比较好
:
: casted to other
: free memory.
: immediately make
: If you really
: released in outter scope,

D*******a
发帖数: 3688
12
你这样出来的2d array在内存上不一定连续

【在 H***a 的大作中提到】
: Can also use like:
: /** 1D **/
: int * foo( int size )
: {
: return malloc( sizeof(int) * size);
: }
: /** 2D **/
: int ** foo( int row, int col )
: {
: int ** ptr = (int **) malloc( sizeof(int *) * row);

H***a
发帖数: 735
13
According to LZ "子函数(void)内用主函数通过参数传过来得指针malloc了空间,
然后做了一些操作之后,返回主函数,
在主函数里面再free这个空间", there I just created a critical example.
Caller - main function - might not need to use the array at all since all
the operations can be done in
subroutine. In this case, it's unfair to let main function free the resource.
That's just my 2 cents.

【在 D*******a 的大作中提到】
: 你这样出来的2d array在内存上不一定连续
H***a
发帖数: 735
14
then?

【在 D*******a 的大作中提到】
: 你这样出来的2d array在内存上不一定连续
q*******i
发帖数: 353
15
Here is what I mean:
void foo( int *ptr)
{
//malloc a 2D array
}
int main()
{
int *ptr
foo(ptr);
}
is there significant difference with your code?

【在 H***a 的大作中提到】
: Here is what I mean:
: void foo( int ***ptr)
: {
: //malloc a 2D array
: }
: int main()
: {
: int **my_ptr;
: foo(&my_ptr);
: //How to free? you need to know the column size, right?

b********e
发帖数: 58
16
If I were you, I may have the following code (assuming it's C):
typedef struct {
int row;
int column;
int* data;
} IArray;
IArray * IArrayInit(int row, int col)
{
IArray *ptr;
ptr = malloc(sizeof(IArray));
ptr->row = row;
ptr->column = col;
ptr->data = malloc(sizeof(int)*row*col);
}
void IArrayFree(IArray *ptr)
{
assert(ptr);
if (ptr->data ) free(ptr->data);
free(ptr);
}
In your main
IArray *myArray;
myArray = IArrayInit(5, 6)
/* do whatever you want with myArray */
IArrayFree(myArray);

【在 H***a 的大作中提到】
: Here is what I mean:
: void foo( int ***ptr)
: {
: //malloc a 2D array
: }
: int main()
: {
: int **my_ptr;
: foo(&my_ptr);
: //How to free? you need to know the column size, right?

t****t
发帖数: 6806
17
yes, significantly different -- your code returns nothing (allocated memory
is forever lost) while his code correctly returns pointer to allocated
memory.

【在 q*******i 的大作中提到】
: Here is what I mean:
: void foo( int *ptr)
: {
: //malloc a 2D array
: }
: int main()
: {
: int *ptr
: foo(ptr);
: }

s**********o
发帖数: 197
18
as long as logically good is okay la.

memory

【在 t****t 的大作中提到】
: yes, significantly different -- your code returns nothing (allocated memory
: is forever lost) while his code correctly returns pointer to allocated
: memory.

H***a
发帖数: 735
19
Logically it's not good either.

【在 s**********o 的大作中提到】
: as long as logically good is okay la.
:
: memory

q*******i
发帖数: 353
20
Sorry I am a rookie, I malloc memory use the same pointer (*ptr) transmitted
by main function. after the memory has been allocated in foo(), does the
(*ptr) in main automatically associated with the memory?
Thanks
void foo( int *ptr)
{
//malloc a 2D array using ptr
}
int main()
{
int *ptr
foo(ptr);
}

memory

【在 t****t 的大作中提到】
: yes, significantly different -- your code returns nothing (allocated memory
: is forever lost) while his code correctly returns pointer to allocated
: memory.

相关主题
VC++ 中的 memory leak problem[合集] 谁给个stack-based allocation 的C++的例子?
菜鸟请教C问题sizeof(string)
why do we still use dynamic allocation?突然发现现在很反感malloc了
进入Programming版参与讨论
n**f
发帖数: 121
21
You can either
a) malloc the memory in caller function instead of callee and manage free
call based on return code of callee
b) design the interface(aka class) to pair up malloc and free
c) ignore the mismatch if the malloc and free are logically or physically
close to each other.

【在 q*******i 的大作中提到】
: Sorry I am a rookie, I malloc memory use the same pointer (*ptr) transmitted
: by main function. after the memory has been allocated in foo(), does the
: (*ptr) in main automatically associated with the memory?
: Thanks
: void foo( int *ptr)
: {
: //malloc a 2D array using ptr
: }
: int main()
: {

c**b
发帖数: 2999
22
dynamic memory还是及时释放的好.

【在 q*******i 的大作中提到】
: Sorry I am a rookie, I malloc memory use the same pointer (*ptr) transmitted
: by main function. after the memory has been allocated in foo(), does the
: (*ptr) in main automatically associated with the memory?
: Thanks
: void foo( int *ptr)
: {
: //malloc a 2D array using ptr
: }
: int main()
: {

g**w
发帖数: 969
23
内存的情况稍微有点特殊。通常来说,
acquire 系统资源
do something
release 系统资源
是非常常见的。acquire后不release, 是个bug. 如果这个都hangle不好,没法写程序
了.
前面给出的 init/release pair就是常见的设计。

【在 q*******i 的大作中提到】
: Sorry I am a rookie, I malloc memory use the same pointer (*ptr) transmitted
: by main function. after the memory has been allocated in foo(), does the
: (*ptr) in main automatically associated with the memory?
: Thanks
: void foo( int *ptr)
: {
: //malloc a 2D array using ptr
: }
: int main()
: {

d****p
发帖数: 685
24
In your function, the ptr is valid after you allocate memory for it. After
the function returns, the ptr variable is destroyed. You need to change it
to void foo(int **ptr) to pass its value out.

transmitted

【在 q*******i 的大作中提到】
: Sorry I am a rookie, I malloc memory use the same pointer (*ptr) transmitted
: by main function. after the memory has been allocated in foo(), does the
: (*ptr) in main automatically associated with the memory?
: Thanks
: void foo( int *ptr)
: {
: //malloc a 2D array using ptr
: }
: int main()
: {

s**********o
发帖数: 197
25
不好意思,看错人了;上面另外一个code的不错。

【在 H***a 的大作中提到】
: Logically it's not good either.
1 (共1页)
进入Programming版参与讨论
相关主题
sizeof(string)再问一个free()的问题
突然发现现在很反感malloc了C里面一个被分配了内存的指针如何知道分配了多少?
c的问题(2)一个函数指针的问题
关于内存泄漏内存管理的问题
这个怎么allocate memory?effective C++里的memory pool 一问:
C++ Interview Question问个简单的memory allocation 的问题。
什么是OS Memory management and heap structure?array allocation in c
a=(char **)malloc(12*sizeof(char *)) 是什么意思?VC++ 中的 memory leak problem
相关话题的讨论汇总
话题: ptr话题: int话题: malloc话题: memory话题: free