0

I need to store a large number of strings to be use by my program. I cannot statically allocate the memory - but I'm not sure how to design the code for the dynamic allocation such that I can access it as an array as each block won't be of the same size.

How should I proceed?

1
  • So you want an array of pointers to char Commented Sep 24, 2013 at 20:16

5 Answers 5

2

What is unknown, the number of strings, size of strings, or both?

You can dynamically allocate an array of char *'s to hold each string char **strings = (char**) malloc(NUM_OF_STRINGS * (sizeof(char*))); calling realloc() as necessary to allocate more strings. For each string you add to the array of strings, also dynamically allocate with malloc strings[index] = (char*) malloc(SIZE_OF_STRING); You'll need to keep track of the number of these strings you allocate and resize the strings array accordingly. Each string will need to be free'd by iterating over the array of strings:

for(unsigned int i = 0; i < stringCount; ++i)
{
    free(strings[i]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

yeah neither condition is known. What does the realloc() segment look like. Say I have a while loop while(int args = scanf("%s", string)) where string is a char *.
You'll use some variable to track the number of strings currently in the strings array and the total size of the array. This is how you will index into the array to add another, but before that you'll need to check to make sure you are not going out of bounds of the array. it will look something like if (index == maxSize) { strings = (char**) realloc( maxSize * 2 * sizeof(char*)); maxSize *= 2;} This should be done prior to assigning a new string to an element of the array.
1

Create an array of char *. These will be uninitialized to begin with. When you need to use one, use malloc() to allocate a buffer of the appropriate size (remember to leave space for the null character), or if you already have a string that you're copying from, use strdup() to allocate the memory and copy from it as necessary.

Comments

0

Are you looking for something like this:-

const char *a[20];
a[0] = "abc";
a[1] = "xyz";
.
.
.
.
a[19] = "try";

1 Comment

well, neither the size of the strings nor the size of the string list are known.
0

You can allocate each string in the dynamic memory by strdup(), and thereafer - just use pointer ti this string. don't forget free(ptr), after string is used.

Example:

char *strings[10000]; // array for 10, 000 string pointers
int ndx = 0;
char strbuf[1000];

while(fgets(strbuf, sizeof(strbuf), f)
  strins[ndx++] = strdup(strbuf);

Comments

0

You can allocate a small array in the beginning of your program and then expand it by using realloc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.