字符数组操作 - 编码风格 [英] character array manipulation - coding style

查看:76
本文介绍了字符数组操作 - 编码风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码应该做的事情(似乎工作正常)


取一个char *数组并从中删除第一个字符....简单如

那样。


所提到的char *数组是一个在线程中共享的''global''var。


声明为:char * buffer =(char *)malloc(bufferSize)

(后来我将其更改为使用C ++''new'',但它做同样的事情)

然后我将一个函数声明为:


void * readBuffer(void *)

{

blah blah


char * copy = malloc和stuff;


copy = buffer; //你会这样做吗?


blah(用副本做的东西)


* buffer ++; / *所以现在缓冲区丢失了它的第一个元素

缓冲区[0] * /

}

这是我的问题:


这个代码在样式方面有多糟糕?


做* buffer ++来削减第一个字符是否有意义

关闭?


它工作正常,但我想知道这样的事情是否会在现实世界中接受
(而不是学校作业)?


我应该自己清理''(缓冲区[0]之前现在位于

内存未使用)?


我自己如何清理?


我没有使用strncpy或任何其他''内置''的原因函数应该是问题本身的限制。


我会感激评论。

Here''s what my code is supposed to do (it seems to work fine)

Take a char* array and remove a first character from it....as simple as
that.

The mentioned char* array is a ''global'' var shared among threads.

Declared as : char* buffer = (char*) malloc(bufferSize)
(later I changed it to use C++ ''new'', but it does the same thing)
Then I have a function declared as :

void* readBuffer(void*)
{
blah blah

char* copy=malloc and stuff ;

copy=buffer; // would YOU do it this way ?

blah (do stuff with copy )

*buffer++; /* so now buffer lost its first element
at buffer[0] */
}
Here''s my question:

How bad is this code when it comes to style?

Does it make sense to do *buffer++ to cut the first character
off ?

It works just fine, but I''d like to know if something like this would
be acceptable in the real world ( as opposed to school assignment)?

Should I ''clean up'' after myself (buffer[0] from before now sits in
memory unused) ?

How would I clean up after myself ?

The reason I did not use strncpy or anyother ''built-in'' function is due
to restrictions in the problem itself.

I''d appreciate a comment.

推荐答案

am*********@gmail.com 说:

这里是我的代码应该做的(它看起来工作正常)


取一个char *数组并从中删除第一个字符。 ......就像

一样简单。


提到的char *数组是一个在线程中共享的全局var。


声明为:char * buffer =(char *)malloc(bufferSize)
Here''s what my code is supposed to do (it seems to work fine)

Take a char* array and remove a first character from it....as simple as
that.

The mentioned char* array is a ''global'' var shared among threads.

Declared as : char* buffer = (char*) malloc(bufferSize)



放弃演员。

Drop the cast.


(后来我改为使用C ++''new'',但它做同样的事情)
(later I changed it to use C++ ''new'', but it does the same thing)



不,它没有' T。确定您使用的是C还是C ++。如果您使用的是C ++,那么
你就错了。如果你想要C,new就不会像你那样工作

期望它。

No, it doesn''t. Decide whether you are using C or C++. If you are using C++,
you''re in the wrong place. If you want C, new doesn''t work like you might
expect it to.


然后我将一个函数声明为:


void * readBuffer(void *)

{

blah blah


char * copy = malloc和stuff;


copy = buffer; //你会这样做吗?
Then I have a function declared as :

void* readBuffer(void*)
{
blah blah

char* copy=malloc and stuff ;

copy=buffer; // would YOU do it this way ?



不会。我会写一些编译的东西。

No. I''d write something that compiles.


>

blah(用副本做的东西)


* buffer ++; / *所以现在缓冲区丢失了它的第一个元素

缓冲区[0] * /
>
blah (do stuff with copy )

*buffer++; /* so now buffer lost its first element
at buffer[0] */



领先的*没有意义。

The leading * is meaningless.


}


这是我的问题:


这段代码有多糟糕风格?
}
Here''s my question:

How bad is this code when it comes to style?



这是令人震惊的。问你的编译器,它会同意。

It''s appalling. Ask your compiler, and it will agree.


>

做* buffer ++来删除第一个字符是否有意义/>
关闭?
>
Does it make sense to do *buffer++ to cut the first character
off ?



No.

No.


它工作正常,但我想知道是否有类似的东西这将是现实世界中可接受的b $ b(与学校作业相对)吗?
It works just fine, but I''d like to know if something like this would
be acceptable in the real world ( as opposed to school assignment)?



你会对在现实世界中被认为可以接受的东西感到震惊。但如果

我是你的技术主管并且你告诉我这个,我会尽快让你上一个内部的

C课程。

You''d be astounded at what''s considered acceptable in the real world. But if
I were your tech lead and you showed me this, I''d get you onto an in-house
C course as soon as possible.


我应该在自己之后'清理''(缓冲区[0]之前现在位于

内存中未使用)?
Should I ''clean up'' after myself (buffer[0] from before now sits in
memory unused) ?



不,首先不要使用那个字节!没有必要。你有完全控制分配和复制

,对吗?所以,不要复制

那个字节。从下一个字节开始。


size_t len = strlen(输入); / *丢弃第一个字节 - ABC - " BC" * /

char * new = malloc(len);

if(new!= NULL)

{

memcpy(new,input + 1,len);

}

No, just don''t use that byte in the first place! There''s no need. You have
total control over the allocation and copying, right? So just Don''t Copy
That Byte. Start at the next byte.

size_t len = strlen(input); /* dropping first byte - "ABC" -"BC" */
char *new = malloc(len);
if(new != NULL)
{
memcpy(new, input + 1, len);
}


我将如何清理自己?
How would I clean up after myself ?



完成内存后,调用free()函数,传递malloc返回的

指针值。 />

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

When you are done with the memory, call the free() function, passing it the
pointer value returned by malloc.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


am ********* @ gmail.com 写道:
am*********@gmail.com wrote:

提到的char *数组是线程之间共享的全局var。
The mentioned char* array is a ''global'' var shared among threads.



听起来已经不错了,但好吧。

Sounding like a bad idea already, but alright.


声明为:char * buffer =( char *)malloc(bufferSize)

(后来我把它更改为使用C ++''new'',但是它做了同样的事情)


然后我有一个函数声明为:


void * readBuffer(void *)

{

blah blah


char * copy = malloc和stuff;


copy = buffer; //你会这样做吗?
Declared as : char* buffer = (char*) malloc(bufferSize)
(later I changed it to use C++ ''new'', but it does the same thing)

Then I have a function declared as :

void* readBuffer(void*)
{
blah blah

char* copy=malloc and stuff ;

copy=buffer; // would YOU do it this way ?



不,因为我不喜欢内存泄漏。

No, because I don''t like memory leaks.


blah(做复制的东西) )


* buffer ++; / *所以现在缓冲区丢失了它的第一个元素

缓冲区[0] * /

}
blah (do stuff with copy )

*buffer++; /* so now buffer lost its first element
at buffer[0] */
}



你平均缓冲区++,虽然*缓冲区++在这种情况下可能不会受到伤害 -

但它具有误导性和毫无意义。

You mean buffer++, although *buffer++ probably wouldn''t hurt in this context -
but it''s misleading and pointless.


Amer发布了:
Amer posted:

char * copy = malloc and stuff;


copy = buffer; //你会这样做吗?
char* copy=malloc and stuff ;

copy=buffer; // would YOU do it this way ?



我认为我们应该制作一个指针教程并将其添加到常见问题解答中 - 每天都有几张海报,只需要几个海报不知道指针是如何工作的。


-


Frederick Gotham


I think we should make a Pointers Tutorial and add it to the FAQ -- there are
several posters each day who simply don''t understand how pointers work.

--

Frederick Gotham


这篇关于字符数组操作 - 编码风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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