Week 0xA I
Crypto
struct
typedef
malloc
$ gcc thing.c -w # we uh, will fix warnings latter
$ ./a.out one two three
argv[0] = ./a.out
argv[1] = one
argv[2] = two
argv[3] = three
4096_t
integer arrays were data structures the whole time.struct
typedef
both
to print_argv
by:argc
a 4 byte int
and argv
an 8 byte address living on the stack.both
to print_argv
by:argc
and argv
on the stack.void *
both
to print_argv
by:int main(int argc, char **argv) {
void **both = (void **)malloc(sizeof(void *) * 2);
both[0] = (void *)&argc;
argc
and argv
on the stack.argc
, a number, in void *
slot
both
to print_argv
by:int main(int argc, char **argv) {
void **both = (void **)malloc(sizeof(void *) * 2);
both[0] = (void *)argc;
both[1] = (void *)argv;
argc
and argv
on the stack.argc
, a number, in void *
slotargv
, a char *
, in a void *
slot
int main(int argc, char **argv) {
void **both = (void **)malloc(sizeof(void *) * 2);
both[0] = (void *)argc;
both[1] = (void *)argv;
print_argv(both);
argc
and argv
on the stack.argc
, a number, in void *
slotargv
, a char *
, in a void *
slotboth
, the array location.both
, but…
both
both
, find a void *
of size 8, take the lower order 4 bits and call it an int
named argc
both
both[0] -> print_argv.argc:int
both[1] -> print_argv.argv:char **
gcc
what that void **
really is?void *
and back?struct
keyword, whichstruct
typedef
4096_t.h
we have declared functions.struct
typedef
struct
malloc
motivating example, structs, like other vars, will be stack allocated.structs_variable_name.data_field_name
)struct argv_struct {
int argc;
char **argv;
};
void print_argv(struct argv_struct args) {
printf("location of arg(s,c,v): %p,%p,%p\n", &args, &(args.argc), &(args.argv));
for (int i = 0; i < args.argc; i++) {
printf("argv[%d] = %s\n", i, args.argv[i]);
}
}
int main(int argc, char **argv) {
struct argv_struct args;
printf("location of arg(s,c,v): %p,%p,%p\n", &args, &(args.argc), &(args.argv));
args.argc = argc;
args.argv = argv;
print_argv(args);
return 0;
}
$ ./a.out one two three
location of arg(s,c,v): 0x7ffe800297b0,0x7ffe800297b0,0x7ffe800297b8
location of arg(s,c,v): 0x7ffe80029770,0x7ffe80029770,0x7ffe80029778
argv[0] = ./a.out
argv[1] = one
argv[2] = two
argv[3] = three
$ ./a.out one two three
location of arg(s,c,v): 0x7ffe800297b0,0x7ffe800297b0,0x7ffe800297b8
location of arg(s,c,v): 0x7ffe80029770,0x7ffe80029770,0x7ffe80029778
argv[0] = ./a.out
argv[1] = one
argv[2] = two
argv[3] = three
main
, one in print_args
.json
instead of types, which is a whole thingstruct
typedef
typedef
bool
before defining it because
True
and Fals
bool
struct
typedef
4096_t
internal integer fields.
gcc
needs size information.$ make
gcc client.c pair.c -std=c89 -Wall -Wextra -Werror -Wpedantic -O2
client.c: In function ‘main’:
client.c:4:17: error: storage size of ‘p’ isn’t known
4 | struct pair p;
| ^
client.c:5:9: error: invalid use of undefined type ‘struct pair’
5 | p = newp();
| ^~~~
client.c:4:17: error: unused variable ‘p’ [-Werror=unused-variable]
4 | struct pair p;
| ^
cc1: all warnings being treated as errors
make: *** [Makefile:5: all] Error 1
malloc
and free
but any size goes.$ make
gcc client.c pair.c -std=c89 -Wall -Wextra -Werror -Wpedantic -O2
client.c: In function ‘main’:
client.c:6:6: error: invalid use of undefined type ‘struct pair’
6 | p->x = 1;
| ^~
client.c:7:6: error: invalid use of undefined type ‘struct pair’
7 | p->y = p->x * 2;
| ^~
client.c:7:13: error: invalid use of undefined type ‘struct pair’
7 | p->y = p->x * 2;
| ^~
make: *** [Makefile:5: all] Error 1
4096_t
’s as pointers.4096_t.h
uint64_t bigadd(uint64_t *in0, uint64_t *in1, uint64_t *sum);
uint64_t bigsub(uint64_t *min, uint64_t *sub, uint64_t *dif);
uint64_t bigmul(uint64_t *in0, uint64_t *in1, uint64_t *out);
uint64_t bigdiv(uint64_t *num, uint64_t *den, uint64_t *quo, uint64_t *rem);
uint64_t bigquo(uint64_t *num, uint64_t *den, uint64_t *quo);
uint64_t bigrem(uint64_t *num, uint64_t *den, uint64_t *rem);
free
in your .c files and valgrind
on your executables.struct
typedef