LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2005, 09:50 PM   #1
ltordsen
LQ Newbie
 
Registered: Aug 2005
Distribution: suse 9.3
Posts: 16

Rep: Reputation: 0
sizeof problem


Hello.

I want to find the size of an object. This doesn't quite work. Also, this is C, not C++

/********************/
int *x;
x = (int *)malloc(100*sizeof(x));

int mysize;
mysize = sizeof(x)/sizeof(int); //doesn't work.

/***********************/

How can I make this work?

In the actual program, I wont know the size of the array. Thanks for your help!!
 
Old 12-03-2005, 09:55 PM   #2
ltordsen
LQ Newbie
 
Registered: Aug 2005
Distribution: suse 9.3
Posts: 16

Original Poster
Rep: Reputation: 0
Nevermind--I see other threads now--Its determined at compile time. I was hoping that wasn't the case
 
Old 12-04-2005, 08:48 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I'm not certain what it is you want to do, however you can allocate 100 integers just by using an array. See the following code

Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int x[100];
  printf("Hello\n%i %i\n",sizeof(int),sizeof(x));
  system("PAUSE");	
  return 0;
}
This shows that x is 100 times the size of the int. On my computer it came up with 4 and 400.

graeme.
 
Old 12-04-2005, 08:57 AM   #4
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally Posted by graemef
I'm not certain what it is you want to do
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.
 
Old 12-04-2005, 09:12 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
malloced area

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;
}
graeme
 
Old 12-04-2005, 09:42 AM   #6
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
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.
 
Old 12-04-2005, 10:19 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.

graeme
 
Old 12-04-2005, 11:57 PM   #8
ltordsen
LQ Newbie
 
Registered: Aug 2005
Distribution: suse 9.3
Posts: 16

Original Poster
Rep: Reputation: 0
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));
}

else
{
*target = (int *)realloc(*target,newnumelts*sizeof(int));
if(*target == NULL)
{
printf("couln't allocate memory\n");
return 0;
}
}
*numelts = newnumelts;
return 1;
}
/******************************/

This works, but I would like to get rid of the numelts argument.
 
Old 12-05-2005, 09:13 AM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.

graeme.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof and malloc in C exvor Programming 4 08-07-2005 12:06 PM
Trying to install QTParted - problem with "sizeof" Robstro Linux - Software 1 04-18-2004 04:24 PM
sizeof( ) in c++ question amoor757 Programming 6 01-26-2004 02:42 PM
sizeof my typedef... jhorvath Programming 7 10-11-2002 05:41 PM
sizeof() and C/C++ boku Programming 2 09-01-2002 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration