Advertisement
micha_b

my_malloc.c

Nov 15th, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | Fixit | 0 0
  1. /*
  2.  *  Source:     malloc.c  
  3.  *  Program:    malloc      
  4.  *  Version:    1.0.0
  5.  *  Target-OS:  AmigaOS
  6.  *
  7.  *  Purpose:    demonstrate dynamic memory allocation    
  8.  *
  9.  *  Author: Micha B. / 2.11.2022
  10.  *
  11.  *  Compiler: SAS/C v6.59
  12.  *  Release: sc NOSTACKCHECK OPTIMIZE OPTTIME OPTLOOP CPU=ANY ADDSYM SMALLCODE NOICONS BATCH IDLEN=64 COMNEST STREQ STRMERGE LIB:scnb.lib LIB:amiga.lib
  13.  *  Debug: sc NOSTACKCHECK DEBUG=SYMBOLFLUSH NOOPT CPU=ANY ADDSYM SMALLCODE NOICONS BATCH IDLEN=64 COMNEST STREQ STRMERGE LIB:sc:scnb.lib LIB:amiga.lib LIB:debug.lib
  14.  */
  15. #include <exec/types.h>
  16. #include <proto/exec.h>
  17.  
  18. #include <dos/dos.h>
  19. #include <proto/dos.h>
  20.  
  21. #include <proto/intuition.h>
  22.  
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stdlib.h> /* malloc(), calloc(), free() */
  26.  
  27. #define SIZE 1024
  28.  
  29. int main( int argc, char *argv[] )
  30. {
  31.     int i=0, length=0;
  32.     //char *teststring = (char *)calloc(SIZE, sizeof(char));        
  33.     //char *targetstring = (char *)calloc(SIZE, sizeof(char));
  34.    
  35.     char *teststring = (char *)malloc(SIZE * sizeof(char));        
  36.     char *targetstring = (char *)malloc(SIZE * sizeof(char));
  37.    
  38.     if(teststring != NULL)
  39.     {
  40.         puts("Memory OK for teststring!");
  41.     }
  42.     else
  43.     {
  44.         puts("No memory left for teststring!");
  45.         return 0;
  46.     }
  47.        
  48.     if(targetstring != NULL)
  49.     {
  50.         puts("Memory OK for targetstring!");
  51.     }
  52.     else
  53.     {
  54.         puts("No memory left for targetstring!");
  55.         return 0;
  56.     }
  57.      
  58.     printf("\nI am a test program, dynamically allocating memory...\n");
  59.     #ifdef __STORMGCC__
  60.         printf("Hello, StormGCC!\n");
  61.     #endif
  62.     #ifdef __STORMC__
  63.         printf("Hello, StormC v3!\n");
  64.     #endif
  65.     #ifdef __SASC
  66.         printf("Hello, SAS C!\n");
  67.     #endif
  68.     #ifdef __GNUC__
  69.         printf("Hello, GCC!\n");
  70.     #endif
  71.     #ifdef __VBCC__
  72.         printf("Hello, VBCC!\n");
  73.     #endif
  74.    
  75.     printf("\nAll Setup Done.\nSkelleton is working. Start coding now!\n\n");
  76.    
  77.     /* Let's play with the string */
  78.    
  79.     teststring = "Aa Bb Cc Dd STOP";
  80.     length = strlen(teststring);
  81.    
  82.     printf("Länge: %ld, sizeof(testring): %ld, teststring: \t\t%s\n", length, sizeof(teststring), teststring);
  83.    
  84.     /* zeichenweise umkopieren */
  85.     i = 0;
  86.     while(teststring[i] != '\0')
  87.     {
  88.         //printf("i = %ld, teststring[%ld] = %s\n", i, i, &teststring);
  89.         targetstring[i] = teststring[i];
  90.         i++;
  91.     }
  92.     targetstring[i] = 0;
  93.    
  94.     /* Vergleichsausgabe */
  95.     length = strlen(targetstring);
  96.     printf("Länge: %ld, sizeof(targetstring): %ld, targetstring: \t%s\n", length, sizeof(targetstring), targetstring);
  97.    
  98.     /* allozierten Speicher freigeben */
  99.     free(targetstring);
  100.     free(teststring);
  101.            
  102.     return(0);    
  103. }
Tags: malloc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement