-4

I am implementing SVD and I found a code online which I am trying to use. I am getting the following error (Exception). Exception thrown at 0x003A3D43 in Project2.exe: 0xC0000005: Access violation writing location 0x00000000.

int main()
{
    float **convA;
    convA = new float*();
    float *singular;
    singular = new float();
    float **Vt;
    Vt = new float*();
    for (int p=0; p < row; p++)
    {
        for (int m=0; m < col; m++)
        {
            convA[p][m] = (m + p)*2.5; //program breaks here
        }
    }

    dsvd(convA, row, col, singular, Vt); 
}

Function definition of dsvd is:

int dsvd(float **a, int m, int n, float *w, float **v);
BSMP
  • 4,596
  • 8
  • 33
  • 44
Shil
  • 3
  • 1
  • `convA` only points to 1 pointer, which is null. going `convA[p]` goes out of bounds already – M.M Dec 14 '17 at 04:17
  • 1
    While it won't really help in this case, you should really be using [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) (or [`std::array`](http://en.cppreference.com/w/cpp/container/array)) instead. – Some programmer dude Dec 14 '17 at 04:19
  • 3
    OK, I'll have to ask -- where did you get a code sample like this showing how to allocate a 2-dimensional array? It looks like you're just guessing. – PaulMcKenzie Dec 14 '17 at 04:21
  • As for your crash, think of `new float*()` as being an array of ***one*** pointer to `float`. And that single pointer is initialized to be a ***null*** pointer. – Some programmer dude Dec 14 '17 at 04:21
  • Lastly, perhaps you should take a few steps back, [get a good beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and start over from the beginning? – Some programmer dude Dec 14 '17 at 04:22

2 Answers2

0

Your convA is a single float *, you have to specify how many items you are going to store in the array. And unless you are doing this for a class or similar learning I would seriously suggest you use std::vector instead of direct allocation.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
0
float **convA;
convA = new float*();
float *singular;
singular = new float();

What do you think you're doing here? You only allocated one pointer for convA. The parentheses after that only denotes initialization (to zero, by default). To correctly use it, you must allocate another area for that pointer to point at. Also, in the upcoming for loop, you're assuming you have enough space for rows and columns for convA, which is obviously wrong.

A correct way to allocate space is:

convA = new float*[row];
for (int i = 0; i < row; i++)
    convA[i] = new float[col];

And to free the space, remember to do in the reverse order:

for (int i = 0; i < row; i++)
    delete[] convA[i];
delete[] convA;

Since you're writing in C++, working with raw pointers is generally a bad practice (unless you're implementing an allocator). You should try Standard Template Libraries. std::vector is probably a good candidate for your code:

std::vector<std::vector<float>> convA;
convA.resize(row);
for (int i = 0; i < row; i++)
    convA[i].resize(col);

The rest of your code would remain unchanged because std::vector has a handy operator[]() overload function, except that you should remove the deallcoation as std::vector handles it automatically.

iBug
  • 35,554
  • 7
  • 89
  • 134