0

I am doing one project in which I define a data types like below

typedef QVector<double> QFilterDataMap1D;

typedef QMap<double, QFilterDataMap1D> QFilterDataMap2D;

Then there is one class with the name of mono_data in which i have define this variable

QFilterMap2D valid_filters;



mono_data Scan_data // Class

Now i am reading one variable from a .mat file and trying to save it in to above "valid_filters" QMap.

Qt Code: Switch view

for(int i=0;i<1;i++)
    {
        for(int j=0;j<1;j++)
        {
            Scan_Data.valid_filters[i][j]=valid_filters[i][j];
            printf("\nValid_filters=%f",Scan_Data.valid_filters[i][j]);
        }
    }

The transferring is done successfully but then it gives run-time error

Windows has triggered a breakpoint in SpectralDataCollector.exe.

This may be due to a corruption of the heap, and indicates a bug in SpectralDataCollector.exe or any of the DLLs it has loaded.

The output window may have more diagnostic information

Can anyone help in solving this problem. It will be of great help to me.

Thanks

Saad Saadi
  • 1,031
  • 10
  • 26

1 Answers1

0

Different issues here:

1. Using double as key type for a QMap

Using a QMap<double, Foo> is a very bad idea. the reason is that this is a container that let you access a Foo given a double. For instance:

map[0.45] = foo1;
map[15.74] = foo2;

This is problematic, because then, to retrieve the data contained in map[key], you have to test if key is either equal, smaller or greater than other keys in the maps. In your case, the key is a double, and testing if two doubles are equals is not a "safe" operation.

2. Using an int as key while you defined it was double

Here:

Scan_Data.valid_filters[i][j]=valid_filters[i][j];

i is an integer, and you said it should be a double.

3. Your loop only test for (i,j) = (0,0)

Are you aware that

for(int i=0;i<1;i++)
{
    for(int j=0;j<1;j++)
    {
        Scan_Data.valid_filters[i][j]=valid_filters[i][j];
        printf("\nValid_filters=%f",Scan_Data.valid_filters[i][j]);
    }
}

is equivalent to:

Scan_Data.valid_filters[0][0]=valid_filters[0][0];
printf("\nValid_filters=%f",Scan_Data.valid_filters[0][0]);

?

4. Accessing a vector with operator[] is not safe

When you do:

Scan_Data.valid_filters[i][j]

You in fact do:

QFilterDataMap1D & v = Scan_Data.valid_filters[i]; // call QMap::operator[](double)
double             d = v[j];                       // call QVector::operator[](int)

The first one is safe, and create the entry if it doesn't exist. The second one is not safe, the jth element in you vector must already exist otherwise it would crash.

Solution

It seems you in fact want a 2D array of double (i.e., a matrix). To do this, use:

typedef QVector<double> QFilterDataMap1D;
typedef QVector<QFilterDataMap1D> QFilterDataMap2D;

Then, when you want to transfer one in another, simply use:

Scan_Data.valid_filters = valid_filters;

Or if you want to do it yourself:

Scan_Data.valid_filters.clear();
for(int i=0;i<n;i++)
{
    Scan_Data.valid_filters << QFilterDataMap1D();
    for(int j=0;j<m;j++)
    {
        Scan_Data.valid_filters[i] << valid_filters[i][j];
        printf("\nValid_filters=%f",Scan_Data.valid_filters[i][j]);
    }
}

If you want a 3D matrix, you would use:

typedef QVector<QFilterDataMap2D> QFilterDataMap3D;
Community
  • 1
  • 1
Boris Dalstein
  • 7,015
  • 4
  • 30
  • 59
  • First of all thanks a lot for the reply. For the 3rd part, yeah i know that they are equivalent..actually my matrix size is 1*900..i was just checking it with 1*1. for the solution part, i modified it little and it is working great. can you please give me some advice how i can write code for 3d? typedef QVector QFilterDataMap1D; typedef QMap QFilterDataMap2D; typedef QMap QFilterDataMap3D; – Saad Saadi Aug 29 '13 at 06:56
  • @Saad Ok, glad it helped :-) Yes, what you propose is fine. If you need more efficiency though, you may want in fact to use a 1D vector of size sizeX * sizeY * sizeZ, and access the elements with myEmulated3DArray[i*sizeY*sizeZ + j*sizeZ + k]. This later approach is better for a variety of reason, but I would say don't worry about it now, go with the simplest approach :) – Boris Dalstein Aug 29 '13 at 07:32
  • @Saad. Oh no, I just realized you used QMap again. No! Forget about QMap, you are misleaded by the name, this really isn't what you need. :) For a 3D matrix, use: `QVector< QVector< QVector > >` – Boris Dalstein Aug 29 '13 at 07:36
  • If i am not wrong then this is right or wrong syntax?typedef QVector QFilterDataMap2D; – Saad Saadi Aug 29 '13 at 07:45
  • @Saad, My bad, I did a mistake in my answer due to copy-pasting. This syntax is wrong, remove the `double`, see in 1min my updated answer. – Boris Dalstein Aug 29 '13 at 07:53
  • Of course the notion that comparison of doubles is somehow "unsafe" is as bogus as it gets. A double is simply a binary representation of an exponent, a sign and a mantissa. If you have two of them and they have the same value, they compare equal. If they don't have the same value, the compare unequal. There's no magic to it, so please, let's not propagate bogosity. Now if you're asking whether it's always easy to produce the very value you intend to - now that may be harder, but it's of no consequence here. It really depends on what you're trying to do. – Kuba hasn't forgotten Monica Aug 29 '13 at 18:50
  • Dear Boris..Can you please on this new thread started by me...this is also regarding qt and i am hopeful that you will help me in this also.http://stackoverflow.com/questions/18626426/qstring-to-string-and-vica-versa.. – Saad Saadi Sep 05 '13 at 01:40