> char* pstring1 = "pstring1\0";
> char* pstring2 = "pstring2\0";
[quoted text clipped - 4 lines]
>
> free(strings);
No.
char** strings = static_cast<char**>(malloc(2 * sizeof(char*));
strings[0] = string1;
strings[1] = string2;
free(strings);
Remember, strings stores char* pointers, not chars.
Tom
> Thanks for the reply Will,
You are welcome.
> So I guess if I knew my list was going to be 2 items in length, and each
> item was a maximum of 50 chars, I would want to do something like this?:
[quoted text clipped - 7 lines]
>
> free(strings);
Well, no. :)
The first two lines are OK. You are declaring two pointers to strings and
setting each to point to fixed strings which the compiler stores in the data
segment. So far, so good.
The next few lines exhibit some confusion.
On the left side of this declaration
> char** strings = malloc(2 * 50);
you have a pointer to a pointer to a character. Then you allocate 100 bytes
for it. Ut oh.
Since the variable strings holds a pointer to a pointer, strings[n] holds a
pointer, so these lines
> strings[0] = string1;
> strings[50] = string2;
are copying _pointers_ even though I _think_ that you intended for them to
copy _strings_.
To the end of this post I have appended tired old C source (definitely not
modern C++) that demonstrates handling an array of strings in three ways. In
the first I statically allocate the array. In the second I dynamically
allocate an array of fixed size strings. In the third I allocate an array of
variably sized strings.
In all cases, the size of the array is known at compile time. It gets even
uglier if the upper bound for the array is not known until runtime.
Note that I AM NOT advocating that you proceed down this path. I'd no sooner
suggest that than I'd suggest using an antique operating system whose name
ends in X. <gd&r> But you asked.
Finally note that buffer overruns are all to easy where null-terminated
strings are concerned. And exploiting this vulnerability is a favorite
technique of virus writers. If you must use this old stuff you may want to
read up on the safer variants of the old string functions here:
http://msdn2.microsoft.com/en-us/library/td1esda9
Regards,
Will
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define SIZE 50
#define COUNT 10
int main()
{
unsigned int i, j;
// static allocation
{
char strings1[COUNT][SIZE + 1] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight",
"nine" };
printf("\nStatic Allocation\n\n");
for ( i = 0; i < COUNT; i++ )
{
printf( "String #%d is '%s', characters are ", i, strings1[i] );
for ( j = 0; j < strlen( strings1[i] ); j++ )
printf("'%c' ", strings1[i][j] );
printf("\n\n");
}
}
// dynamic allocation of fixed-size strings
{
char (*strings2)[COUNT][SIZE + 1];
printf("\nDynamic Allocation - Fixed Size\n\n");
strings2 = calloc(COUNT, SIZE + 1);
strcpy( (*strings2)[0], "zero");
strcpy( (*strings2)[1], "one");
strcpy( (*strings2)[2], "two");
strcpy( (*strings2)[3], "three");
strcpy( (*strings2)[4], "four");
strcpy( (*strings2)[5], "five");
strcpy( (*strings2)[6], "six");
strcpy( (*strings2)[7], "seven");
strcpy( (*strings2)[8], "eight");
strcpy( (*strings2)[9], "nine");
for ( i = 0; i < COUNT; i++ )
{
printf( "String #%d is '%s', characters are ", i, (*strings2)[i] );
for ( j = 0; j < strlen( (*strings2)[i] ); j++ )
printf("'%c' ", (*strings2)[i][j] );
printf("\n\n");
}
}
// dynamic allocation of fixed sized strings
{
char *strings3[COUNT];
printf("\nDynamic Allocation - Variable Size\n\n");
strings3[0] = malloc(strlen("zero") + 1);
strcpy(strings3[0], "zero");
strings3[1] = malloc(strlen("one") + 1);
strcpy(strings3[1], "one");
strings3[2] = malloc(strlen("two") + 1);
strcpy(strings3[2], "two");
strings3[3] = malloc(strlen("three") + 1);
strcpy(strings3[3], "three");
strings3[4] = malloc(strlen("four") + 1);
strcpy(strings3[4], "four");
strings3[5] = malloc(strlen("five") + 1);
strcpy(strings3[5], "five");
strings3[6] = malloc(strlen("six") + 1);
strcpy(strings3[6], "six");
strings3[7] = malloc(strlen("seven") + 1);
strcpy(strings3[7], "seven");
strings3[8] = malloc(strlen("eight") + 1);
strcpy(strings3[8], "eight");
strings3[9] = malloc(strlen("nine") + 1);
strcpy(strings3[9], "nine");
for ( i = 0; i < COUNT; i++ )
{
printf( "String #%d is '%s', characters are ", i, strings3[i] );
for ( j = 0; j < strlen( strings3[i] ); j++ )
printf("'%c' ", strings3[i][j] );
printf("\n\n");
}
}
return 0;
}