Skip to content

Commit 041f261

Browse files
committed
Memory management and variable arguments
Create snippets for the use of malloc/calloc/realloc, also pairing the first two with the corresponding free. Also add a similar snippet for managing variable number of arguments in a function, from the creation in the va_list to the destruction with va_end. Also add some small snippets like scanf() or seed_rand() Small changes in some other snippets, like a new alias for _Static_assert(), no comma in printf to be like fprintf.
1 parent caa0e0e commit 041f261

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

neosnippets/c.snip

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ abbr if () {}
66
${0:TARGET}
77
}
88

9+
# No head option in else/elseif so it can be expanded after "}"
910
snippet else
10-
options head
11+
abbr else {}
1112
else {
1213
${0:TARGET}
1314
}
1415

1516
snippet elseif
16-
options head
17+
abbr else () {}
1718
else if (${1:#:condition}) {
1819
${0:TARGET}
1920
}
@@ -37,14 +38,16 @@ abbr for () {}
3738
# Counter based for's (don't ask for the type or count start)
3839
snippet fori
3940
options head
40-
abbr for(int x;...){}
41+
abbr for (int x;...; x++) {}
4142
for (int ${1:i} = 0; $1 < ${2}; $1++) {
4243
${0:#:TARGET}
4344
}
4445

4546
# For reverse counter
4647
snippet forri
47-
for (int ${1:i} = ${2}; $1 > 0; $1--) {
48+
options head
49+
abbr for (int x; ...; x--) {}
50+
for (int ${1:i} = ${2}; $1 >= 0; $1--) {
4851
${0:#:TARGET}
4952
}
5053

@@ -65,11 +68,11 @@ alias do
6568
snippet switch
6669
options head
6770
abbr switch () {}
68-
switch (${1:#:var}) {
69-
case ${2:#:val}:
70-
${0:TARGET}
71-
break;
72-
}
71+
switch (${1:#:var}) {
72+
case ${2:#:val}:
73+
${0:TARGET}
74+
break;
75+
}
7376

7477
snippet case
7578
options head
@@ -89,7 +92,7 @@ snippet function
8992
options head
9093
alias func
9194
abbr func() {}
92-
${1:void} ${2:#:func_name}(${3:#:void}) {
95+
${1:void} ${2:#:func_name}(${3:void}) {
9396
${0:TARGET}
9497
}
9598

@@ -151,7 +154,7 @@ snippet ifdef
151154
options head
152155
alias #ifdef
153156
abbr #ifdef ... #endif
154-
#ifdef $1
157+
#ifdef ${1:#:SYMBOL}
155158
${0}
156159
#endif
157160

@@ -172,7 +175,7 @@ alias #def, #define
172175
snippet once
173176
options head
174177
abbr include-guard
175-
#ifndef ${1:SYMBOL}
178+
#ifndef ${1:#:SYMBOL}
176179
#define $1
177180

178181
${0:TARGET}
@@ -183,7 +186,11 @@ abbr include-guard
183186
# Built-in function calls {{{
184187
snippet printf
185188
abbr printf("...\n", ...);
186-
printf("${1}\n", ${2});
189+
printf("${1}\n"${2});
190+
191+
snippet scanf
192+
abbr scanf("...", ...);
193+
scanf("${1}", ${2});
187194

188195
snippet fprintf
189196
abbr fprintf(..., "...\n", ...);
@@ -197,11 +204,11 @@ abbr fopen("...", "...");
197204

198205
snippet fgets
199206
abbr fgets(row, length, file);
200-
fgets(${0:ROW}, ${1:LENGTH}, ${2:FILE});
207+
fgets(${0:ROW}, ${1:LENGTH}, ${2:stdin});
201208

202209
snippet fscanf
203210
abbr fscanf(file, "...", ...);
204-
fscanf(${1:FILE}, "${2}", ${3});
211+
fscanf(${1:stdin}, "${2}", ${3});
205212

206213
snippet fwrite
207214
abbr fwrite(......, file)
@@ -213,8 +220,27 @@ abbr fread(......, file)
213220

214221
snippet memcpy
215222
abbr memcpy(dest, src, nbytes)
216-
mempcy(${1:DEST}, ${2:SRC}, ${3:N_MEMBERS})
217-
223+
memcpy(${1:DEST}, ${2:SRC}, ${3:NBYTES})
224+
225+
snippet malloc
226+
abbr malloc(size)
227+
($2 *)malloc(${1:N_MEMBERS} * sizeof(${2:TYPE}));
228+
${0}
229+
free(${3:MEM});
230+
231+
snippet calloc
232+
abbr calloc(n, size)
233+
($2 *)calloc(${1:N_MEMBERS}, sizeof(${2:TYPE}));
234+
${0}
235+
free(${3:MEM});
236+
237+
snippet realloc
238+
abbr realloc(old, size)
239+
($3 *)realloc(${1:OLD}, ${2:N_MEMBERS} * sizeof(${3:TYPE}));
240+
${0}
241+
242+
snippet seed_rand
243+
srand(time(NULL));
218244
# }}}
219245

220246
# Built-in operators and alias {{{
@@ -229,18 +255,26 @@ snippet sizeof_array
229255
alias array_size
230256
(sizeof(${1:#:array}) / sizeof(*($1)))
231257

232-
snippet _static_assert
258+
snippet _Static_assert
259+
alias _static_assert
233260
options head
234-
_Static_assert(${1:#:condition}, ${2:#:message})
261+
_Static_assert(${1:#:condition}, ${2:#:message});
235262

236263
snippet static_assert
237264
options head
238-
static_assert(${1:#:condition}, ${2:#:message})
265+
static_assert(${1:#:condition}, ${2:#:message});
239266

240267
snippet _Generic
241268
alias generic, select
242269
_Generic(${1:#:expression}, ${2:#:association-list})
243270

271+
snippet va_list
272+
options head
273+
abbr va_start(va_list, last_arg); ... ; va_end()
274+
va_list ${1:ap};
275+
va_start($1, ${2:LAST_ARG});
276+
${0}
277+
va_end($1);
244278
# }}}
245279

246280
# Comments {{{

0 commit comments

Comments
 (0)