In one function I have written:
char *ab;
ab=malloc(10);
Then in another function I want to know the size of memory pointed by the ab pointer.
Is there any way that I can know that ab is pointing to 10 chars of memory?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s a deep secret that only free() knows for sure. It’s likely in your system, but in a totally implementation dependent manner.
A bit awkward, but if you want to keep everything together:
typedef struct { // size of data followed by data (C only trick! NOT for C++) int dimension; // number of data elements int data[1]; // variable number of data elements } malloc_int_t; malloc_int_t *ab; int dimension = 10; ab = malloc( sizeof(*ab) + (dimension-1)*sizeof(int) ); ab->dimension = dimension; ab->data[n] // data accessI’ve changed the data type to int to make the code a more generic template.