Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy initialization
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C=== In [[C (programming language)|C]], lazy evaluation would normally be implemented inside one function, or one source file, using [[static variable]]s. In a function: <syntaxhighlight lang="C"> #include <string.h> #include <stdlib.h> #include <stddef.h> #include <stdio.h> struct fruit { char *name; struct fruit *next; int number; /* Other members */ }; struct fruit *get_fruit(char *name) { static struct fruit *fruit_list; static int seq; struct fruit *f; for (f = fruit_list; f; f = f->next) if (0 == strcmp(name, f->name)) return f; if (!(f = malloc(sizeof(struct fruit)))) return NULL; if (!(f->name = strdup(name))) { free(f); return NULL; } f->number = ++seq; f->next = fruit_list; fruit_list = f; return f; } /* Example code */ int main(int argc, char *argv[]) { int i; struct fruit *f; if (argc < 2) { fprintf(stderr, "Usage: fruits fruit-name [...]\n"); exit(1); } for (i = 1; i < argc; i++) { if ((f = get_fruit(argv[i]))) { printf("Fruit %s: number %d\n", argv[i], f->number); } } return 0; } </syntaxhighlight> Using one source file instead allows the state to be shared between multiple functions, while still hiding it from non-related functions. fruit.h: <syntaxhighlight lang="C"> #ifndef _FRUIT_INCLUDED_ #define _FRUIT_INCLUDED_ struct fruit { char *name; struct fruit *next; int number; /* Other members */ }; struct fruit *get_fruit(char *name); void print_fruit_list(FILE *file); #endif /* _FRUIT_INCLUDED_ */ </syntaxhighlight> fruit.c: <syntaxhighlight lang="C"> #include <string.h> #include <stdlib.h> #include <stddef.h> #include <stdio.h> #include "fruit.h" static struct fruit *fruit_list; static int seq; struct fruit *get_fruit(char *name) { struct fruit *f; for (f = fruit_list; f; f = f->next) if (0 == strcmp(name, f->name)) return f; if (!(f = malloc(sizeof(struct fruit)))) return NULL; if (!(f->name = strdup(name))) { free(f); return NULL; } f->number = ++seq; f->next = fruit_list; fruit_list = f; return f; } void print_fruit_list(FILE *file) { struct fruit *f; for (f = fruit_list; f; f = f->next) fprintf(file, "%4d %s\n", f->number, f->name); } </syntaxhighlight> main.c: <syntaxhighlight lang="C"> #include <stdlib.h> #include <stdio.h> #include "fruit.h" int main(int argc, char *argv[]) { int i; struct fruit *f; if (argc < 2) { fprintf(stderr, "Usage: fruits fruit-name [...]\n"); exit(1); } for (i = 1; i < argc; i++) { if ((f = get_fruit(argv[i]))) { printf("Fruit %s: number %d\n", argv[i], f->number); } } printf("The following fruits have been generated:\n"); print_fruit_list(stdout); return 0; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)