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

Re: Confusion of Malloc and Sizeof

by Jack Klein <jackklein@[EMAIL PROTECTED] > Mar 30, 2008 at 02:58 PM

On Fri, 28 Mar 2008 19:20:27 -0500 (CDT), shofu_au@[EMAIL PROTECTED]
 wrote
in comp.lang.c.moderated:

> 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.

Why do you think that you need to?

When you allocated the memory, you knew exactly how much you asked for
and, if the allocation was successful, you received.  Why are you so
insistent on hiding this knowledge?

> 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);

This use of the sizeof operator is most certainly not going to do what
you want.  sizeof *any_pointer_to_char is guaranteed to be 1, always.
Even for a null pointer or one that is uninitialized.

>   printf ( "Number of elements available %d\n", numCharAvailable);
> }
> 
> int main (int argc, char **argv)
> {
>   char *readBuffer;
>   readBuffer = (char *) malloc (15);

Note that since you have correctly included <stdlib.h> which provides
a prototype for malloc(), the cast is completely unnecessary in C.
Many experienced C programmers, myself included, feel that it is
detrimental.

>   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?

On fairly common alternative that eliminates passing more than one
pointer or keeping a global variable is something like this:

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

struct dyn_string
{
    char *the_string;
    size_t its_size;
};

void do_read(struct dyn_string *ds)
{
   printf("Size is %lu\n", (unsigned long)ds->its_size);
}

#define SIZE 15

int main(void)
{
   struct dyn_string ds;
   if (NULL != (ds.the_string = malloc(SIZE)))
   {
      ds.its_size = SIZE;
      do_read(&ds);
   }
   else
   {
      puts("Memory allocation failed");
   }

   free(ds.the_string);
   return 0;
}

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.para****ft.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
-- 
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 Fri Jul 25 21:15:46 CDT 2008.