1

I alloced block of memory for matrix. Than in cycle I remember poiters to the lines.
I got Unhandled exception: Access violation writing location 0x00557148 in _tmain cycle for. What did I wrong?

double **d; 

#define COUNT 10 

int create()
{
    d = (double**) malloc(COUNT * sizeof(double*));
    if (!d)
        return 0; 
    int size = COUNT * sizeof(double); 
    double *_new =  (double*) malloc(COUNT * size);

    if (!_new)
        return 0; 
    for (int i = 0; i < COUNT; i++) {
        d[i] = _new;
        _new += size; 
    }

    return 1; 
}

int _tmain(int argc, _TCHAR* argv[])
{
    double *_d; 
    if (create()) {
        for(int i = 0; i < COUNT; i++) {
            _d = d[i]; 
            for (int j = 0; j < COUNT; j++)
                _d[j] = 5; 
        }
    } else
        return -1;

    return 0;
}
2
  • Did you #include <stdlib.h>? (Don't cast the result of malloc!) Commented Jul 20, 2013 at 15:07
  • yes I did. Unhandled exception in main cycle for. when i = 6 Commented Jul 20, 2013 at 15:10

1 Answer 1

1

I'm pretty sure you need to change _new += size to _new += COUNT

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

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.