Mixing debug and release libraries in Visual Studio

buffer overrun

Mixing debug and release code is bad practice.

Different compiled versions can depend on different fundamental parts of the C++ runtime library, such as how memory is allocated, structures for things like iterators might be different, extra code could be generated to perform operations. In particular, the std containers and iterators are different and incompatible and do not let this work.

Sometimes one get the following error

A buffer overrun has occurred in Program_64.exe which has corrupted the program’s internal state. Press Break to debug the program or Continue to terminate the program.

For more details please see Help topic ‘How to debug Buffer Overrun Issues’.

So are we stuck?

But what if you have inherited the release library and do not have the source code. You are stuck for development and if the compiler is not letting you to use these libraries in your debug development code.

But we are not entirely stuck, here’s a workaround that worked for me.


// the offending libraries
#include "quad.h"
#include "uqtktools.h"

#include "mytypedef.h"

using namespace std;

The work around

#ifdef _DEBUG
#define DEBUG_WAS_DEFINED
#undef _DEBUG
#endif
// the offending libraries
#include "quad.h"
#include "uqtktools.h"

#ifdef DEBUG_WAS_DEFINED
#define _DEBUG
#endif

#include "mytypedef.h"
Advertisement

Multimap in multimap with C++ — an example

multimap example in c++

A colleague of mine is working in NXOpen and extracting the boundary conditions of the finite element model using Multimap.

Looking at the code, I suggested that he would be better up if he choose to use Multimap in Multimap for the functionality he is after.

To demonstrate the multimap in multimap concept, wrote a simple multimap in multimap example in c++

It helped him and should simplify his work. Putting it here in the hope that this will help someone.



#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
 
 
using namespace std;
 
void printmapvalues(multimap<string,int> p1,string ss1);
void printallmap(multimap<string,int> p1);
void printallmap2(multimap<string,multimap<string,int>> me);
 
int main()
{
      multimap<string,int> mmap,mmap2;  // simple string integer multimap
    multimap<string,multimap<string,int>> mera; // multimap of multimap
   
      // add some values
      mmap.insert(pair<string,int>("SSS",11));
      mmap.insert(pair<string,int>("SSS",11));
      mmap.insert(pair<string,int>("SSS",21));
      mmap.insert(pair<string,int>("SSS",13));
      mmap.insert(pair<string,int>("SSS",31));
      mmap.insert(pair<string,int>("MMM",31));
      mmap.insert(pair<string,int>("MMM",41));
      mmap.insert(pair<string,int>("MMM",51));
      mmap.insert(pair<string,int>("MMM",61));
      mmap.insert(pair<string,int>("MMM",11));
 
// add another multimpap valuess
      mmap2.insert(pair<string,int>("Sim",301));
      mmap2.insert(pair<string,int>("Mim",310));
 
// insert the two above multimaps to this multimap in multimap
      mera.insert(pair<string,multimap<string,int>>("Office",mmap));
    mera.insert(pair<string,multimap<string,int>>("Ghar",mmap2));
 
//    cout<<"Size of  ss "<<mmap.count("SSS")<<endl; // cont the number of value for SSS
    printallmap(mmap);
      printallmap(mmap2);
      printmapvalues(mmap,"MMM");
      printallmap2(mera);
     
      return 0;
}
 
void printmapvalues(multimap<string,int> p1,string ss1){
// print values of a given filed of a malutimap
      pair <multimap<string,int>::iterator, multimap<string,int>::iterator> ret;
      ret = p1.equal_range(ss1);
 
      for (multimap<string,int>::iterator it=ret.first; it!=ret.second; ++it){
        cout << ' ' << it->second;
      cout << '\n';
      }
 
}
 
void printallmap(multimap<string,int> p1){
// print all values of a multimap
      for (multimap<string,int>::iterator it=p1.begin(); it!=p1.end(); ++it){
      cout << it->first << ' ' << it->second << '\n';
      }
}
 
void printallmap2(multimap<string,multimap<string,int>> me){
// prints values of a multimap containing a multimap
      for (multimap<string,multimap<string,int>>::iterator it=me.begin(); it!=me.end(); ++it){
     cout<<it->first<<" contains...."<<endl    ;
            printallmap(it->second);
      }
 
}

Slowing Down is the Key…..

slow down

Slowing down is the key to increased speed.

Past couple of months I was dabbing with fortran GUI and trying pgplot graphics library. I have produced gui’s in c, vb and then integrated them with fortran, but creating GUIs from fortran was new to me.

As the exploration began I took the fire aim adjust approach!! Dived deep into the tutorials and anything that I could lay my hands on.

Quickly from tutorials I graduated to actually creating my own little programs. This went on for a couple of months.

In the beginning I was sprinting as hard as possible. Learning, doing, getting stuck, reading and then doing again. The pace was fast.

But as I become comfortable, my approach shifted. I slowed.

I wrote a program and pondered how and what am I actually doing. This slowing down and pondering doubled my learning. It felt like I was learning at greater pace with this slowdown.

So the technique I want to advocate to anyone learning a new programming language, a new analysis tool or cad software, is to sprint in the first few weeks. Race and learn as much as you are able to handle. Dive deep and continue the pace as long as you are able to.

When exhaustion, sense of acheivement begins to creep in, slow down. Become deliberate in what you do? Question why and what you are doing?

I hope applying this method will help you as much as it has helped me.

What are your views, do let it out in the comments.

Deal or No Deal with Deal.II

deal-ii

Writing finite element programs with mesh adaptivity and complex shape functions is a nontrivial task. Handling grids, inputting meshes, and formatting graphical output are among the numerous cumbersome steps required.  And any helps that comes in this area is always welcome.

Three young researchers—Wolfgang Bangerth, Ralf Hartmann, and Guido Kanschat—have developed a finite element software library that overcomes these problems. Their new software, called deal.II, is a large, object-oriented software library that facilitates the implementation of finite element algorithms with mesh adaptivity and complex shape functions. A unified interface provides support for one, two, and three space dimensions. The library is not tied to a particular set of equations and can integrate the work of others.

So what is deal.II

deal.II is a C++ program library targeted at the computational solution of partial differential equations using adaptive finite elements. It uses state-of-the-art programming techniques to offer you a modern interface to the complex data structures and algorithms required.

The main aim of deal.II is to enable rapid development of modern finite element codes, using among other aspects adaptive meshes and a wide array of tools classes often used in finite element program. A complete stand-alone linear algebra library including sparse matrices, vectors, Krylov subspace solvers, support for blocked systems, and interface to other packages.

To know about how it all began click History of Deal.II [pdf]

Is it NaN ?

C was the computer language that I seriously began programming with. Before c, I had used basic to code few games for myself. But after my first job, I began with c. Then Fortran entered my life and ever since it has dominated my work.

Recently a friend asked me to code a simple routine which checks if the number is nan or not a nan. So I dipped my feet in c. 

 

Here’s the little program i came up with lots of help and blessings of wikipedia.. 🙂

So here’s the program listing of simple isNAN function in c!!

#include <stdio.h>
void myisnan(float x);
int main()
{
    int i=0, j=0, nan_count=0;
     float x ,y;
     y=0.0;
     x = y/0.0;
     myisnan(x);
     x=y/1.0;
     myisnan(x);
}
 

void myisnan(float x)
{
int *temp;
int exp, man;
temp = (int*)&x;
exp = *temp & 0x7f800000; // Extract the exponent;
man = *temp & 0x7fffff;   // Extract the mantissa;
if ((exp == 0x7f800000) && (man != 0))
{
   printf("\n%f – We found NAN\n ",x);
}
else
{
   printf("\nHey %f – is not NAN\n",x);
}
}