Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. buffer = (double*) malloc((r + r + r) * sizeof(double *) +
  2. (r*r + r*r + r*r) * sizeof(double));
  3.  
  4. if(buffer == NULL) {
  5. printf("out of memoryn");
  6. return 0;
  7. }
  8.  
  9. for(i = 0, j = 0; i < r; i++) {
  10. a[i] = &buffer[j];
  11. j+=r;
  12. }
  13.  
  14. for(i = 0; i < r; i++) {
  15. b[i] = &buffer[j];
  16. j+=r;
  17. }
  18.  
  19. for(i = 0; i < r; i++) {
  20. c[i] = &buffer[j];
  21. j+=r;
  22. }
  23.  
  24. a = buffer[j];
  25. b = buffer[j + r];
  26. c = buffer[j + r + r];
  27.  
  28. double* all = (double*) malloc( 3*r*r*sizeof(double));
  29.  
  30. double *p1 = all;
  31. double *p2 = &all[r*r];
  32. double *p3 = &all[2*r*r];
  33.  
  34. size_t r = ...;
  35.  
  36. double (*a)[r] = NULL;
  37. double (*b)[r] = NULL;
  38. double (*c)[r] = NULL;
  39.  
  40. double (*backing_store)[r][r] = malloc(3 * sizeof *backing_store);
  41. if (!backing_store)
  42. {
  43. // panic and exit
  44. }
  45.  
  46. a = backing_store[0];
  47. b = backing_store[1];
  48. c = backing_store[2];
  49.  
  50. a[i][j] = ...;
  51. printf("%fn", b[x][y]);
  52.  
  53. free(backing_store);
  54.  
  55. #define R ...
  56.  
  57. double (*a)[R] = NULL;
  58. double (*b)[R] = NULL;
  59. double (*c)[R] = NULL;
  60.  
  61. double (*backing_store)[R][R] = malloc(3 * sizeof *backing_store);
  62. if (!backing_store)
  63. {
  64. // panic and exit
  65. }
  66.  
  67. a = backing_store[0];
  68. b = backing_store[1];
  69. c = backing_store[2];
  70.  
  71. double *a = NULL;
  72. double *b = NULL;
  73. double *c = NULL;
  74.  
  75. double *backing_store = malloc(3 * r * r * sizeof *backing_store);
  76. if (!backing_store)
  77. {
  78. // panic
  79. }
  80.  
  81. a = backing_store;
  82. b = backing_store + r * r;
  83. c = backing_store + 2 * r * r;
  84.  
  85. a[i*r+j] = ...;
  86. printf("%fn", b[x*r+y]);
  87.  
  88. free(backing_store);
  89.  
  90. double (*Memory)[r][r] = malloc(3 * sizeof *Memory);
  91. // Now Memory points to three r-by-r arrays of double.
  92. double (*a)[r] = Memory[0];
  93. double (*b)[r] = Memory[1];
  94. double (*c)[r] = Memory[2];
  95.  
  96. double *(*PointerMemory)[r] = malloc(3 * sizeof *PointerMemory);
  97. // PointerMemory points to three arrays of r elements of pointers to double.
  98. double (*ElementMemory)[r][r] = malloc(3 * sizeof *ElementMemory);
  99.  
  100. // Set a to point to the first array of r elements of pointers to double.
  101. double *a[r] = PointerMemory[0];
  102. // Initialize the elements of a to point to rows of the first r-by-r array of double.
  103. for (i = 0; i < r; ++i)
  104. a[i] = ElementMemory[0][i];
  105.  
  106. // Set b for the second array of pointers and the second array of double.
  107. double *b[r] = PointerMemory[0];
  108. for (i = 0; i < r; ++i)
  109. b[i] = ElementMemory[1][i];
  110.  
  111. // Set c for the third arrays.
  112. double *c[r] = PointerMemory[0];
  113. for (i = 0; i < r; ++i)
  114. c[i] = ElementMemory[2][i];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement