Thread: size of the memory reserved for a pointer

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Question size of the memory reserved for a pointer

    Hello,

    I've got a doubt I didn't find in books. Is there a way of knowing how much memory has been reserved for a pointer.

    I guess, C must mark somehow the memory reserved for a pointer (by marking last position, saving size of the memory reserved,...??) to avoid conflicts using that memory for another pointer. So, if this is correct, I guess there must a way of knowing how many memory was reserved previously for that pointer. Am I correct?

    (The idea is to know in a function which receives a pointer the size it has reserved)

    Well, thank you for your help.

    Cristian

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    pointers don't reserve memory, they are just variables that point to memory locations. YOU have to allocate that memory (or point it to memory already allocated). In terms of how dynamically allocated memory keeps track of the size, it's not explicitly defined by the language. It can vary from compiler to compiler. Usually that data is stored in the memory prior to the location that the poiner points to.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    thank u for your answer

    However, I think I must be missing something, or getting something wrong. Do you mean that the size of the allocated memory is stored in position -1?
    Well, it sounds wierd to me. Is that right? or I did not catch what you meant? How would I acces to the size information?

    thank you

    Cristian

  4. #4
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    The pointer has no size. It is just pointing to some memory location that contains your data. You'd have to keep track of how big you made the array.
    If you want to find out how big a data type is, you are looking for sizeof(). That will tell you the size reserved for an int, float, char, etc.

    Here's an example...
    Code:
    #include <stdio.h>
    
    main()
    {
        printf("float: %d\n" , sizeof(float));
        printf("int: %d\n" , sizeof(int));
        printf("double: %d\n" , sizeof(double));
        printf("long: %d\n" , sizeof(long));
        printf("long long: %d\n" , sizeof(long long));
        printf("long double: %d\n" , sizeof(long double));
    }

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    A pointer-variable does have a size. This size depends on which system you use. I thought a pointer-variable is usually integer size. You can check it like this:

    Code:
    char *ptr;
    printf ("Size of pointer-variable is %d", sizeof (ptr));
    The size of a dynamically allocated datastructure is not stored, you should keep administration of its size if you want to know it.
    Last edited by Shiro; 03-04-2003 at 12:34 PM.

  6. #6
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    The pointer itself doesn't contain the size though. A pointer is of a data type, whose size can be told through sizeof(). It's all how you phrase things...I interpretted it alittle differently I guess

    At any rate, if you create a dynamic array, you will need to keep track of the size of that array, it's not stored anywhere in the array.

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by cris_orpi
    How would I acces to the size information?
    As I said, there is nothing in the standard that says how the data must be stored or how to access it. You simply CAN'T get the size. You have to keep track of it by yourself.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How would I acces to the size information?
    Code:
    size_t size = some_num;
    
    p = malloc ( size * sizeof *p );
    if ( p != NULL )
      printf ( "%p points to %lu blocks\n", p, (unsigned long)size );
    The size information is now in the size variable. This is the only portable and accurate/safe way to remember how much memory is allocated to you.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    Actually, this is the way I allocate the memory assiated to pointer "p".

    But, if I pass pointer "p" to a function, were I'm reading or writing information in the memory associated, can have any control of whether or not I'm writing out of the reserved memory?

    My doubt is if it is possible to know the size of the allocated memory pointed by a pointer, in case I don't have acces to the variable "size".

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by cris_orpi
    Actually, this is the way I allocate the memory assiated to pointer "p".

    But, if I pass pointer "p" to a function, were I'm reading or writing information in the memory associated, can have any control of whether or not I'm writing out of the reserved memory?

    My doubt is if it is possible to know the size of the allocated memory pointed by a pointer, in case I don't have acces to the variable "size".
    Most functions that receive a pointer as an arg use one of two method to determine how much data they can read/write. In the case of strings (char arrays), the nul terminator ('\0') is used to denote the end of the array. The second method is to pass the size of the memory chunk as another parameter.
    Code:
    void foo(char *p, int len)
    {
       printf ("I can read/write %d bytes at location %p\n", len, (void*)p);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Question

    and what if function "foo" doesn't receive "len" value? Would there be a way, inside function "foo", of knowing the size of the memory pointed to pointer "p"? or, in another words, to have control of whether or not we are writing/reading information out of the reserved memory?

    For what's been said, I guess there's no way of doing that Is this right?

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is this right?
    Correct, there is no guaranteed way of getting the size of allocated memory. Which is why we told you to place the size in a variable from the start.

    -Prelude
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    OK! Thank you.

    But, then, there's must be a way for the program to control what memory is already being used.

    How does the program know where it can allocate new memory?

  14. #14
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Polymorphic OOP
    In terms of how dynamically allocated memory keeps track of the size, it's not explicitly defined by the language. It can vary from compiler to compiler. Usually that data is stored in the memory prior to the location that the poiner points to.
    In other words, it can vary. That's not something specifically specified by the language, which is why you have to keep track of it by yourself as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM