0

So i guess the titles says it all i m trying to create dynamic structure of adjacency lists and i m having some errors since i m a little new in C anyone mind taking a look at it and tell me of any mistakes i might be doing

STRUCTURES

struct adjList                       //Data structure for an adjacency List
{
    int pageNo;                     //stores the page number (unique)
    int * link;                     //Stores a list of links to other pages
    int noLinks;                    //No of links stored - If noLinks > 0 then node is a non-dangling node
    // else if noLinks == 0 then its a dangling node
};

struct matrixList                   //Data structure for a Matrix
{
    struct adjList ** matrix;       //Array containing all the rows in our matrix
    int noRows;                     //Total Number of rows in our matrix
    int noColumns;                  //The total number of columns in our matrix
};

struct blockMatrix
{
    struct matrixList  *** block;       //A2 dimensional Array of MatrixList
    int noRows;                      //Total Number of rows in our matrix
    int noColumns;                  //The total number of columns in our matrix
};

// BlockLoading.cpp : main project file.

#include "stdafx.h"
#include "PageRankFuncs.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");
    
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    
    blockMatrix * blocks = (blockMatrix *)malloc( sizeof(blockMatrix) );

    //Create a 2 dimesional array of matrices
    blocks->block = (matrixList ***)malloc( sizeof(matrixList **) );
    
    blocks->noColumns = 5;
    blocks->noRows=2;


    for (int y = 0; y < blocks->noColumns; y++ )
    {
       
        blocks->block[y]  = (matrixList **)malloc(sizeof(matrixList));
        
        for ( int x = 0; x < blocks->noRows; x++)
        {
           
            blocks->block[y][x] = (matrixList *)malloc(sizeof(matrixList));
            
            blocks->block[y][x]->matrix = (adjList **)malloc(BLOCK_SIZE * sizeof(adjList));

            for (int z = 0; z < BLOCK_SIZE ; z++ )
            {
                blocks->block[y][x]->matrix[z] = (adjList *) malloc(sizeof(adjList));
                blocks->block[y][x]->matrix[z]->link = (int *)malloc( sizeof(int) );
                blocks->block[y][x]->matrix[z]->link[0] = 22;
            }
        }
    }

   
   
   
   for (int y = 0; y < blocks->noColumns; y++ )
   {

        for ( int x = 0; x < blocks->noRows; x++)
        {

            for (int z = 0; z < BLOCK_SIZE ; z++ )
            {
                
                if ( blocks->block[y][x]->matrix[z]->link != NULL )
                    free( blocks->block[y][x]->matrix[z]->link );
                    
                if ( blocks->block[y][x]->matrix[z] != NULL )
                    free( blocks->block[y][x]->matrix[z] );
            }
            
            if ( blocks->block[y][x]->matrix != NULL )
                free( blocks->block[y][x]->matrix );

            if ( blocks->block[y][x] != NULL )
                free( blocks->block[y][x] );
        }
        
        if ( blocks->block[y] != NULL )
            free( blocks->block[y] );
    }

    if ( blocks->block != NULL )
       free( blocks->block );
        
    if (blocks != NULL)
        free( blocks);
    
    return 0;

}

Edited by kng: n/a

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Salem
0

Didn't mention that i am getting an error from line

if ( blocks->block != NULL )
free( blocks->block );

HEAP CORRUPTION DETECTED: after Normal block (#184) at 0x0027A110.
CRT detected that the application wrote to memory after end of heap buffer.

Memory allocated at .\BlockLoading.cpp(20).

0

Consider this as a friendly warning. You will be ignore by those that can really help you if you don't use the proper C code tags when you post code.
Example here.

0

Thanks Fixxed it should be nice and readable now :D New to the forum BTW

0

> int main(array<System::String ^> ^args)
What's this crud?
This isn't C.
It isn't even C++.
It's microsoft's munged to hell managed c++, or whatever they call it.

You need to decide which language you're actually using.
Because this "pick any random syntax which seems to compile" approach will lead you into trouble.

blocks->noColumns = 5;
    blocks->noRows=2;
    blocks->block = (matrixList ***)malloc( sizeof(matrixList **) * blocks->noColumns );

    for (int y = 0; y < blocks->noColumns; y++ )
    {
        blocks->block[y]  = (matrixList **)malloc(sizeof(matrixList));

If you're going to treat a block of memory as an array, you need to allocate 'N' of them (and not just one of each).

Ditto for all the other nested 'arrays' you're allocating.

0

Salem First of all i d like to thank you for the timely and accurate reply which worked out :D

Secondly would you mind to take a couple of minutes to explain a little further what is wrong with my code that is the mesh up between c and c++ since i am quite unaware of this

If your talking about

using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

That has been automatically generated by visual c++ and i was to lazy to translate it into C :P since the application is just a test app to check that my structures are valid.

P.S Thanks Again deeply appretiated

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.