Liste des Groupes | Revenir à cl c |
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);
}
Les messages affichés proviennent d'usenet.