Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* vla_test.c */
- /* compile
- clang -std=gnu99 -ggdb3 -o vla_test vla_test.c
- */
- /* run
- ./vla_test n
- where n = {1, 2, 3, 4, 5}
- */
- /** A test. 5 similar functions,
- some of them correct, some not.
- Which ones are good?
- which are bad?
- which are dangerous?
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- typedef void (*string_function)(char* s);
- void string_func1(char* s);
- void string_func2(char* s);
- void string_func3(char* s);
- void string_func4(char* s);
- void string_func5(char* s);
- #define SIZE 1000
- #define CHARSIZE sizeof(char)
- #define PSIZE sizeof(char*)
- #define DEFAULTSTRING "DEFAULTSTRING"
- int main( int argc, char** argv )
- {
- string_function f;
- char teststring[80] ={0};
- int n;
- if (argc < 2)
- {
- fprintf(stderr, "usage:\tvla_test <int> {1-5}\n\n");
- exit(1);
- }
- if ( sscanf( argv[1], "%d", &n) != 1)
- {
- fprintf(stderr, "need an integer (1-5)\n");
- exit(1);
- }
- if (argc >=3 )
- {
- strncpy(teststring, argv[2], 80);
- }
- else
- strncpy(teststring, DEFAULTSTRING, 80);
- /* Dispatch Tables -- "So Hot Right Now!!" */
- switch(n)
- {
- case 1:
- f = string_func1;
- break;
- case 2:
- f = string_func2;
- break;
- case 3:
- f = string_func3;
- break;
- case 4:
- f = string_func4;
- break;
- case 5:
- f = string_func5;
- break;
- default:
- fprintf(stderr, "need an integer (1-5)\n");
- exit(1);
- }
- f(teststring); /* call it */
- } /* main*/
- /******QUESTION*********/
- /* This function compiles without error.
- But is it a correct use of memory management?
- If so, why? If not, why not?
- */
- /* make and print 1000 strings */
- void string_func1(char* s)
- {
- int i = 0;
- int len = 0;
- len = strlen(s) + 1;
- /* allocate string pointers */
- char** stringarray = (char**) malloc( SIZE * PSIZE );
- for(i = 0; i < SIZE; i++)
- {
- /* allocate string */
- stringarray[i] = (char*) malloc( len * CHARSIZE);
- /* set string */
- stringarray[i] = s;
- }
- /* print */
- for(i = 0; i <SIZE;i++)
- fprintf(stdout, "%d: %s " , i, stringarray[i]);
- /* cleanup */
- i = SIZE;
- while(i--)
- free( stringarray[i] );
- free(stringarray);
- } /* string_func1 */
- /******QUESTION*********/
- /* This function compiles without error.
- But is it a correct use of memory management?
- If so, why? If not, why not?
- */
- /* make and print 1000 strings */
- void string_func2(char* s)
- {
- int i = 0;
- int len = 0;
- len = strlen(s) + 1;
- /* allocate string pointers */
- char** stringarray = (char**) malloc( SIZE * PSIZE );
- /* duplicate strings */
- for(i = 0; i < SIZE; i++)
- stringarray[i] = strndup(s, len);
- /* print strings */
- for(i = 0; i <SIZE;i++)
- fprintf(stdout, "%d: %s " , i, stringarray[i]);
- /* cleanup strings */
- i = SIZE;
- while (i--)
- free( stringarray[i] );
- free(stringarray);
- } /* string_func2 */
- /******QUESTION*********/
- /* This function compiles without error.
- But is it a correct use of memory management?
- If so, why? If not, why not?
- */
- /* make and print 1000 strings */
- void string_func3(char* s)
- {
- int i = 0;
- int len = 0;
- len = strlen(s) + 1;
- /* allocate string pointers */
- char** stringarray = (char**) malloc( SIZE * PSIZE );
- /* allocate & duplicate strings */
- for(i = 0; i < SIZE; i++)
- {
- stringarray[i] = (char*) malloc(len * CHARSIZE );
- stringarray[i] = strndup(s, len);
- }
- /* print strings */
- for(i = 0; i <SIZE; i++)
- fprintf(stdout, "%d: %s " , i, stringarray[i]);
- /* cleanup strings */
- i = SIZE;
- while (i--)
- free( stringarray[i] );
- free(stringarray);
- } /* string_func3 */
- /******QUESTION*********/
- /* This function compiles without error.
- But is it a correct use of memory management?
- If so, why? If not, why not?
- */
- void string_func4(char* s)
- {
- int i = 0;
- char* strings[SIZE] = {0};
- int len = strlen(s) +1;
- char stringarray[len * SIZE];
- /* initialize to 0 */
- memset(stringarray, 0, (len * SIZE) );
- /* assign string pointers */
- for (i=0; i < SIZE; i++)
- strings[i] = (stringarray+(i*len));
- /* copy strings */
- for (i=0; i < SIZE; i++)
- strncpy (strings[i], s, len);
- /* print strings */
- for (i=0; i < SIZE ; i++)
- fprintf(stdout, "%d: %s " , i, strings[i]);
- /* cleanup */
- memset(stringarray, 0, (len * SIZE) );
- memset(strings, 0, SIZE);
- } /* string_func4 */
- /******QUESTION*********/
- /* This function compiles without error.
- But is it a correct use of memory management?
- If so, why? If not, why not?
- */
- void string_func5(char* s)
- {
- int i = 0;
- int len = 0;
- char* strings[SIZE] = {0};
- len = strlen(s) +1;
- /* allocate & copy string */
- for (i =0; i < SIZE; i++)
- {
- strings[i] = (char*) malloc( len * CHARSIZE);
- memcpy(strings[i], s, len);
- }
- /* print stings */
- for (i =0; i < SIZE; i++)
- fprintf(stdout, "%d: %s " , i, strings[i]);
- /*cleanup*/
- i = SIZE;
- while(i--)
- free(strings[i]);
- } /* string_func5 */
- /*end*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement