-3

this function is supposed to allocate a matrix ('resMat') of 1 rows with 2 cols.

for some reason all I get in 'resMat' is 1 row with 1 col.

any idea why? thanks.

void Ex2()
{
    int** resMat = NULL;
    int rows = 1;
    int* cols = (int*)calloc(rows, sizeof(int*));
    cols = {2};
    int i;
    resMat = (int**)calloc(rows, sizeof(int*));
    assert(resMat);
    for (i = 0; i < rows; i++)
    {
        resMat[i] = (int*)calloc(cols[i], sizeof(int)); // cols[i]=cols[0]=2
        assert(resMat[i]);
    }   

I changed the code a bit to be more readable. 'rows' and 'cols' are actually defined by other functions and that's why 'cols' is an array(in case rows>1)

Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Do not cast the result of `calloc` if you are targeting C (your question is tagged for C), as this can hide problems: https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Alex Reynolds Aug 09 '21 at 17:07
  • 1
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under a [CC BY-SA license](//creativecommons.org/licenses/by-sa/4.0), for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: [How does deleting work? …](//meta.stackexchange.com/q/5221). If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app. – Makyen Aug 12 '21 at 19:39

1 Answers1

2

These statements

int* cols = (int*)calloc(rows, sizeof(int*));
cols = {2};

that produce a memory leak (at first the pointer cols was initialized with the address of the allocated memory and then was reassigned by the integer constant 2; so the address of the allocated memory is lost) with this for loop

for (i = 0; i < rows; i++)
{
    resMat[i] = (int*)calloc(cols[i], sizeof(int)); // cols[i]=cols[0]=2
    assert(resMat[i]);
}   

do not make a sense.

In this statement within the for loop

resMat[i] = (int*)calloc(cols[i], sizeof(int));

there is used the pointer cols with the value 2 that is dereferenced in the expression cols[i] that results in undefined behavior

All what you need is the following

int cols = 2;

resMat = calloc(rows, sizeof(int*));
assert(resMat);
for ( i = 0; i < rows; i++)
{
    resMat[i] = calloc( cols, sizeof( int ));
    assert(resMat[i]);
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Using `assert` as a way to detect memory allocation failures is bad practice. It is a macro that may be implemented as a NO-OP. If the assertion fails it may call `abort`, which again may cause core dump. In general `assert` is a useful macro to detect programming errors where the the developer violates some API-contracts. But it is not useful for detecting and signaling errors external to the program. Use `if(...) { perror("..."); ... }` or similar for that. – HAL9000 Aug 09 '21 at 17:14