Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct
- {
- char *str;
- } words;
- main()
- {
- words x[100]; // I do not want to use this, I want to dynamic increase the size of the array as data comes in.
- }
- words* array = (words*)malloc(sizeof(words));
- typedef struct {
- char *str;
- } words;
- // Allocate first string.
- words x = (words) malloc(sizeof(words));
- x[0].str = "john";
- // Allocate second string.
- x=(words*) realloc(x, sizeof(words));
- x[1].FirstName = "bob";
- // printf second string.
- printf("%s", x[1].str); --> This is working, it's printing out bob.
- free(x); // Free up memory.
- printf("%s", x[1].str); --> Not working since its still printing out BOB even though I freed up memory. What is wrong?
- x=NULL;
- # include <stdio.h>
- # include <stdlib.h>
- # include <string.h>
- # include <ctype.h>
- typedef struct
- {
- char *str1;
- char *str2;
- } words;
- void LoadData(words *, int *);
- main()
- {
- words *x;
- int num;
- LoadData(&x, &num);
- printf("%s %s", x[0].str1, x[0].str2);
- printf("%s %s", x[1].str1, x[1].str2);
- getch();
- }//
- void LoadData(words *x, int * num)
- {
- x = (words*) malloc(sizeof(words));
- x[0].str1 = "johnnie ";
- x[0].str2 = "krapson ";
- x = (words*) realloc(x, sizeof(words)*2);
- x[1].str1 = "bob ";
- x[1].str2 = "marley ";
- *num=*num+1;
- }//
- #include <stdio.h>
- #include <vector>
- typedef std::vector<char*> words;
- int main(int argc, char** argv) {
- words myWords;
- myWords.push_back("Hello");
- myWords.push_back("World");
- words::iterator iter;
- for (iter = myWords.begin(); iter != myWords.end(); ++iter) {
- printf("%s ", *iter);
- }
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct s_words {
- char* str;
- struct s_words* next;
- } words;
- words* create_words(char* word) {
- words* newWords = malloc(sizeof(words));
- if (NULL != newWords){
- newWords->str = word;
- newWords->next = NULL;
- }
- return newWords;
- }
- void delete_words(words* oldWords) {
- if (NULL != oldWords->next) {
- delete_words(oldWords->next);
- }
- free(oldWords);
- }
- words* add_word(words* wordList, char* word) {
- words* newWords = create_words(word);
- if (NULL != newWords) {
- newWords->next = wordList;
- }
- return newWords;
- }
- int main(int argc, char** argv) {
- words* myWords = create_words("Hello");
- myWords = add_word(myWords, "World");
- words* iter;
- for (iter = myWords; NULL != iter; iter = iter->next) {
- printf("%s ", iter->str);
- }
- delete_words(myWords);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- char** words;
- size_t nWords;
- size_t size;
- size_t block_size;
- } word_list;
- word_list* create_word_list(size_t block_size) {
- word_list* pWordList = malloc(sizeof(word_list));
- if (NULL != pWordList) {
- pWordList->nWords = 0;
- pWordList->size = block_size;
- pWordList->block_size = block_size;
- pWordList->words = malloc(sizeof(char*)*block_size);
- if (NULL == pWordList->words) {
- free(pWordList);
- return NULL;
- }
- }
- return pWordList;
- }
- void delete_word_list(word_list* pWordList) {
- free(pWordList->words);
- free(pWordList);
- }
- int add_word_to_word_list(word_list* pWordList, char* word) {
- size_t nWords = pWordList->nWords;
- if (nWords >= pWordList->size) {
- size_t newSize = pWordList->size + pWordList->block_size;
- void* newWords = realloc(pWordList->words, sizeof(char*)*newSize);
- if (NULL == newWords) {
- return 0;
- } else {
- pWordList->size = newSize;
- pWordList->words = (char**)newWords;
- }
- }
- pWordList->words[nWords] = word;
- ++pWordList->nWords;
- return 1;
- }
- char** word_list_start(word_list* pWordList) {
- return pWordList->words;
- }
- char** word_list_end(word_list* pWordList) {
- return &pWordList->words[pWordList->nWords];
- }
- int main(int argc, char** argv) {
- word_list* myWords = create_word_list(2);
- add_word_to_word_list(myWords, "Hello");
- add_word_to_word_list(myWords, "World");
- add_word_to_word_list(myWords, "Goodbye");
- char** iter;
- for (iter = word_list_start(myWords); iter != word_list_end(myWords); ++iter) {
- printf("%s ", *iter);
- }
- delete_word_list(myWords);
- return 0;
- }
- words* array = (words*)malloc(sizeof(words) * 100);
- free(array);
- // initial size
- int count = 100;
- words *testWords = (words*) malloc(count * sizeof(words));
- // resize the array
- count = 76;
- testWords = (words*) realloc(testWords, count* sizeof(words));
- // Allocate a words struct
- words* CreateWords(int size);
- // Assign a value
- void AssignWord(word* dest, char* str);
- // Clear a words structs (and possibly internal storage)
- void FreeWords(words* w);
- // Resize words (must know original and new size if shrinking
- // if you need to free internal storage first)
- void ResizeWords(words* w, size_t oldsize, size_t newsize);
- size = 64; i = 0;
- x = malloc(sizeof(words)*size); /* enough space for 64 words */
- while (read_words()) {
- if (++i > size) {
- size *= 2;
- x = realloc(sizeof(words) * size);
- }
- }
- /* done with x */
- free(x);
- *x = (words*) realloc(*x, sizeof(words)*2);
- size_t size = 500;
- char* dynamicAllocatedString = new char[ size ];
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- typedef struct
- {
- char *str1;
- char *str2;
- } words;
- void LoadData(words**, int*);
- main()
- {
- words **x;
- int num;
- LoadData(x, &num);
- printf("%s %sn", (*x[0]).str1, (*x[0]).str2);
- printf("%s %sn", (*x[1]).str1, (*x[1]).str2);
- }
- void LoadData(words **x, int *num)
- {
- *x = (words*) malloc(sizeof(words));
- (*x[0]).str1 = "johnnie ";
- (*x[0]).str2 = "krapson ";
- *x = (words*) realloc(*x, sizeof(words) * 2);
- (*x[1]).str1 = "bob ";
- (*x[1]).str2 = "marley ";
- *num = *num + 1;
- }
Add Comment
Please, Sign In to add comment