摧毁物体 [英] destroying objects

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

问题描述

该程序只是从用户那里读取一份雇员名单,将其打印成

然后销毁分配的内存。我有一个函数

free_memory(void * ptr)将释放

指针ptr指向的内存。我在emplist中包含了一个指向此函数的指针

数据结构free_employee_list。这是一个很好的编程方式吗?

如果有这么多不同的数据结构会怎样。它是否足以在整个项目中拥有一个free_memory函数

并在每个数据结构中包含一个指向此函数的指针?


#include< stdio.h>

#include< stdlib.h>


typedef struct _employee

{

int eid;

float sal;

int age;

}员工;


typedef struct _emplist

{

int n;

员工* e;

void(* free_employee_list)();

} emplist;

void free_memory(void * ptr)

{

free(ptr);

printf(" \\\
Memory freed\\\
);

}

int main(void)

{

emplist * list;

int i;


list = malloc(sizeof( * list));

if(list == NULL)

{

fprintf(stderr,memory allocation failed\\\
);

返回1;

}


list-> fre e_employee_list = free_memory;

printf("输入员工人数\ n);

scanf("%d",& list-> n) ;


list-> e = malloc(sizeof(employee)* list-> n);


if(list-> ; e == NULL)

{

fprintf(stderr,内存分配失败\ n);

返回1; < (
}


for(i = 0;我<列表 - 将N; i ++)

{

printf("输入id,age,salary \ nn);;

scanf("%d% d%f",& list-> e [i] .eid,& list-> e [i] .age,& list-


> e [i] .sal);



}


for(i = 0; i< list-> n; i ++)

{

printf("员工ID:%d年龄:%d sal:%f \ n",list-> e [i] .eid,list-


> e [i] .age,list-> e [i] .sal);



}


list-> free_employee_list(list);


返回0;

}

解决方案



" pereges" < Br ***** @ gmail.com在消息新闻中写道:


这个程序只是从用户那里读取一份雇员名单,打印出来

然后销毁分配的内存。我有一个函数

free_memory(void * ptr)将释放

指针ptr指向的内存。我在emplist中包含了一个指向此函数的指针

数据结构free_employee_list。这是一个很好的编程方式吗?



这是(可以说)编程的一种好方法,但不是C. C'的语法不是'很丰富

足以支持嵌入在他们自己的结构中的大量间接功能指针集。这是一个容易达到不可读性的途径。


但是C ++中的相同想法很好。该语言将自动为您调用析构函数




-

免费游戏和编程好东西。
< a rel =nofollowhref =http://www.personal.leeds.ac.uk/~bgy1mmtarget =_ blank> http://www.personal.leeds.ac.uk/~bgy1mm


pereges写道:


这个程序只是从中读取一份雇员名单用户,打印它

然后销毁分配的内存。我有一个函数

free_memory(void * ptr)将释放

指针ptr指向的内存。我在emplist中包含了一个指向此函数的指针

数据结构free_employee_list。这是一个很好的编程方式吗?

如果有这么多不同的数据结构会怎样。它是否足以在整个项目中拥有一个free_memory函数

并在每个数据结构中包含一个指向此函数的指针?


#include< stdio.h>

#include< stdlib.h>


typedef struct _employee

{

int eid;

float sal;

int age;

}员工;


typedef struct _emplist

{

int n;

员工* e;

void(* free_employee_list)();

} emplist;


void free_memory(void * ptr)

{

free(ptr);

printf(" \\\
Memory freed\\\
);

}

int main(无效)

{

emplist * list;

int i;


list = malloc(sizeof(* list));

if(list == NULL)

{

fprintf(stderr,内存分配失败) \ n");

返回1;

}


list-> free_employee_list = free_memory;

printf("输入员工人数\ n);

scanf("%d",& list-> n);


list-> e = malloc(sizeof(employee)* list- > n);


if(list-> e == NULL)

{

fprintf(stderr," ;内存分配失败\ nn;);

返回1;

}


for(i = 0;我<列表 - 将N; i ++)

{

printf("输入id,age,salary \ nn);;

scanf("%d% d%f",& list-> e [i] .eid,& list-> e [i] .age,& list-


> e [i] .sal);



}


for(i = 0; i< list-> n; i ++)

{

printf("员工ID:%d年龄:%d sal:%f \ n",list-> e [i] .eid,list-


> e [i] .age,list-> e [i] .sal);



}


list-> free_employee_list(list);


返回0;

}



它看起来过于复杂。

我不明白为什么分配一系列结构然后释放它就不那么简单了。


/ * BEGIN employees_2.c * /


#include< stdio.h>

#include< stdlib.h>


int main(void)

{

struct {

int eid;

double sal;

int年龄;

} * e_ptr;

unsigned i,n;


puts(" / * BEGIN employees_2.c输出* / \ n");

put(输入员工人数);

if(scanf("%u",& n)! = 1){

put(忘了它。);

退出(EXIT_SUCCESS);

}

e_ptr = malloc(n * sizeof * e_ptr);

if(e_ptr == NULL){

pu ts(" e_ptr == NULL");

退出(EXIT_FAILURE);

}


for(i = 0 ;我!= n; ++ i){

puts(输入id,年龄,工资);

if(scanf("%d%d%lf",

& e_ptr [i] .eid,

& e_ptr [i] .age,

& e_ptr [i] .sal)! = 3)

{

put(忘了它。);

休息;

}

}

putchar(''\ n'');

for(n = 0; i!= n; ++ n){

printf(员工ID:%d年龄:%d sal:%f \ n,

e_ptr [n] .eid,

e_ptr [n] .age,

e_ptr [n] .sal);

}

免费(e_ptr) ;

puts(\ nArray is freed.\ n);

puts(" / * END employees_2.c output * /");

返回0;

}


/ * END employees_2.c * /

-

pete


2008年6月11日星期三11:11:24 -0700(太平洋时间),pereges< Br ***** @ gmail.com>

写道:


>这个程序只是读取一个来自用户的雇员列表,将其打印
然后销毁分配的内存。我有一个函数
free_memory(void * ptr),它将释放
指针ptr指向的内存。我在emplist
数据结构free_employee_list中包含了一个指向此函数的指针。这是一个好方法吗?



最好有一个函数,其唯一目的是调用

标准函数?我想不是。


让函数指针指向这个函数是个好主意吗?

除非你有多个这样的函数并且点〜

您决定使用哪一个从您调用的地方删除

函数。


这是一个把这个指针作为结构成员的好主意

_emplist?也许,因为你需要控制的所有其他东西...... b $ b struct _employee包含在struct _emplist中。


>如果有的话许多这样的不同数据结构。它是否足以在整个项目中拥有一个free_memory函数
并在每个数据结构中包含一个指向此函数的指针?



如果你只有一个函数,为什么还要在每个数据结构中浪费空间来指示它。


>
#include< stdio.h>
#include< stdlib.h>

typedef struct _employee
{

int eid;

float sal;

int age;
}员工;

typedef struct _emplist

int n;

employee * e;

void(* free_employee_list)();
} emplist;

void free_memory(void * ptr)
$
free(ptr);

printf(" ; \ nMemory freed\\\
);



如果你说什么内存被释放会很好。东西

喜欢

printf(\ nMemory分配在%p freed\ n,ptr);

free(ptr) ;


>}
int main(无效)

emplist * list;

int i;


list = malloc(sizeof(* list));

if(list == NULL)

{

fprintf(stderr,memory allocation failed\\\
);



指出哪个分配失败会很好。


返回1;



要便携,请使用EXIT_FAILURE。


}


list-> free_employee_list = free_memory;

printf("输入员工人数\ n);

scanf("%d",& list - > n);


list-> e = malloc(sizeof(employee)* list-> n);


if(list-> e == NULL)

{

fprintf(stderr,memory allocation failed\\\
);



此时您应该免费列出。


返回1;

}


for(i = 0; i< list-> n; i ++)

{

printf (输入id,年龄,薪水\ n);

scanf("%d%d%f",& list-> e [i] .eid,& list-> e [i] .age,& list-


>> e [i] .sal);



}


for(i = 0; i< list-> n; i ++)

{

printf("员工ID:%d年龄:%d sal:%f \ n",list-> e [i] .eid,list-


>> e [i] .age,list-> e [i] .sal);



}


list-> free_employee_list(list);



通过在释放列表之前不释放list-> e,你创建了一个内存

泄漏。


>

返回0;
}



删除del电子邮件


This program simply reads a list of employes from the user, prints it
and then destroys the allocated memory. I have a function
free_memory(void *ptr) which will free the memory pointed to by
pointer ptr. I included a pointer to this function in the emplist
data structure free_employee_list. Is this a good way to program ??
what if there are a number of such different data structures. Would it
suffice to have a single free_memory function throughout the project
and include a pointer to this function in every data structure ??

#include <stdio.h>
#include <stdlib.h>

typedef struct _employee
{
int eid;
float sal;
int age;
}employee;

typedef struct _emplist
{
int n;
employee *e;
void (*free_employee_list)();
}emplist;
void free_memory(void *ptr)
{
free(ptr);
printf("\nMemory freed\n");
}
int main(void)
{
emplist *list;
int i;

list = malloc(sizeof(*list));
if(list == NULL)
{
fprintf(stderr, "memory allocation failed\n");
return 1;
}

list->free_employee_list = free_memory;
printf("Enter number of employees\n");
scanf("%d", &list->n);

list->e = malloc(sizeof(employee) * list->n);

if(list->e == NULL)
{
fprintf(stderr, "memory allocation failed\n");
return 1;
}

for(i = 0; i < list->n; i++)
{
printf("Enter id, age, salary\n");
scanf("%d %d %f", &list->e[i].eid, &list->e[i].age, &list-

>e[i].sal);

}

for(i = 0; i < list->n; i++)
{
printf("Employe id: %d age: %d sal: %f\n", list->e[i].eid, list-

>e[i].age, list->e[i].sal);

}

list->free_employee_list(list);

return 0;
}

解决方案


"pereges" <Br*****@gmail.comwrote in message news:

This program simply reads a list of employes from the user, prints it
and then destroys the allocated memory. I have a function
free_memory(void *ptr) which will free the memory pointed to by
pointer ptr. I included a pointer to this function in the emplist
data structure free_employee_list. Is this a good way to program ??

It''s (arguably) a good way to program, but not in C. C''s syntax isn''t rich
enough to support a heavily indirected set of function pointers embedded in
their own structures. It''s an easy route to unreadability.

However the same idea in C++ is fine. The language will call destructors
automatically for you.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


pereges wrote:

This program simply reads a list of employes from the user, prints it
and then destroys the allocated memory. I have a function
free_memory(void *ptr) which will free the memory pointed to by
pointer ptr. I included a pointer to this function in the emplist
data structure free_employee_list. Is this a good way to program ??
what if there are a number of such different data structures. Would it
suffice to have a single free_memory function throughout the project
and include a pointer to this function in every data structure ??

#include <stdio.h>
#include <stdlib.h>

typedef struct _employee
{
int eid;
float sal;
int age;
}employee;

typedef struct _emplist
{
int n;
employee *e;
void (*free_employee_list)();
}emplist;
void free_memory(void *ptr)
{
free(ptr);
printf("\nMemory freed\n");
}
int main(void)
{
emplist *list;
int i;

list = malloc(sizeof(*list));
if(list == NULL)
{
fprintf(stderr, "memory allocation failed\n");
return 1;
}

list->free_employee_list = free_memory;
printf("Enter number of employees\n");
scanf("%d", &list->n);

list->e = malloc(sizeof(employee) * list->n);

if(list->e == NULL)
{
fprintf(stderr, "memory allocation failed\n");
return 1;
}

for(i = 0; i < list->n; i++)
{
printf("Enter id, age, salary\n");
scanf("%d %d %f", &list->e[i].eid, &list->e[i].age, &list-

>e[i].sal);

}

for(i = 0; i < list->n; i++)
{
printf("Employe id: %d age: %d sal: %f\n", list->e[i].eid, list-

>e[i].age, list->e[i].sal);

}

list->free_employee_list(list);

return 0;
}

It looks overly complicated to me.
I don''t understand why it isn''t just as simple
as allocating an array of structures and then freeing it.

/* BEGIN employees_2.c */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
struct {
int eid;
double sal;
int age;
} *e_ptr;
unsigned i, n;

puts("/* BEGIN employees_2.c output */\n");
puts("Enter number of employees");
if (scanf("%u", &n) != 1) {
puts("Forget about it.");
exit(EXIT_SUCCESS);
}
e_ptr = malloc(n * sizeof *e_ptr);
if (e_ptr == NULL) {
puts("e_ptr == NULL");
exit(EXIT_FAILURE);
}

for (i = 0; i != n; ++i) {
puts("Enter id, age, salary");
if (scanf("%d %d %lf",
&e_ptr[i].eid,
&e_ptr[i].age,
&e_ptr[i].sal) != 3)
{
puts("Forget about it.");
break;
}
}
putchar(''\n'');
for (n = 0; i != n; ++n) {
printf("Employe id: %d age: %d sal: %f\n",
e_ptr[n].eid,
e_ptr[n].age,
e_ptr[n].sal);
}
free(e_ptr);
puts("\nArray is freed.\n");
puts("/* END employees_2.c output */");
return 0;
}

/* END employees_2.c */
--
pete


On Wed, 11 Jun 2008 11:11:24 -0700 (PDT), pereges <Br*****@gmail.com>
wrote:

>This program simply reads a list of employes from the user, prints it
and then destroys the allocated memory. I have a function
free_memory(void *ptr) which will free the memory pointed to by
pointer ptr. I included a pointer to this function in the emplist
data structure free_employee_list. Is this a good way to program ??

Is it a good idea to have a function whose only purpose is to call a
standard function? I think not.

Is it a good idea to have a function pointer point to this function?
Not unless you have more than one such function and the point where
you decide which one to use is removed from the point where you call
the function.

Is it a good idea to have this pointer as a member of the struct
_emplist? Probably, since everything else you need to "control"
struct _employee is included in struct _emplist.

>what if there are a number of such different data structures. Would it
suffice to have a single free_memory function throughout the project
and include a pointer to this function in every data structure ??

If you only have one function, why bother wasting the space for a
pointer to it in every data structure.

>
#include <stdio.h>
#include <stdlib.h>

typedef struct _employee
{
int eid;
float sal;
int age;
}employee;

typedef struct _emplist
{
int n;
employee *e;
void (*free_employee_list)();
}emplist;
void free_memory(void *ptr)
{
free(ptr);
printf("\nMemory freed\n");

It would be nice if you said what memory was being freed. Something
like
printf("\nMemory allocated at %p freed\n", ptr);
free(ptr);

>}
int main(void)
{
emplist *list;
int i;

list = malloc(sizeof(*list));
if(list == NULL)
{
fprintf(stderr, "memory allocation failed\n");

It would be nice to indicate which allocation failed.

return 1;

To be portable use EXIT_FAILURE.

}

list->free_employee_list = free_memory;
printf("Enter number of employees\n");
scanf("%d", &list->n);

list->e = malloc(sizeof(employee) * list->n);

if(list->e == NULL)
{
fprintf(stderr, "memory allocation failed\n");

You ought to free list at this point.

return 1;
}

for(i = 0; i < list->n; i++)
{
printf("Enter id, age, salary\n");
scanf("%d %d %f", &list->e[i].eid, &list->e[i].age, &list-

>>e[i].sal);

}

for(i = 0; i < list->n; i++)
{
printf("Employe id: %d age: %d sal: %f\n", list->e[i].eid, list-

>>e[i].age, list->e[i].sal);

}

list->free_employee_list(list);

By not freeing list->e before freeing list, you have created a memory
leak.

>
return 0;
}


Remove del for email


这篇关于摧毁物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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