How to have allocated pointer and size information along with memory allocation?

when I use heap space, along with the allocation I need the information of 1) allocated pointer, 2) size, 3) line number, 4) file name.

extendMalloc +—+—+—+—+———–+ | 1 | 2 | 3 | 4 | malloc | +—+—+—+—+———–+

When memory is freed, it has to be freed with extendMalloc information.

How to apply Memory Allocation? (eg. malloc, calloc, free) This C Program counts the amount of words in a list?

Considering the code provided by @David C. Rankin in this previous answer:

https://stackoverflow.com/questions/56141468/how-to-count-only-words-that-start-with-a-capital-in-a-list

How do you optimise this code to include Memory Allocation for much larger text files? With this code below it will complete for small .txt files.

However, what is the best way to set memory allocation to this code so that C (Programming Language) does not run out of memory. Is it best to use linked lists?

/**  * C program to count occurrences of all words in a file.  */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <limits.h>  #define MAX_WORD     50     /* max word size */ #define MAX_WORDS   512     /* max number of words */  #ifndef PATH_MAX #define PATH_MAX   2048     /* max path (defined for Linux in limits.h) */ #endif  typedef struct {            /* use a struct to hold */     char word[MAX_WORD];    /* lowercase word, and */     int cap, count;         /* if it appeast capitalized, and its count */ } words_t;  char *strlwr (char *str)    /* no need for unsigned char */ {     char *p = str;      while (*p) {         *p = tolower(*p);         p++;     }      return str; }  int main (void) {      FILE *fptr;     char path[PATH_MAX], word[MAX_WORD];     size_t i, len, index = 0;      /* Array of struct of distinct words, initialized all zero */     words_t words[MAX_WORDS] = {{ .word = "" }};      /* Input file path */     printf ("Enter file path: ");     if (scanf ("%s", path) != 1) {  /* validate every input */         fputs ("error: invalid file path or cancellation.\n", stderr);         return 1;     }      fptr = fopen (path, "r");   /* open file */     if (fptr == NULL) {         /* validate file open */         fputs ( "Unable to open file.\n"                 "Please check you have read privileges.\n", stderr);         exit (EXIT_FAILURE);     }      while (index < MAX_WORDS &&                 /* protect array bounds  */             fscanf (fptr, "%s", word) == 1) {   /* while valid word read */         int iscap = 0, isunique = 1;    /* is captial, is unique flags */          if (isupper (*word))            /* is the word uppercase */             iscap = 1;          /* remove all trailing punctuation characters */         len = strlen (word);                    /* get length */         while (len && ispunct(word[len - 1]))   /* only if len > 0 */             word[--len] = 0;          strlwr (word);                  /* convert word to lowercase */          /* check if word exits in list of all distinct words */         for (i = 0; i < index; i++) {             if (strcmp(words[i].word, word) == 0) {                 isunique = 0;               /* set unique flag zero */                 if (iscap)                  /* if capital flag set */                     words[i].cap = iscap;   /* set capital flag in struct */                 words[i].count++;           /* increment word count */                 break;                      /* bail - done */             }         }         if (isunique) { /* if unique, add to array, increment index */             memcpy (words[index].word, word, len + 1);  /* have len */             if (iscap)                      /* if cap flag set */                 words[index].cap = iscap;   /* set capital flag in struct */             words[index++].count++;         /* increment count & index */         }     }     fclose (fptr);  /* close file */      /*      * Print occurrences of all words in file.      */     puts ("\nOccurrences of all distinct words with Cap in file:");     for (i = 0; i < index; i++) {         if (words[i].cap) {             strcpy (word, words[i].word);             *word = toupper (*word);             /*              * %-15s prints string in 15 character width.              * - is used to print string left align inside              * 15 character width space.              */             printf("%-15s %d\n", word, words[i].count);         }     }      return 0; } 

Example Use/Output

Using your posted input

$   ./bin/unique_words_with_cap Enter file path: dat/girljumped.txt  Occurrences of all distinct words with Cap in file: Any             7 One             4 Some            10 The             6 A               13 

Memory allocation and leaking issue?

I am working on the “print_selected(int x)” function which, takes x then extracts bits 5, 6, 7, and 8 (starting from bit 0 at the rightmost) then print them in both binaries then in hexadecimal.

There is a helper function, “int2bin(int n)” within the “print_selected(int x)” and “int2bin(int)” function returns a char type array to “s”.

The problem is that I can print out the binary number of bits of 5, 6, 7, and 8bits correctly, however, the hexadecimal number returns somehow weird number(due to the memory leaking issue?). I strongly doubt that ” char* s = int2bin(x)” and ” free(s)” within “print_selected(int)” might be the problem but I do not know which part I should make a change to correctly print out the right hexadecimal number.

char* int2bin(int n)  // takes integer and returns char array with 32  binary number. {     int nbits = sizeof(n) * 8;     char *s = malloc(nbits+1);        s[nbits] = '';   unsigned int u = *(unsigned int*)&n; int i; unsigned int mask = 1 << (nbits-1);  for (i = 0; i < nbits; i++, mask >>= 1)     s[i] = ((u & mask) != 0) + '0';     return s; }  void print_selected(int x) // takes an integer and print in "binary" and  "hexadecimal".  { int hex[4];  // to store the 4 bits of (5,6,7,8)  int i;  char *s = int2bin(x); //  I think this part makes problem?  printf("bits 5 to 8 in bin: "); for(i = 23; i <= 26 ; i++) {     printf("%d", s[i] - '0');     hex[i] = s[i] - '0'; }  free(s); // I think this part makes problem?  printf("\n");  int j = 3; // since we already know the number of bits, int sum = 0;  for(i = 0 ; i <= 3 ; i++) {     sum = sum + (hex[i] * pow(2, j));     j--;         }  printf("in hex: 0x%x\n", sum); }   int main(void) { int a = 278;  print_selected(a);  return 0; } 

Design pattern and/or data structure for cache friendly memory allocation?

I am a bit new to this site. Feel free to edit or move me to where I or question would fit better.

I am a applied mathematician / computational engineer searching for design patterns and data structures exist for cache-friendly memory allocation (for fast numerical floating point algorithms)?

For example I have N activities for which I want to group data to be local in memory when allocated as I know that when activity # k is executed it will greatly benefit if all of the data it uses is within one cache window.

Bipartite allocation with minimum cost

Given two vertex sets $ V_1$ and $ V_2$ . The vertices in $ V_2$ have a limitation on the maximum degree of each vertex being $ K$ . I need to find an allocation algorithm such that every pair of vertices in $ V_1$ is interconnected via a vertex in $ V_2$ i.e $ X \leftrightarrow Y \leftrightarrow Z$ , where $ X,Z \in V_1$ and $ Y \in V_2$ .

The goal is to find an allocation that minimizes the distances between all such pairs in $ V_1$ . The distance is measured as : $ \; dist(X, Y) + dist(Y, Z)\;$ . The distances are provided beforehand. I kind of came up with an algorithm, but it doesn’t guarantee optimality. Any help will be appreciated.

Banker’s algorithm – additional allocation

I have a problem with the banker’s algorithm. There are 6 processes and one type of resources. The allocation is (0, 29, 35, 10, 25, 35) and the MAX need is (50, 80, 50, 25, 40, 90). The available resources are equal to D. The first question is what is the minimum value for D as the state to be safe. I have managed to solve this and the value is 15. But the next question is: To how many processes can be allocated additional 5 resources to keep the state safe (multiple situations)? I am not sure I understand this. It means that I need to add 5 to the allocation array or both allocation array and MAX need?

Let $V_1$ be the variance of the estimated mean from a stratified random sample of size $n$ with proportional allocation.

Let $ V_1$ be the variance of the estimated mean from a stratified random sample of size $ n$ with proportional allocation. Assume that the strata sizes are such that the allocations are all integers.Let $ V_2$ be the variance of the estimated mean from a simple random sample of size $ n$ . Show that the ratio $ \frac{V1}{V2}$ is independent of $ n$ .

We define the estimator $ \bar y_{st}=\sum_{h}w_h \bar{y}_h$ , where $ w_h=\frac{N_h}{N}$ . We know this is an unbiased estimator of the population mean. For proportional allocation, $ n_h=nw_h $

$ V(\bar y_{st})=V_1=\frac{1}{n}\sum w_h \sigma^2_h$ (If we assume SRSWR is applied to sample from each strata)

Again, $ V_2=V(\bar y)=\frac{\sigma^2}{n}$

Now, $ \frac{V_1}{V_2}=\sum w_h \frac{\sigma^2_h}{\sigma^2}=\frac{1}{N}-\sum \frac{w_h(\bar Y_h- \bar Y)^2}{\sigma^2}$ Now, how can I proceed from here?