ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I think he wants to know how many elements he's got to work with in a malloc()ed area. Tricky one, that. You could try catching the SEGV signal and bouncing up the pointer until it triggers.
Yes but if you have a mallo'd area then you will know how much area has been allocated - or at least should know. Then the following should help you out:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
void *y, *x, *z;
int alloc_size = 100*sizeof(long);
y = malloc(alloc_size);
x = y;
z = y;
printf("Hello\n%i chars\n%i shorts\n%i longs\n%i pointers\n"
,alloc_size/sizeof(char)
,alloc_size/sizeof(short)
,alloc_size/sizeof(long)
,alloc_size/sizeof(void*)
);
system("PAUSE");
return 0;
}
Just a thought when you do an sizeof of a pointer you be back the size of the pointer. So as above if you do
int *x;
x = (int *)malloc(100*sizeof(x));
int mysize;
mysize = sizeof(x)/sizeof(int); //doesn't work.
when you do the sizeof(x) you will be back 4 bytes (if it is a 16 bit int) or 8 bytes (if it is a 32 bit int) size. It does not give you back the memory you have allocated. So when you do the sizeof(x)/sizeof(int) you should be back 1 since you a dividing by the same thing.
Exactly, the sizeof returns the size of the scalar variable passed to it. So if you pass it an array of intergers it will look at it, and because C will interpret an array as a pointer to the first element it will return the length of a pointer. If you dereference the array then it will return the size of the first element.
However my point was that if a slab of memory has been alloc'd then you should know how much has been allocated, and thus how much is available regardless of what it is you want to hold in that allocated area of memory. So it's probably just a case of remembering how much has been allocated.
The tricky part is I'm passing a pointer to an array in a function.
In the function, based on the size, I would like to know if I need to realloc more memory. The only solution I've found is to also pass a pointer indicating the current size, and increment.
So for example, a scaled down version might look like this:
Code:
/********************/
int moremem(void** target, int* numelts, int type)
{
int newnumelts = *numelts + 1;
if(*target == NULL)
{
*target = (int *)malloc(newnumelts*sizeof(int));
}
Your hitting a language implementation issue here. In some languages Pascal for example the number of elements in the array is passed along with the array. This is done by the language.
Not so for C, that is why in most system function written in C when a structure is passed there is a twin argument the size of the structure.
This allows the function to work even if different structures are passed in. That is important because as computing develops new structures are required to meet these changing needs. An example would be the socket functions. The first function may have been written for UNIX raw sockets but then UDP and TCP came along. Now we have IPv6 which has a larger structure because its contains more structured data. However the original functions still work because the size of the structure is passed to the function.
As an aside the newest forms of these structures contain the size of the structure as the second element of the structure (the first being the type of structure - I think) so if you really wanted just one parameter then you could create your own structure with the first element being the size of the structure and the second element being a pointer to the memory that has been allocated.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.