[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Size Of Arrays

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
0
basiclife
Asked:
basiclife
  • 2
  • 2
1 Solution
 
cupCommented:
You should be returning solvePath[nestDepth*2]

Do not put sizeof(int) into the index
0
 
basiclifeAuthor Commented:
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:

[00000000,11111111,00000000,1111111]

wouldn't querying solvePath[2] return the 2nd byte not the 2nd word?
0
 
cjjcliffordCommented:
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)

e.g.
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] );
}
0
 
cjjcliffordCommented:
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;
0
 
basiclifeAuthor Commented:
Exce;;ent. Thanks for your help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now