I'm creating a MFC DLL which will be called by a C app. I need to dynamically create/resize structure arrays so Im using calloc/resize/free. While calloc seems to work fine for simple data types I'm having difficulty working with structure arrays. Consider the following ex which compiles fine in VS 2K3 C++.

To clarify further, the error is in the function iteslf. The paramater is needed as an inpu/output parameter between the C application and the C++ DLL. The C application will create the pointers, my DLL will allocate memory for them and populate with data then return to the calling appl, the calling appl will then manipulate the arrays and free memory.

About bug inside of function, your description is missing details. What does this mean "cannot be referenced"? Do you have exception? What exception exactly, what is code line?
Why do you use calloc and not C++ new? C client doesn't care what is the way of allocation. In any case, you need to write function in Dll which releases this pointer.

I am able to reference the allocated memory as a single structure instead of as an array of structures.

strcpy(samp->string, "hello"); //has no effect but does not generate compile/runtime errors
strcpy(samp[0]->string, "hello"); //index '0' out of bounds

In the code below the first pass works fine. The second pass generates a Null Ref Exception when I allocate memory for the char*. If I do not calloc the char * within the structure then I'll get a Null Ref Exception with strcpy.

The third line in my previous post should read ...
strcpy(samp[1]->string1, "hello"); //generates null ref exception not index out of bounds

To answer your prior questions.
The C appl will be responsible for freeing the memory I allocate after it manipulates the data. To my knowledge C does not support delete and mixing new/free is a no-no.

I'm not certain, but I think the problem is more pronounced in Debug builds -- somehow relating to the internal tracking that MFC does in making and verifying allocations (e.g., the replacement of the "new" operator with DEBUG_NEW).

I am using AFX_MANAGE_STATE( AfxGetStaticModuleState() ); in the exported function of my DLL (not reflected in my sample). My code for allocating memory is actually in helper functions where data is retrieved from a database, manipulated, stored in the allocated memory so the C appl can then use the data and free memory.

I'm not even progressing beyond the point of allocating memory so right now the problem is local to the routines responsible for allocating memory. Not in the return to the caller.

=-=-=-=-=-=-=-=-=-=-=-=-=
It compiles without error, allocates the string data storage, allows assignment of string data to the allocations, and frees without error.

One thing it does NOT do is return a valid pointer to the allocated data (related to the need to pas a Samp** rather than a Samp* -- as discussed above). But first things first... your version of this code needs to work before the next step can be taken.

Are you saying you were able to copy strings into the string1 and string2 char*??

I tried this again and after the first pass in the for loop samp[0].string1 is undefined. If I try to see its value in the Command Window I get the following. In case something was changed I copied the code you pasted in. If it is working for you I'm at a loss.

I started from scratch with a new project and it works to some degree. I think the problem I was having has something to do with .NET. Originally I'd created a "Console Application .NET" mindless of any differences. This time I created a "Win32 Console Application".

I'm having trouble figuring out the indirection necessary for the myFunc() call. It is still bombing out allocating memory for the char * on the second pass.

My next suggestion was for you to make myFunc() return a Samp* -- just to simplify it (but I see you hit on that yourself :-).

An important error-reducing trick is this one:
free(samp[index].string1);
samp[index].string1= 0; // null the pointer to help detect invalid re-use
...
free(samp);
samp= 0; // null the pointer to help detect invalid re-use

It only makes a difference if your program erroneously attempts to write new data into the allocation after it has been freed. But if the program does make that mistake, you will get an immediate exception -- so it helps you track down the error in seconds rather than hours or days ;-)

Thanks for the suggestion. Now I'm left struggling with a local heap violation. I'm seeing a lot of posts hit on this topic but Im not finding the answer I think I need. I am able to create the pointer local to the exe, allocate memory within the dll, write data to the memory within the dll, return to the exe caller and read the data from allocated memory ... then the free() call fails. Debug Assertion _CrtIsValidHeapPointer Fails when I attempt to free within the exe.

Since I'm new to experts-exchange I'd also like to ask if it is appropriate that this thread continue or should it be split to another thread?

One possibility relates to using a diffferent 'Runtime library' settings in the build settings (Project>Settings>C++>Code Generation) and that is easy to check. If that's not it, try perusing the other threads that turn up in that search

The following diagram presents a diamond class hierarchy:
As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…

In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…

When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…