How do i know the size of variable pointer

This is a discussion on How do i know the size of variable pointer within the C Programming forums, part of the General Programming Boards category; Hi all, I have a variable pointer, which i allocate the memory using the malloc command. I want to reverse ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    18

    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. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You wrote it, so you should know how many values you put in the array.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by cpjust View Post
    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. #4
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    ^^.

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

    You need calloc to initialize memory to zero.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by unikgila View Post
    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.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    18
    Quote Originally Posted by MeTh0Dz View Post
    ^^.

    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. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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;
    Last edited by zacs7; 10-27-2008 at 09:41 PM.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by unikgila View Post
    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.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by unikgila View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21