• C

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You should be returning solvePath[nestDepth*2]

Do not put sizeof(int) into the index

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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:


wouldn't querying solvePath[2] return the 2nd byte not the 2nd word?
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] );
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;
basiclifeAuthor Commented:
Exce;;ent. Thanks for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.