Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: Confusion o...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 13 Topic 1067 of 1134
Post > Topic >>

Re: Confusion of Malloc and Sizeof

by jgd@[EMAIL PROTECTED] (John Dallman) Mar 30, 2008 at 02:58 PM

In article <clcm-20080328-0003@[EMAIL PROTECTED]
>, shofu_au@[EMAIL PROTECTED]
 () 
wrote:

> How do you determine the number of elements in a dynamically allocated
> variable, without passing the number or having a global variable.

You can't. C does not provide this information. 

You need to understand that sizeof is a strictly compile-time operation, 
and cannot tell you things whose answers could be determined at 
run-time. And the size of dynamic memory allocations is determined at 
run-time, when the malloc call is executed. The fact that you have given 
a literal size in the source code does not help at all. 

Some implementations of malloc will store the size in ways that can be 
accessed by means of pointer arithmetic on the address, but these are 
not part of the language, and are in no way constrained to work in the 
same manner across different operating systems, compilers, or run-time 
libraries. Using them means you will, at some awkward future time, find 
yourself in deep trouble. So don't. 

If you are starting on the architecture of a complex system, it may be 
worth creating a memory allocation scheme of your own on top of malloc, 
and such a system can provide what you're asking for - there are many 
possible ways to do it - but C does not. 

-- 
John Dallman, jgd@[EMAIL PROTECTED]
 HTML mail is treated as probable spam.
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 13 Posts in Topic:
Confusion of Malloc and Sizeof
shofu_au@[EMAIL PROTECTED  2008-03-28 19:20:27 
Re: Confusion of Malloc and Sizeof
=?ISO-8859-1?Q?Hans-Bernh  2008-03-30 14:58:44 
Re: Confusion of Malloc and Sizeof
Chris McDonald <chris@  2008-03-30 14:58:13 
Re: Confusion of Malloc and Sizeof
Carl Barron <cbarron41  2008-03-30 14:58:26 
Re: Confusion of Malloc and Sizeof
=?utf-8?Q?Dag-Erling_Sm=C  2008-03-30 14:58:47 
Re: Confusion of Malloc and Sizeof
Jack Klein <jackklein@  2008-03-30 14:58:24 
Re: Confusion of Malloc and Sizeof
Barry Schwarz <schwarz  2008-03-30 14:58:33 
Re: Confusion of Malloc and Sizeof
Thomas Richter <thor@[  2008-03-30 14:58:41 
Re: Confusion of Malloc and Sizeof
Erik Trulsson <ertr101  2008-03-30 14:58:35 
Re: Confusion of Malloc and Sizeof
gordonb.lbk55@[EMAIL PROT  2008-03-30 14:58:18 
Re: Confusion of Malloc and Sizeof
Ulrich Eckhardt <dooms  2008-03-30 14:58:29 
Re: Confusion of Malloc and Sizeof
jgd@[EMAIL PROTECTED] (J  2008-03-30 14:58:38 
Re: Confusion of Malloc and Sizeof
Barry Schwarz <schwarz  2008-03-30 14:58:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:37:50 CDT 2008.