for循环中的标签 [英] label inside for-loop
问题描述
嗨NG,
我正在尝试分配一些资源,在本例中是一个结构,其中包含一个缓冲区。当分配失败时,需要释放所有先前的分配
。当我尝试编译它时,我收到下一个警告:
resource-loop.c:38:警告:在复合结束时弃用标签
声明
有没有更好的方法呢?
或者这保证可以工作吗?
#include< stdlib.h>
#include< stdio.h>
struct foo
{
char * bar;
};
int main(无效)
{
int i;
struct foo * array [6];
for(i = 0; i< 6; ++ i)
{
if(!(array [i] = malloc(sizeof * array [i])))
{
fprintf(stderr,无法分配foo%d \ n,i);
goto err_malloc_foo;
}
>
if(!(array [i] - > bar = malloc(1024)))
{
fprintf(stderr,无法分配) bar%d \ n",i);
goto err_malloc_bar;
}
}
返回EXIT_SU CCESS;
for(; i> = 0; --i)
{
免费(array [i] - > bar);
err_malloc_bar:
免费(array [i]);
err_malloc_foo:
}
返回EXIT_FAILURE;
}
马克
-
<<删除电子邮件的del>>
Capstar写道:嗨NG,
我是尝试分配一些资源,在本例中是一个结构,其中包含一个缓冲区。当分配失败时,需要释放所有先前的分配。当我尝试编译它时,我得到了下一个警告:
resource-loop.c:38:警告:在复合结束时弃用标签
>有没有更好的方法呢?
或者这是否保证能够正常工作?
>#include< stdlib.h>
#include< stdio.h>
struct foo
{
char * bar;
};
int main(无效)
{
int i;
struct foo * array [6];
for(i = 0; i< 6; ++ i)
{
if( !(array [i] = malloc(sizeof * array [i])))
{
fprintf(stderr,无法分配foo%d \ n,i);
goto err_malloc_foo;
}
if(!(array [i] - > bar = malloc(1024)))
{
fprintf(stderr," ;无法分配bar%d \ n",i);
goto err_malloc_bar;
}
返回EXIT_SUCCESS;
for(; i> = 0; --i)
{
free(array [i] - > bar);
err_malloc_bar:
免费(array [i]);
err_malloc_foo:
返回EXIT_FAILURE;
}
Mark
好吧,我只是想到了做最后一件作品的另一种方式:
返回EXIT_SUCCESS;
while(i> = 0)
{
free(array [i] - > bar );
err_malloc_bar:
免费(array [i]);
err_malloc_foo:
--i;
}
返回EXIT_FAILURE;
警告现在消失了,因为标签不再是复合语句的结尾,所以这样做了。但为什么这会产生任何
的差异?在我看来,它在功能上根本没有变化。
或者我在这里遗漏了什么?
Mark
-
<<删除电子邮件的del>>
2004年6月8日星期二,Capstar写道:
C> Capstar写道:
C>>嗨NG,
C>>
C>>我正在尝试分配一些资源,在这个例子中是一个结构,
C>>包含缓冲区。当分配失败时,所有以前的分配都需要
C>>被释放。当我尝试编译它时,我收到下一个警告:
C>>
C>> resource-loop.c:38:警告:在复合结束时弃用标签
C>>声明
C>>
C>>有没有更好的方法呢?
C>>或者这是否保证能够正常工作?
C>>
C>> #include< stdlib.h>
C>> #include< stdio.h>
C>>
C>> struct foo
C>> {
C>> char * bar;
C>> };
C>>
C>> int main(void)
C>> {
C>> int i;
C>> struct foo * array [6];
C>>
C>> for(i = 0; i< 6; ++ i)
C>> {
C>> if(!(array [i] = malloc(sizeof * array [i])))
C>> {
C>> fprintf(stderr,无法分配foo%d \ n,i);
C>>转到err_malloc_foo;
C>> }
C>>
C>> if(!(array [i] - > bar = malloc(1024)))
C>> {
C>> fprintf(stderr,无法分配bar%d \ n,i);
C>> goto err_malloc_bar;
C>> }
C>> }
C>>
C>>返回EXIT_SUCCESS;
C>>
C>> for(; i> = 0; - i)
C>> {
C>> free(array [i] - > bar);
C>> err_malloc_bar:
C>>
C>> free(array [i]);
C>> err_malloc_foo:
C>> }
C>>
C>>返回EXIT_FAILURE;
C>> }
C>>
C>>
C>> Mark
C>>
C>
C>好的,我只是想到了做最后一件作品的另一种方式:
C>
C>返回EXIT_SUCCESS;
C>
C> while(i> = 0)
C> {
C> free(array [i] - > bar);
C> err_malloc_bar:
C>
C> free(array [i]);
C> err_malloc_foo:
C>
C> --i;
C> }
C>
C>返回EXIT_FAILURE;
C>
C>警告现在已经消失,因为标签不在最后,所以会发出警告
C>再一次的复合声明。但为什么这有什么不同呢?它在我看来,在功能上它根本没有变化。
C>
C>或者我错过了什么这里?
标签总是在声明之前。如果你最后需要一个标签
的化合物只是在标签后面加上一个空语句:
foo :;
harti
Capstar写道:Capstar写道:
Hi NG ,
我正在尝试分配一些资源,在这个例子中是一个包含缓冲区的结构。当分配失败时,需要释放所有先前的分配。当我尝试编译它时,我得到下一个
警告:
resource-loop.c:38:警告:在复合语句结尾处不推荐使用标签>
有没有更好的方法呢?
也许,这个版本怎么样?它没有goto',并且对
malloc的调用较少,希望能够实现更快的代码和更少的内存碎片。
#include< stdlib。 h>
#include< stdio.h>
struct foo {
char * bar;
};
#define NELEM 6
int main(无效)
{
int i;
struct foo * array;
if((array = malloc(sizeof * array * NELEM))== NULL )
返回EXIT_FAILURE;
for(i = 0; i< NELEM; i ++){
if((array) [i] .bar = malloc(1024))== NULL){
while( - i> = 0)
free(array [i] .bar );
免费(数组);
返回EXIT_FAILURE;
}
}
返回EXIT_SUCCESS;
}
HTH,
boa
[snip]
Hi NG,
I''m trying to allocate some resources, in this example a structure, with
containing a buffer. When a allocation failes, all previous allocations
need to be freed. When I try to compile it I get the next warning:
resource-loop.c:38: warning: deprecated use of label at end of compound
statement
Is there a better way of doing this?
Or is this guaranteed to work anyway?
#include <stdlib.h>
#include <stdio.h>
struct foo
{
char *bar;
};
int main(void)
{
int i;
struct foo* array[6];
for(i = 0; i < 6; ++i)
{
if(!(array[i] = malloc(sizeof *array[i])))
{
fprintf(stderr, "Unable to allocate foo%d\n", i);
goto err_malloc_foo;
}
if(!(array[i]->bar = malloc(1024)))
{
fprintf(stderr, "Unable to allocate bar%d\n", i);
goto err_malloc_bar;
}
}
return EXIT_SUCCESS;
for(; i >= 0; --i)
{
free(array[i]->bar);
err_malloc_bar:
free(array[i]);
err_malloc_foo:
}
return EXIT_FAILURE;
}
Mark
--
<<Remove the del for email>>
Capstar wrote:Hi NG,
I''m trying to allocate some resources, in this example a structure, with
containing a buffer. When a allocation failes, all previous allocations
need to be freed. When I try to compile it I get the next warning:
resource-loop.c:38: warning: deprecated use of label at end of compound
statement
Is there a better way of doing this?
Or is this guaranteed to work anyway?
#include <stdlib.h>
#include <stdio.h>
struct foo
{
char *bar;
};
int main(void)
{
int i;
struct foo* array[6];
for(i = 0; i < 6; ++i)
{
if(!(array[i] = malloc(sizeof *array[i])))
{
fprintf(stderr, "Unable to allocate foo%d\n", i);
goto err_malloc_foo;
}
if(!(array[i]->bar = malloc(1024)))
{
fprintf(stderr, "Unable to allocate bar%d\n", i);
goto err_malloc_bar;
}
}
return EXIT_SUCCESS;
for(; i >= 0; --i)
{
free(array[i]->bar);
err_malloc_bar:
free(array[i]);
err_malloc_foo:
}
return EXIT_FAILURE;
}
Mark
Ok, I just thought of another way of doing the last piece:
return EXIT_SUCCESS;
while(i >= 0)
{
free(array[i]->bar);
err_malloc_bar:
free(array[i]);
err_malloc_foo:
--i;
}
return EXIT_FAILURE;
The warning is gone now, which makes sence because the label is not at
the end of a compound statement anymore. But why does this make any
difference? It seems to me that functionally it didn''t change at all.
Or am I missing something here?
Mark
--
<<Remove the del for email>>
On Tue, 8 Jun 2004, Capstar wrote:
C>Capstar wrote:
C>> Hi NG,
C>>
C>> I''m trying to allocate some resources, in this example a structure, with
C>> containing a buffer. When a allocation failes, all previous allocations need
C>> to be freed. When I try to compile it I get the next warning:
C>>
C>> resource-loop.c:38: warning: deprecated use of label at end of compound
C>> statement
C>>
C>> Is there a better way of doing this?
C>> Or is this guaranteed to work anyway?
C>>
C>> #include <stdlib.h>
C>> #include <stdio.h>
C>>
C>> struct foo
C>> {
C>> char *bar;
C>> };
C>>
C>> int main(void)
C>> {
C>> int i;
C>> struct foo* array[6];
C>>
C>> for(i = 0; i < 6; ++i)
C>> {
C>> if(!(array[i] = malloc(sizeof *array[i])))
C>> {
C>> fprintf(stderr, "Unable to allocate foo%d\n", i);
C>> goto err_malloc_foo;
C>> }
C>>
C>> if(!(array[i]->bar = malloc(1024)))
C>> {
C>> fprintf(stderr, "Unable to allocate bar%d\n", i);
C>> goto err_malloc_bar;
C>> }
C>> }
C>>
C>> return EXIT_SUCCESS;
C>>
C>> for(; i >= 0; --i)
C>> {
C>> free(array[i]->bar);
C>> err_malloc_bar:
C>>
C>> free(array[i]);
C>> err_malloc_foo:
C>> }
C>>
C>> return EXIT_FAILURE;
C>> }
C>>
C>>
C>> Mark
C>>
C>
C>Ok, I just thought of another way of doing the last piece:
C>
C> return EXIT_SUCCESS;
C>
C> while(i >= 0)
C> {
C> free(array[i]->bar);
C>err_malloc_bar:
C>
C> free(array[i]);
C>err_malloc_foo:
C>
C> --i;
C> }
C>
C> return EXIT_FAILURE;
C>
C>The warning is gone now, which makes sence because the label is not at the end
C>of a compound statement anymore. But why does this make any difference? It
C>seems to me that functionally it didn''t change at all.
C>
C>Or am I missing something here?
A label always stands before a statement. If you need a label at the end
of a compound just put a null-statement after the label:
foo: ;
harti
Capstar wrote:Capstar wrote:Hi NG,
I''m trying to allocate some resources, in this example a structure,
with containing a buffer. When a allocation failes, all previous
allocations need to be freed. When I try to compile it I get the next
warning:
resource-loop.c:38: warning: deprecated use of label at end of
compound statement
Is there a better way of doing this?
Maybe, how about this version? It has no goto''s and has fewer calls to
malloc, hopefully leading to faster code and less fragmented memory.
#include <stdlib.h>
#include <stdio.h>
struct foo {
char *bar;
};
#define NELEM 6
int main(void)
{
int i;
struct foo* array;
if( (array = malloc(sizeof *array * NELEM)) == NULL)
return EXIT_FAILURE;
for(i = 0; i < NELEM; i++) {
if( (array[i].bar = malloc(1024)) == NULL) {
while(--i >= 0)
free(array[i].bar);
free(array);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
HTH,
boa
[snip]
这篇关于for循环中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!