# Memory alignment issue Wrong pointers after memory allocation

**URL:** <https://forums.developer.nvidia.com/t/memory-alignment-issue-wrong-pointers-after-memory-allocation/22189>\
**Category:** CUDA Programming and Performance\
**Created:** [April 27, 2011, 11:21am UTC](https://forums.developer.nvidia.com/t/memory-alignment-issue-wrong-pointers-after-memory-allocation/22189 "2011-04-27T11:21:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![A1taiR](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/a1tair/32/23158_2.png) [@A1taiR](https://forums.developer.nvidia.com/u/A1taiR)\
**Post date:** [April 27, 2011, 11:21am UTC](https://forums.developer.nvidia.com/t/memory-alignment-issue-wrong-pointers-after-memory-allocation/22189/1 "2011-04-27T11:21:42Z")

</div>

Have such piece of code:

```auto
__device__ void make_box2ind(double *x, int dim, int N, int comp1, int comp2, int bs,int inveps, int *ind, int *box, int *lis) {

	printf("Thread %d make_box2ind started \n", threadIdx.x);

	int d, i, ix, iy, ixy;

	int ib = bs - 1;

	double *xx;

	xx = (double *) malloc(dim*N);

	printf("Make_box2ind mem allocated \n");

	memcpy(xx, x, N * dim * sizeof(double));

	printf("make_box2ind mem copied \n");

        ...

	free(xx);

}

```

This function is called from **global** one, that is runned by only one thread, so program is serial.

And all is going wrong after declaration of those integers:

 ![Untitled-1.jpg](https://global.discourse-cdn.com/nvidia/original/2X/7/72a52244f8c67ebba2c74545d37706b214b9b1ba.jpeg)

and then BAM!

 ![Untitled-2.jpg](https://global.discourse-cdn.com/nvidia/original/2X/e/ecdeb7d6b131ee5f460092348a32151a55f9e132.jpeg)

variable dim changed it value from 4 to -1 without any reason.

However, if I’ll remove those declarations, another issue comes:

 ![Untitled-3.jpg](https://global.discourse-cdn.com/nvidia/original/2X/9/9e0f2d0143bfc6cd2f2d7e6ee085b7b74ee42a40.jpeg)

and then

 ![Untitled-4.jpg](https://global.discourse-cdn.com/nvidia/original/2X/5/507e81be8b2b6c299355323704dc6badb1a8a5c2.jpeg)

dim changed again.

Tried both toolkits: 3.2 and 4.0 on GForce GTX550Ti with sm\_20.

I can’t even imagine: what’s wrong with that. Seems like problem with alignment, but how to resolve that???

Will be very appreciate for your advices.

 ![Untitled-4.jpg](https://global.discourse-cdn.com/nvidia/original/2X/5/507e81be8b2b6c299355323704dc6badb1a8a5c2.jpeg)  
 ![Untitled-3.jpg](https://global.discourse-cdn.com/nvidia/original/2X/9/9e0f2d0143bfc6cd2f2d7e6ee085b7b74ee42a40.jpeg)  
 ![Untitled-2.jpg](https://global.discourse-cdn.com/nvidia/original/2X/e/ecdeb7d6b131ee5f460092348a32151a55f9e132.jpeg)  
 ![Untitled-1.jpg](https://global.discourse-cdn.com/nvidia/original/2X/7/72a52244f8c67ebba2c74545d37706b214b9b1ba.jpeg)
