# Thread: How do i know the size of variable pointer

1. ## How do i know the size of variable pointer

Hi all,

I have a variable pointer, which i allocate the memory using the malloc command. I want to reverse the order of the variable pointer. But how do i know the last memory address for this?

Code:
```#define ARRAY_SIZE 100;
..
int x = ARRAY_SIZE

int *pa, *pb, *pc, *pd;
pa = malloc(sizeof(int))*x);

int j = 0;
for(j=0; j<6;j++)
{
pa[j] = j+1;              //sign pa = {1,2,3,4,5,6}
pb[j] = j+7;              //sign pb = {7,8,9,10,11,12}
}```
i want to reverse the order of pa, so it become {6,5,4,3,2,1}, but how do i know the address of memory with value 6. Any idea?

Thanks you

2. You wrote it, so you should know how many values you put in the array.

3. Originally Posted by cpjust
You wrote it, so you should know how many values you put in the array.
In this case i know how many values, but suppose i don't know how many values that i have assigned. is there any way to know it?

Because, in my understanding, if i already reserve the memory using malloc, so all the memory will be assigned with 0.

4. ^^.

Malloc doesn't initialize memory to zero, it has garbage data.

You need calloc to initialize memory to zero.

5. Originally Posted by unikgila
Because, in my understanding, if i already reserve the memory using malloc, so all the memory will be assigned with 0.
No, malloc() does nothing of the sort. calloc() on the other hand, does set all the memory to 0.

If you don't know how many values you put in the array, you need to redesign your program, since you obviously forgot to include a variable to keep track of that.

6. Originally Posted by MeTh0Dz
^^.

Malloc doesn't initialize memory to zero, it has garbage data.

You need calloc to initialize memory to zero.
I mean Malloc doesn't initialize memory to zero, but assigned the value to 0.
Code:
```int main()
{
int x = ARRAY_SIZE;
int *pa, *pb, *pc, *pd;
pa = malloc(sizeof(int) * x);
pb = malloc(sizeof(int) * x);

int j = 0;
for(j=0; j<6;j++)
{
pa[j] = j+1;
pb[j] = j+7;
}

int k = 0;
for(k=0; k<20;k++)
{
printf("%d ",*(pa+k));
}

printf("\n");
k = 0;
for(k=0; k<20;k++)
{
printf("%d ",*(pb+k));
}
}```
and the result is :
Code:
```7 8 9 10 11 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0```
in this case, the value 0 is assigned automatically by Malloc. My problem is i don't know how to get the last value that i have assigned, so i can reserve the order of it. I know in string we can use
Code:
```void reverve(char *string){
char *end, char *start = string;
end = string + strlen(string) -1;
..
}```

7. i think others have mentioned it already (unless im not understanding the problem), but you need a variable to keep track of the size. it looks like you already have a statement to define the array size, so use that. the last element in your array would be the array plus the max size - 1.

if you have an array like int iArray[5], i think you can use sizeof(iArray)/sizeof(int). if you use malloc and only have a pointer, i dont think you can get the size any other way besides being given the size. many functions that work with char arrays or other types of arrays will require an argument for the size of the array, so the function will not go out of bounds.

in order to actually reverse an array (and not just print the reverse of it), you need another array of the same size (well, at least the same size). then you copy the last element of the original array to the first element of the new reversed array, etc. you could then "free" the original array and have the original pointer point to your new malloc'd reversed array.

you should also be careful when your making a post, as it just makes things difficult for us to understand your problem. in your first post you say "reverse", your last post you said "reserve", and the code example says "reverve".

8. No it doesn't. It's just dumb luck that it's 0 -- very bad luck too.

You assign the values, so you should know which was the last one to be assigned. In this case, the last spot you assigned to was pb[5] and pa[5].

you could do;

Code:
```struct array_s
{
int * elements;
size_t used;
size_t size;
};

/* ... make the array */
struct array_s pa;
pa.size = 20;
pa.elements = malloc(pa.size * sizeof(int));
/* check result of malloc */
pa.used = 0;

/* ... set first six */
for(i = 0; i < 6; i++)
{
pa.elements[i] = i + 1;
}
pa.used = 6;```

9. Originally Posted by unikgila
I mean Malloc doesn't initialize memory to zero, but assigned the value to 0.
If you put 1 bullet in a gun, point it at your head and pull the trigger, you might get lucky and not kill yourself. Would you then conclude that you can pull the trigger as many times as you want and it will never kill you?

When you turn on your computer, all the RAM in your machine is 0, and it doesn't change until a program gets loaded in that location. The part of RAM where your array is just happens to be all 0's now, but that's not guaranteed to always be true.

10. A simple example:
Code:
```int main()
{
int *p = malloc(4 * sizeof(int));

for(i = 0; i < 4; i++)
{
p[i] = 100;
}

free(p);
p = malloc(4 * sizeof(int));

for(i = 0; i < 4; i++)
{
printf("p[%d] = %d\n", i, p[i]);
}
return 0;
}```
Most likely, you will get 100 as the output. It may not be - it may be just about anything.

"fresh" memory from the OS is most often filled with zero so that the OS doesn't leak information from one process to another [you wouldn't be very happy if all someone had to do to find your password to the banking site was to be "lucky" when allocating memory and finding it in the memory that the program allocated].

--
Mats

11. Originally Posted by unikgila
in this case, the value 0 is assigned automatically by Malloc. My problem is i don't know how to get the last value that i have assigned, so i can reserve the order of it.
It's not guaranteed that malloc() will initialize the allocated memory to zero just sheer dumb luck as zacs7 says.
Reversing the stored sequence of values in the memory that pa points to? You have the index of the last memory location that contains the number 6. Using that you could walk backwards while exchanging the complementary elements of the int array pa.