I cannot spot the issue and neither can VS2010/ VS11 Developer preview edition. The code runs perfectly until allocating memory for the double pointer new_matrix. The only reason I cannot find the problem is because I've allocated space for 3 other pointers successfully. I believe I have the syntax correct since I followed exactly what I had before with minor adjustments. Any help would be greatly appreciated!

main.c

//Variable Declaration and initialization
int **matrices = (int **) malloc(4 * sizeof(int)); //Pointer to the set of matrix registers
int *rows = (int *) malloc(4 * sizeof(int)); //Pointer to hold row sizes
int *columns = (int *) malloc(4 * sizeof(int));	//Pointer to hold column sizes.



Memory_Allocation(matrices[Matrix_ctr], Matrix_ctr);	//Allocate space for user data.

matrix_functions.h

void Memory_Allocation(int matrices[], int Matrix_ctr);

matrix_functions.c

void Memory_Allocation(int **new_matrix, int matrix_Index)
...
new_matrix = (int **) malloc(size * sizeof(int)); //The pointer was not initialized to NULL.

I am calling the function Memory_Allocation from my main.c file which takes in a double pointer as an argument. This pointer will point to **matrices to hold all of the user's matrices in the end.

Thanks for your help!:)

malloc returns a pointer to void. You're trying to typecast a pointer to an integer pointer to what malloc expects to be a pointer to a variable of whatever type. That might be the problem. Other than that, I wouldn't know what to tell you without further details.

The code runs perfectly until allocating memory for the double pointer new_matrix.

That's not too informative. Please, give us the error message, if you would.

Actually, if you're running a Windows App, you might be better off using one of the other Memory Allocation Routines: preferably HeapCreate, HeapAlloc, HeapFree.
Even CoTaskMemAlloc would work.

The purpose of memory allocation routines is to reserve memory on the heap, stack (calloc), or virtual memory for variable space. Make sure that is what you are doing. If you use the Heap functions. Then it allows your program the option of growing the heap automatically up to a limit that you can specify. So, if you don't happen to understand the dynamics of what's going on and the program needs to allocate more memory than what you expect for the variable space of the arrays, variable, etc., then HeapAlloc will be more forgiving than malloc.

Edited 5 Years Ago by hkdani: n/a

Not really an error but more of a memory location that cannot be used I believe is what I read. 0xcdcdcdcd (???)- I don't know what this means when the rest of my pointers get the following

size - 0x00377a10(-842150451)

When I try to write data it gives me the following error:
Unhandled exception at 0x0F878ERE5 (msvcr110d.dll) in Project3.exe: 0xC00000005: Access violation writing location 0xCDCDCDCD.

I have gotten this error for the past couple days and do not know what I have to do to get around this.

Thanks!

if you don't happen to understand the dynamics of what's going on and the program needs to allocate more memory than what you expect for the variable space of the arrays, variable, etc., then HeapAlloc will be more forgiving than malloc.

Gosh I wish I could use HeapAlloc. However, I do not know whether the function would run on the linux machine. This project is for my programming class and my professor likes to work on linux. But, I cannot stand using it because I'm not proficient with it. Also, VS2010 detects errors while I type and that is a feature I've become so accustomed to that I cannot live without it now. :icon_smile:

Edited 5 Years Ago by DmytriE: n/a

Okay. Make life easy on yourself.

Don't typecast with a pointer to a pointer: (int**). Just use it with a pointer to a variable.

If you just have to leave the pointer to a pointer parameter in there for some reason, just declare an integer * after the function in the code, allocate memory for the integer pointer you declared, but make sure before doing that you assign it the same address as the passed in pointer to pointer parameter. Do you get my point?

You should be able to get away with that and it should still work even though you are using a pointer to a pointer. The main goal is to allocate space in memory on the heap. Your program can use that memory space for pointer to pointer to ....... etc. Just so long as you have enough memory allocated. You may not see the variables showing up in debug; but you can punch in queries for int[0][1] while debugging and see that it's working.

// void function ... blah, blah, blah
int *nArray ;

nArray = &newmatrix ;

nArray = (int*)malloc( blah, blah, blah) ;

Your pointer to pointer matrix will have the same address as the pointer you just declared. Just make sure you allocate enough memory. I don't think the compiler cares really about if you're using a pointer to pointer. It just cares about what type of variable and that you allocate enough space to handle what you want to do.

VS2010 detects errors while I type and that is a feature I've become so accustomed to that I cannot live without it now

Get used to using VIM. With exuberant ctags it has auto completion. And besides, Petzold recommends that you get used to writing with a text editor over using IDEs that correct everything you type and suggest everything that you should do. It helps to exercise the brain.. And when working with these abstract ideas, we probably need all the exercise that we can get.

Edited 5 Years Ago by hkdani: n/a

Oh, before I hit the sack. Do all your operations on the int*, not on the pointer to the pointer. Since you've assigned the same address of the pointer to the pointer to the int*, your values will fill in anyway.

Just remember malloc was designed to assign memory based on a variable type. So, whether you use the space for two dimensional arrays, 3 dimensional arrays, etc. All malloc cares about is that your designate the type, and it cares about how many instances of that type need to be allocated.

Looking at the code you've given, I can tell you straight away that you're not using malloc() correctly. However, I'm having difficulty figuring out exactly what you're trying to do. The variable names (eg. matrices, new_matrix) suggest you're storing multiple two-dimensional matrices of varying size. In that case your types are totally mismatched. Here's an example of the allocation, initialization, display, and release process for what I've described:

#include <stdio.h>
#include <stdlib.h>

void Memory_Allocation(int ***new_matrix, int rows, int cols)
{
    int i;
    
    *new_matrix = malloc(rows * sizeof(int *));
    
    for (i = 0; i < rows; i++)
        (*new_matrix)[i] = malloc(cols * sizeof(int));
}

void release_matrix(int ***matrix, int rows)
{
    int i;
    
    for (i = 0; i < rows; i++)
        free((*matrix)[i]);
        
    free(*matrix);
    *matrix = NULL;
}

void initialize_matrix(int **matrix, int rows, int cols, int start)
{
    int i, j;
    
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++)
            matrix[i][j] = start++;
    }
}

void display_matrix(int **matrix, int rows, int cols)
{
    int i, j;
    
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++)
            printf("%3d", matrix[i][j]);
        puts("");
    }
}

#define MATRICES 4
#define ROWS 2
#define COLS 4

int main(void)
{
    int ***matrices = malloc(MATRICES * sizeof(int**));
    int i;
    
    for (i = 0; i < MATRICES; i++)
        Memory_Allocation(&matrices[i], ROWS, COLS);
        
    for (i = 0; i < MATRICES; i++)
        initialize_matrix(matrices[i], ROWS, COLS, i);
    
    for (i = 0; i < MATRICES; i++) {
        display_matrix(matrices[i], ROWS, COLS);
        puts("");
    }
    
    for (i = 0; i < MATRICES; i++)
        release_matrix(&matrices[i], ROWS);
    
    return 0;
}

Is this what you were going for? If not, please describe what you're trying to do rather than how you're trying to do it, and someone can offer suggestions for how to do it properly.

This article has been dead for over six months. Start a new discussion instead.