3

Suppose you have a VB6 app which uses a C++ DLL. They share the same memory (you can use pointers from one in the other). The DLL is declared in the VB6 app with Public Declare Function ... Lib ...

So how does this fit with the "Stack grows from one side of memory, heap from the other" philosophy? Where is the stack of the DLL? Are global DLL variables allocated when the application is started? If so, why does it only give me an error when I try to run a function from the DLL?

2
  • 4
    Why don't you give us a hint and tell us the error message? Commented Aug 17, 2011 at 0:10
  • 1
    There is no error. It works perfectly. I am just wondering where the global variables of the DLL are in memory. I thought a process has heap growing from one side and stack from the other -- which makes it difficult to account for global variables of a DLL which is loaded dynamically. Commented Aug 17, 2011 at 0:12

4 Answers 4

4

VB6 uses thread local storage for module-level variables, not data segements. What this means is that public (global) variables in a module can have different values per different threads. Which is not what a C/C++ developer is used to.

Sign up to request clarification or add additional context in comments.

Comments

4

Global variables are stored in the Data Segment.

http://en.wikipedia.org/wiki/Data_segment

The stack is only used for local variables.

1 Comment

Yup, Data Segment is the part of memory that the OP is missing. Locals live on stack, dynamically allocated objects live in the heap, but statics/globals live in the data segment of the exe or dll that they belong to.
2

Global DDL symbols will be in the DLL image itself. If the DLL uses the symbol as a pointer to which it attaches some dynamic memory, then the memory will be from whatever the dynamic allocation is from (typically the heap used by the CRT). We would need to see exactly how the VB declaration of the C++ import looks like and what the C++ DLL does (could be initializing on DllMain, could be a static region in the DLL image, could require call to some Init functione etc etc etc).

"Stack grows from one side of memory, heap from the other" was true maybe on 8088 processors, no such thing happens on modern platforms. Stack gets allocated per thread and goes upwards, true, but there could be hundreds of stacks in a process. Heap gets allocated all over the place and grows, basically, at random. And a typical process also has several heaps in it.

Comments

1

There is typically one stack per thread. The function in the DLL will use the stack of the current thread (the thread on which is was invoked).

See Remus's answer to your other questions about memory management.

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.