Size Of Arrays

Posted on 2004-11-03
Last Modified: 2010-04-15
Put simply, mt prog has to find its way through a maze. it does this by calling a function to try every direction form the current square and then calling this function again from the next, etc... The problem is, I'm not storing the data in an array correctly. The size of the map is variable so the max. length of the path is also going to be variable

Specifically, I'm using:

int writePath (int curX, int curY, int nestDepth) {
//      printf("\n Writing path (%i): x:%i, y:%i\n", nestDepth, curX, curY);
      solvePath[sizeof(int)*nestDepth*2] = curX;
      solvePath[sizeof(int)*nestDepth*2+1] = curY;
      return 0;      

int readPath (int nestDepth) {
      return solvePath[sizeof(int)*nestDepth*2];

(yes I know it's only returning the X variable at present)

The return in solvePath is giving me an Access violation (ie trying to read outside the allocated memory I think). The size of the solvePath array is defined by

            solvePath = (int*)malloc(sizeof(int) * 2 * (arrSize) * (arrSize)); // define the size of the array to carry the solution
//            =int size * no of vars to store * maxX * maxY
as logically, the largest path will include EVERY square. arrSize is the length of one side of the map (it's square)

can anyone tell me where the gaping flaw in my logic is? Thanks
Question by:basiclife
    LVL 11

    Accepted Solution

    You should be returning solvePath[nestDepth*2]

    Do not put sizeof(int) into the index
    LVL 5

    Author Comment

    thenhow ill it know the length of each individual piece of data?, ie....

    if there are 4 8-byte pieces of data as shown below:


    wouldn't querying solvePath[2] return the 2nd byte not the 2nd word?
    LVL 11

    Expert Comment

    sizeof() is a compile-time function that returns the number of bytes for the argument provided - so using it in the malloc() call as you do is correct.
    However, when indexing through an array the compiler correctly decides (from the type of the array) how many bytes each index increment should take, so sizeof() should not be used here, just the count of the entries in the array.

    btw, casting return value from malloc() is unnecessary if you have the correct header files included (stdlib)

    unsigned *char char_ptr = malloc( sizeof( unsigned char ) * 20 ); // redundant, sizeof(char) is 1
    int *int_ptr = malloc( sizeof( int ) * 20 );
    long long_ptr = malloc( sizeof( long ) * 20 );

    .. Some initialisation of the arrays is necessary, but ommitted...

    // all 3 are accessed by a simple index...
    for( i = 0; i != 20; i++ ) {
        printf( "%d\n", char_ptr[i] );
        printf( "%d\n", int_ptr[i] );
        printf( "%ld\n", long_ptr[i] );
    LVL 11

    Expert Comment

    so in your example, solvePath[2] will return the 3rd "thing" that solvePath is an array of...

    so you should use:

    solvePath[nestDepth*2] = curX;
    solvePath[nestDepth*2+1] = curY;
    LVL 5

    Author Comment

    Exce;;ent. Thanks for your help!

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now