code_junkie

How can I create a dynamically sized array of structs?

Nov 12th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. typedef struct
  2. {
  3. char *str;
  4. } words;
  5.  
  6. main()
  7. {
  8. words x[100]; // I do not want to use this, I want to dynamic increase the size of the array as data comes in.
  9. }
  10.  
  11. words* array = (words*)malloc(sizeof(words));
  12.  
  13. typedef struct {
  14. char *str;
  15. } words;
  16.  
  17. // Allocate first string.
  18. words x = (words) malloc(sizeof(words));
  19. x[0].str = "john";
  20.  
  21. // Allocate second string.
  22. x=(words*) realloc(x, sizeof(words));
  23. x[1].FirstName = "bob";
  24.  
  25. // printf second string.
  26. printf("%s", x[1].str); --> This is working, it's printing out bob.
  27.  
  28. free(x); // Free up memory.
  29.  
  30. printf("%s", x[1].str); --> Not working since its still printing out BOB even though I freed up memory. What is wrong?
  31.  
  32. x=NULL;
  33.  
  34. # include <stdio.h>
  35. # include <stdlib.h>
  36. # include <string.h>
  37. # include <ctype.h>
  38.  
  39. typedef struct
  40. {
  41. char *str1;
  42. char *str2;
  43. } words;
  44.  
  45. void LoadData(words *, int *);
  46.  
  47. main()
  48. {
  49. words *x;
  50. int num;
  51.  
  52. LoadData(&x, &num);
  53.  
  54. printf("%s %s", x[0].str1, x[0].str2);
  55. printf("%s %s", x[1].str1, x[1].str2);
  56.  
  57. getch();
  58. }//
  59.  
  60. void LoadData(words *x, int * num)
  61. {
  62. x = (words*) malloc(sizeof(words));
  63.  
  64. x[0].str1 = "johnnie";
  65. x[0].str2 = "krapson";
  66.  
  67. x = (words*) realloc(x, sizeof(words)*2);
  68. x[1].str1 = "bob";
  69. x[1].str2 = "marley";
  70.  
  71. *num=*num+1;
  72. }//
  73.  
  74. #include <stdio.h>
  75. #include <vector>
  76.  
  77. typedef std::vector<char*> words;
  78.  
  79. int main(int argc, char** argv) {
  80.  
  81. words myWords;
  82.  
  83. myWords.push_back("Hello");
  84. myWords.push_back("World");
  85.  
  86. words::iterator iter;
  87. for (iter = myWords.begin(); iter != myWords.end(); ++iter) {
  88. printf("%s ", *iter);
  89. }
  90.  
  91. return 0;
  92. }
  93.  
  94. #include <stdio.h>
  95. #include <stdlib.h>
  96.  
  97. typedef struct s_words {
  98. char* str;
  99. struct s_words* next;
  100. } words;
  101.  
  102. words* create_words(char* word) {
  103. words* newWords = malloc(sizeof(words));
  104. if (NULL != newWords){
  105. newWords->str = word;
  106. newWords->next = NULL;
  107. }
  108. return newWords;
  109. }
  110.  
  111. void delete_words(words* oldWords) {
  112. if (NULL != oldWords->next) {
  113. delete_words(oldWords->next);
  114. }
  115. free(oldWords);
  116. }
  117.  
  118. words* add_word(words* wordList, char* word) {
  119. words* newWords = create_words(word);
  120. if (NULL != newWords) {
  121. newWords->next = wordList;
  122. }
  123. return newWords;
  124. }
  125.  
  126. int main(int argc, char** argv) {
  127.  
  128. words* myWords = create_words("Hello");
  129. myWords = add_word(myWords, "World");
  130.  
  131. words* iter;
  132. for (iter = myWords; NULL != iter; iter = iter->next) {
  133. printf("%s ", iter->str);
  134. }
  135. delete_words(myWords);
  136. return 0;
  137. }
  138.  
  139. #include <stdio.h>
  140. #include <stdlib.h>
  141.  
  142. typedef struct {
  143. char** words;
  144. size_t nWords;
  145. size_t size;
  146. size_t block_size;
  147. } word_list;
  148.  
  149. word_list* create_word_list(size_t block_size) {
  150. word_list* pWordList = malloc(sizeof(word_list));
  151. if (NULL != pWordList) {
  152. pWordList->nWords = 0;
  153. pWordList->size = block_size;
  154. pWordList->block_size = block_size;
  155. pWordList->words = malloc(sizeof(char*)*block_size);
  156. if (NULL == pWordList->words) {
  157. free(pWordList);
  158. return NULL;
  159. }
  160. }
  161. return pWordList;
  162. }
  163.  
  164. void delete_word_list(word_list* pWordList) {
  165. free(pWordList->words);
  166. free(pWordList);
  167. }
  168.  
  169. int add_word_to_word_list(word_list* pWordList, char* word) {
  170. size_t nWords = pWordList->nWords;
  171. if (nWords >= pWordList->size) {
  172. size_t newSize = pWordList->size + pWordList->block_size;
  173. void* newWords = realloc(pWordList->words, sizeof(char*)*newSize);
  174. if (NULL == newWords) {
  175. return 0;
  176. } else {
  177. pWordList->size = newSize;
  178. pWordList->words = (char**)newWords;
  179. }
  180.  
  181. }
  182.  
  183. pWordList->words[nWords] = word;
  184. ++pWordList->nWords;
  185.  
  186.  
  187. return 1;
  188. }
  189.  
  190. char** word_list_start(word_list* pWordList) {
  191. return pWordList->words;
  192. }
  193.  
  194. char** word_list_end(word_list* pWordList) {
  195. return &pWordList->words[pWordList->nWords];
  196. }
  197.  
  198. int main(int argc, char** argv) {
  199.  
  200. word_list* myWords = create_word_list(2);
  201. add_word_to_word_list(myWords, "Hello");
  202. add_word_to_word_list(myWords, "World");
  203. add_word_to_word_list(myWords, "Goodbye");
  204.  
  205. char** iter;
  206. for (iter = word_list_start(myWords); iter != word_list_end(myWords); ++iter) {
  207. printf("%s ", *iter);
  208. }
  209.  
  210. delete_word_list(myWords);
  211.  
  212. return 0;
  213. }
  214.  
  215. words* array = (words*)malloc(sizeof(words) * 100);
  216.  
  217. free(array);
  218.  
  219. // initial size
  220. int count = 100;
  221. words *testWords = (words*) malloc(count * sizeof(words));
  222. // resize the array
  223. count = 76;
  224. testWords = (words*) realloc(testWords, count* sizeof(words));
  225.  
  226. // Allocate a words struct
  227. words* CreateWords(int size);
  228. // Assign a value
  229. void AssignWord(word* dest, char* str);
  230. // Clear a words structs (and possibly internal storage)
  231. void FreeWords(words* w);
  232.  
  233. // Resize words (must know original and new size if shrinking
  234. // if you need to free internal storage first)
  235. void ResizeWords(words* w, size_t oldsize, size_t newsize);
  236.  
  237. size = 64; i = 0;
  238. x = malloc(sizeof(words)*size); /* enough space for 64 words */
  239. while (read_words()) {
  240. if (++i > size) {
  241. size *= 2;
  242. x = realloc(sizeof(words) * size);
  243. }
  244. }
  245. /* done with x */
  246. free(x);
  247.  
  248. *x = (words*) realloc(*x, sizeof(words)*2);
  249.  
  250. size_t size = 500;
  251. char* dynamicAllocatedString = new char[ size ];
  252.  
  253. #include <stdio.h>
  254. #include <stdlib.h>
  255. #include <ctype.h>
  256.  
  257. typedef struct
  258. {
  259. char *str1;
  260. char *str2;
  261. } words;
  262.  
  263. void LoadData(words**, int*);
  264.  
  265. main()
  266. {
  267. words **x;
  268. int num;
  269.  
  270. LoadData(x, &num);
  271.  
  272. printf("%s %sn", (*x[0]).str1, (*x[0]).str2);
  273. printf("%s %sn", (*x[1]).str1, (*x[1]).str2);
  274. }
  275.  
  276. void LoadData(words **x, int *num)
  277. {
  278. *x = (words*) malloc(sizeof(words));
  279.  
  280. (*x[0]).str1 = "johnnie";
  281. (*x[0]).str2 = "krapson";
  282.  
  283. *x = (words*) realloc(*x, sizeof(words) * 2);
  284. (*x[1]).str1 = "bob";
  285. (*x[1]).str2 = "marley";
  286.  
  287. *num = *num + 1;
  288. }
Add Comment
Please, Sign In to add comment