Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3042

qsort() with strings problem

$
0
0
Hi guys, if I do this everything works fine:

Code:

char* strings[]={"Zorro", "Alex", "Celine", "Bill", "Forest", "Dexter"};
qsort(strings, 6, sizeof(char*), cmp_func);

However, I like to allocate my strings like this (filenames example):

Code:

// allocate
char** mem=new char*[n]; // allocate pointers
mem[0]=new char[n*MAX_PATH]; // allocate memory

// set pointers
for(int i=1; i<n; i++){
 mem[i]=mem[i-1]+MAX_PATH;
}

// fill in strings
...

// sort causes debug assertion failure _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
qsort(mem, n, sizeof(char*), cstring_cmp);

Please help. Thank you.

Edit: Guys, I fixed it:

Code:

// instead of allocating the memory like this...
mem[0]=new char[n*MAX_PATH];

// ...it's better to do it this way (with a new variable) so the pointer won't get shuffled
char* buffer=new char[n*MAX_PATH];

Thanks

Viewing all articles
Browse latest Browse all 3042

Trending Articles