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 7 of 13 Topic 1067 of 1126
Post > Topic >>

Re: Confusion of Malloc and Sizeof

by Barry Schwarz <schwarzb@[EMAIL PROTECTED] > Mar 30, 2008 at 02:58 PM

On Fri, 28 Mar 2008 19:20:27 -0500 (CDT), shofu_au@[EMAIL PROTECTED]
 wrote:

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

You don't.  These are the two solutions popular available

>
>For instance in my example how do I determine the number of characters
>available to write into?
>
>#include <stdio.h>
>#include <stdlib.h>
>
>void do_read (char *buffer)
>{
>  int numCharAvailable ;
>  numCharAvailable = sizeof (*buffer);

Since this always evaluates to 1, it obviously doesn't do what you
want.

>  printf ( "Number of elements available %d\n", numCharAvailable);
>}
>
>int main (int argc, char **argv)
>{
>  char *readBuffer;
>  readBuffer = (char *) malloc (15);
>  do_read (readBuffer);
>}
>
>I wish to use the readBuffer pointer in ASYNC callbacks and I would
>like to know the number of elements dynamically allocated?

In the special case where the buffer has had values stored it in, you
have the additional option of placing a sentinel value immediately
after the last "real" value.  do_read could then step through the
buffer looking for the sentinel (similar to the way strlen works).

If you are allocating space for an array (as opposed to a structure),
you could allocate one extra element (or in the case or a char array,
sizeof(int) extra elements) and store the size in the first element
(element 0) and use elements 1 through N for your array.


Remove del for email
-- 
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 Wed Jul 9 0:33:27 CDT 2008.