Hottest Forum Q&A on CodeGuru - November 3rd

Introduction:

Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:


What is the difference between calling by pointer and calling by reference? (top)

Thread:

avi123 knows that there are three common ways to pass a parameter to a function.

  • Calling by value
  • Calling by pointer
  • Calling by reference

But, he is not is not sure about the difference between the second and third options. Do you know? Actually, it's simple.

  • When calling by pointer, the value can be 0.
  • When calling by reference, the value must be valid.

But, if you are just passing an object, using a reference is better in terms of readability. As from the perspective of efficiency, both are the same. However, if you need to pass a pointer to an array of objects, there is no choice but to use a pointer. This is because the address stored by the reference cannot be modified after initialization.


How can I cast params together and pass them to a thread? (top)

Thread:

Daviesrt needs to pass some parameters to a thread. Actually, it isn't a big problem. Right? But, besides that, he also wants to cast them and then pass them to the thread.

I am writing a thread to control a timer. I need to pass
"theEvent" and "theLock" to this thread, where "theEvent" and
"theLock" are
theEvent = new CEvent(FALSE,FALSE, "Something happened");
theLock  = new CSingleLock(theEvent);
How can I cast these together and pass them to the thread?
UINT StartClock(LPVOID pParam)
Is it possible??

Andreas Masur answered the question with a very nice, simple, and practical example code. Here is his answer:

Usually, you allocate the structure you wish to pass on the heap (thus, dynamically). Then, before the thread terminates, you would release the previously allocated memory. However, this might resolve in memory leaks if the thread function terminates unexpectedly (as when a function throws an exception, and so forth) because your 'delete' statement will not be processed in this case. To avoid this, you can use an auto pointer instead...

#include <memory>

struct MyStruct
{
  int m_Int;
  MyStruct(int i) { m_Int = i; }
};

// Create thread
MyStruct *pData = new MyStruct(10);
AfxBeginThread(MyThreadFunc, pData);
UINT MyThreadFunc(void *pvParam)
{
  std::auto_ptr<MyStruct> Struct(static_cast<MyStruct*>(pvParam));
  {
    .....
  }
  return 0;
}

The above code will not leak memory irregardless of how the thread function terminates...


How do I determine what error code is 0040e864? (top)

Thread:

Ralf Schneider developed an application that works fine on his system. Besides that, he has also tested the application on different OSes, such as windows 95, OSR2, 98, and XP and it runs without any problems, but the application still crashes on the customer's system. Ralf is not able to reproduce the error. The only information he got is that his application causes the following exception.

  • MyProg cause an exception 10H in module MyProg.exe at 0137: 0040e864
Several users complain it crashed immediately on their machines
including windows 95, 95 OSR2, and 98. It says somthing like:
MyProg cause an exception 10H in module MyProg.exe at
0137: 0040e864

Registers:
....
Stack dump:
...

I tested the software on my company's Windows 95, OSR2, 98, and XP
machines and it works fine. I just cannot reproduce those crashes,
and they are too far away for me to be on site. Does anyone have
a clue on how to debug this problem?

Basically, you'll need a MAP file to track the error code. Without having a mapfile, it's nearly impossible to determine the error. According to MSDN, the error occurs when an unmasked floating-point exception has signaled a previous instruction. Here is the snippet from MSDN KB 150314 What Are Fatal Exception errors:

16 (10h): Coprocessor Error Fault
This interrupt occurs when an unmasked floating-point exception has
signaled a previous instruction. ( Because the 80386 does not have
access to the Floating Point unit, it checks the ERROR\ pin to test
for this condition.) This is also triggered by a WAIT instruction
if the Emulate Math Coprocessor bit at CR0 is set.

So, the problem is a uninitialized float variable. Ralf confirmed that; he has sent a new version to his customer and now it runs!


How can I call a .dll with arguments? (top)

Thread:

myth7676 needs to start a DLL with arguments. Huh, why would somebody need to do that? You have an EXE for that purpose.

I have a .dll, say test.dll and i want to call this .dll like:
test.dll /filename /start address.
The DLL should take the filename and start address as parameters
and do something with them inside. Can this be done?

The only solution I know would be to export functions from an EXE and call them as if the EXE were a DLL.


What is the maximum size of a structure that can be defined in VC++? (top)

Thread:

Varadha has defined a structure in his application. But, when he starts the application, it crashes with an unhandled Exception. His first thought is that there may be a maximum size for a structure.

When i define the following structure.
typedef struct summa
{
  int a;
  int b;
  char c[1062144];
}summa;
and run the application, I get the following error.
Unhandled Exception: ..... Stack Overflow.
Is there a constraint on the maximum size of the Structure that
can be defined in VC++?

The exception appears because it's too big for the stack. You could allocate it with new and put it on the heap instead. Or, you could increase the size of your stack in the linker options. Or, you could use a std::string or std::vector<char> as a structure member that essentially puts that memory on the heap.




About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website http://dotnetslackers.com. DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at: http://dotnetslackers.com/community/blogs/sonukapoor/

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds