How to populate a dynamic array of strings (char*) 
Author Message
 How to populate a dynamic array of strings (char*)

I want to cycle through a list of data and return an undertermined number of
elements that match a specified criteria back to VB clients. The data being
returned is of type char* in C++. I obviously need a dynamic array but I've
never used one. Can someone point me in the right direction?

--
Attitudes are contagious. Is yours worth catching?
http://www.*-*-*.com/



Mon, 10 Mar 2003 03:00:00 GMT  
 How to populate a dynamic array of strings (char*)

Quote:

> I want to cycle through a list of data and return an undertermined number
of
> elements that match a specified criteria back to VB clients. The data
being
> returned is of type char* in C++. I obviously need a dynamic array but
I've
> never used one. Can someone point me in the right direction?

There are two basic ways to dynamically allocate memory in C++.

A)  Use the C-style function malloc as follows:

// Where n equals the number of characters in the array
char *lpString = (char*)malloc(sizeof(char) * nChars);
// Free memory when your finished with it
free (lpString);

B)  Use the C++ operator new as follows:

char *lpString = new char[nChars];
// Free memory when your finished with it
delete[] lpString;

I'm not sure which, if either way, will be compatible with VB, but that's
how you can handle dynamic arrays in VC.

Daemon



Mon, 10 Mar 2003 03:00:00 GMT  
 How to populate a dynamic array of strings (char*)

Quote:
> There are two basic ways to dynamically allocate memory in C++.

> A)  Use the C-style function malloc as follows:

> // Where n equals the number of characters in the array
> char *lpString = (char*)malloc(sizeof(char) * nChars);
> // Free memory when your finished with it
> free (lpString);

> B)  Use the C++ operator new as follows:

> char *lpString = new char[nChars];
> // Free memory when your finished with it
> delete[] lpString;

What about a <vector>?  Seems like it might be a workable choice.

--Kevin

http://www.precisonline.com
http://www.precisonline.com/gold.htm

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 11 Mar 2003 03:00:00 GMT  
 How to populate a dynamic array of strings (char*)

Kevin,
You cannot use <vector> because he needs to return a char**.
Since he has a array of pointers to CString objects, the first element
becomes a pointer to a pointer.
if he defines
std::vector <CString *>
He would be able to store and add and create the array correctly, but he
will not be able to return the pointer to this array. No function in
std::vector returns the pointer to the array inside. He can easily implement
his own growable array like this : #define a GROWSIZE, and then allocate and
assign the objects, if the array is full, allocate a new bigger array,
relocate all the objects pointer s(as these are pointers only) to the new
array, and then free the old one, point the old array pointer to the new
array.
 You can look into this MSDN article. Info: Dynamic Memory Allocation for
two-dimensional arrays. ID: Q104639
Also this may help: Here I am storing LDAPMod pointers in a growable array
LDAPMod **m_ppAttributeList;
Since ldap modification functions take a LDAPMod** specifically, I cannot
use a vector.

    // New attribute number - also represents the total attributes present
in the list.
    m_nAttrModNumber++;

    int nCurrAttrNo = m_nAttrModNumber - 1;

    // GrowSize should always be greater than zero.
    _ASSERT( ARRAY_GROWSIZE > 0 );

    // If new Attribute List has to be created OR
    // If current array is full.
    if ( (NULL == m_ppAttributeList) || (0 == ( nCurrAttrNo %
ARRAY_GROWSIZE )) )
    {
        // Allocate a new array with new increased size limits.
        LDAPMod ** ppNewAttrList = (LDAPMod**) malloc( (sizeof(LDAPMod*) *
m_nAttrModNumber) +
                                                       (sizeof(LDAPMod*) *
ARRAY_GROWSIZE) );
        // If its a new Attribute list
        if ( NULL == m_ppAttributeList )
        {
            m_ppAttributeList = ppNewAttrList;
        }
        else
        {
            // If its not a new Attribute list,
            // copy the LDAPMod pointers from the old array.
            for ( int i = 0; i < m_nAttrModNumber; i++ )
            {
                ppNewAttrList[i] = m_ppAttributeList[i];
            }
            // Free the old Attribute List and assign the newly created list
to it.
            free ( m_ppAttributeList );
            m_ppAttributeList = ppNewAttrList;
        }
    }

Hope this helps

-Sanju


Quote:

> > There are two basic ways to dynamically allocate memory in C++.

> > A)  Use the C-style function malloc as follows:

> > // Where n equals the number of characters in the array
> > char *lpString = (char*)malloc(sizeof(char) * nChars);
> > // Free memory when your finished with it
> > free (lpString);

> > B)  Use the C++ operator new as follows:

> > char *lpString = new char[nChars];
> > // Free memory when your finished with it
> > delete[] lpString;

> What about a <vector>?  Seems like it might be a workable choice.

> --Kevin

> http://www.precisonline.com
> http://www.precisonline.com/gold.htm

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Tue, 11 Mar 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. char arrays versus char strings.

2. help with function to create dynamic two-dimensional char array

3. finding chars in dynamic arrays

4. HELP! dynamic array of char*

5. Differences between char array[SIZE] and char *array

6. char * to wchar_t (char string to wide string)

7. dynamic multidimensional string arrays....

8. Using pointers for equivalent of dynamic array of strings

9. Dynamic array of static strings

10. C/C++: Dynamic allocation of string array

11. Dynamic allocation of array of character strings???

12. Malloc: Problem with Dynamic string array.

 

 
Powered by phpBB® Forum Software