Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Lazy initialization
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Lazy initialization
(section)
Add topic