| Deutsch English Français Italiano |
|
<87zfi8zzt1.fsf@onesoftnet.eu.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Ar Rakin <rakinar2@onesoftnet.eu.org>
Newsgroups: comp.lang.c
Subject: Re: Which code style do you prefer the most?
Date: Wed, 26 Feb 2025 17:54:34 +0600
Organization: A noiseless patient Spider
Lines: 123
Message-ID: <87zfi8zzt1.fsf@onesoftnet.eu.org>
References: <vpkmq0$21php$1@dont-email.me>
<f7esrj5n1vdfo7jrko6hhojuondnqdho88@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Wed, 26 Feb 2025 12:54:35 +0100 (CET)
Injection-Info: dont-email.me; posting-host="3819d60e36b63f24b1100dea27559741";
logging-data="2675633"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18GdvoXxGYnnrHexkEA/qz4yT0SYPoqaPs="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:Cu0tGrwSY2sKM8H4m8tc1AeDPjM=
sha1:GG0YoTkoww5XI41NT0fU1/We9JE=
Bytes: 4017
Rosario19 <Ros@invalid.invalid> writes:
> On Tue, 25 Feb 2025 21:15:12 +0600, Ar Rakin
> <rakinar2@onesoftnet.eu.org> wrote:
>
>>Hello there,
>>
>>I've been writing C code for a long time, in different styles, but
>>always wanted to know which code style most people prefer. This is all
>>about that question. Which one of the following do you prefer the most?
>>
>>1. GNU Style
>>
>>---------
>>
>>#include <stddef.h>
>>#include <stdbool.h>
>>
>>#include "malloc_internals.h"
>>
>>struct malloc_chunk
>>{
>> struct malloc_chunk *next; /* The next chunk. */
>> struct malloc_chunk *prev; /* The previous chunk. */
>> bool is_free; /* Whether this chunk is usable. */
>> size_t size; /* The size of this chunk. */
>>};
>>
>>static inline void *
>>get_chunk_ptr (struct malloc_chunk *chunk)
>>{
>> return (void *) (chunk + 1);
>>}
>>
>>void *
>>malloc (size_t size)
>>{
>> struct malloc_chunk *chunk = mm_find_chunk (size, MM_CHUNK_FREE);
>>
>> if (chunk)
>> return get_chunk_ptr (chunk);
>>
>> chunk = mm_alloc_chunk (size);
>>
>> if (!chunk)
>> return NULL;
>>
>> return get_chunk_ptr (chunk);
>>}
>>
>>---------
>>
>>
>>2. Linux Style
>>
>>---------
>>
>>#include <stddef.h>
>>#include <stdbool.h>
>>
>>#include "malloc_internals.h"
>>
>>struct malloc_chunk {
>> struct malloc_chunk *next; /* The next chunk. */
>> struct malloc_chunk *prev; /* The previous chunk. */
>> bool is_free; /* Whether this chunk is usable. */
>> size_t size; /* The size of this chunk. */
>>};
>>
>>static inline void *get_chunk_ptr(struct malloc_chunk *chunk)
>>{
>> return (void *) (chunk + 1);
>>}
>>
>>void *malloc(size_t size)
>>{
>> struct malloc_chunk *chunk = mm_find_chunk(size, MM_CHUNK_FREE);
>>
>> if (chunk)
>> return get_chunk_ptr(chunk);
>>
>> chunk = mm_alloc_chunk(size);
>>
>> if (!chunk)
>> return NULL;
>>
>> return get_chunk_ptr(chunk);
>>}
>>
>>---------
>>
>>3. Other Styles?
>>
>>Please show an example!
>>
>>Thank you.
>
> above it seems the same stile
>
> #include <stddef.h>
> #include <stdbool.h>
>
> #include "malloc_internals.h"
>
> struct malloc_chunk {
> struct malloc_chunk *next; /* The next chunk. */
> struct malloc_chunk *prev; /* The previous chunk. */
> bool is_free;/*Whether this chunk is usable.*/
> size_t size; /* The size of this chunk. */
> };
>
> static inline void *get_chunk_ptr(struct malloc_chunk *chunk)
> {return (void *)(chunk+1);}
>
> void *malloc(size_t size)
> {struct malloc_chunk *chunk = mm_find_chunk(size, MM_CHUNK_FREE);
>
> if( chunk)return get_chunk_ptr(chunk);
> if(!(chunk = mm_alloc_chunk(size))) return NULL;
> return get_chunk_ptr(chunk);
> }
Your style seems much more compact.