Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > zeroing structure of char pointers

Reply
Thread Tools

zeroing structure of char pointers

 
 
Aaron Walker
Guest
Posts: n/a
 
      02-29-2004
I was just wondering, if I have:

struct my_struct {
char *str1;
char *str2;
};

and then elsewhere do:

struct my_struct *ms = calloc(1, sizeof(struct my_struct));

Would that be the same as doing:

struct my_struct *ms = malloc(sizeof(struct my_struct));
ms->str1 = ms->str2 = NULL;


Thanks,
Aaron
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      02-29-2004

On Sun, 29 Feb 2004, Aaron Walker wrote:
>
> I was just wondering, if I have:
>
> struct my_struct {
> char *str1;
> char *str2;
> };
>
> and then elsewhere do:
>
> struct my_struct *ms = calloc(1, sizeof(struct my_struct));
>
> Would that be the same as doing:
>
> struct my_struct *ms = malloc(sizeof(struct my_struct));
> ms->str1 = ms->str2 = NULL;


Nope. In the first case, you're allocating memory for a struct,
and then 'calloc' is initializing all the allocated bits to zero.
In the second case, you're allocating memory for a struct, and then
initializing ms->str1 to NULL and ms->str2 to NULL.
So there are two key differences:

1. NULL != all bits zero. This is a FAQ.
http://www.eskimo.com/~scs/C-faq/q7.31.html
As a corollary, structs may have padding bits, which would not get
initialized to anything in the second case (but would receive 'zero'
bits in the first, along with every other bit). This is also a FAQ,
but is much less relevant to your actual problem.

2. The second snippet invokes undefined behavior if 'malloc'
returns NULL (failure to allocate enough memory). The first snippet
is perfectly well-defined in all cases -- but, as above, it won't do
what you're expecting it to do.

Read the FAQ; there's lots of important stuff there.

HTH,
-Arthur

 
Reply With Quote
 
 
 
 
Mac
Guest
Posts: n/a
 
      02-29-2004
On Sun, 29 Feb 2004 02:44:40 +0000, Aaron Walker wrote:

> I was just wondering, if I have:
>
> struct my_struct {
> char *str1;
> char *str2;
> };
>
> and then elsewhere do:
>
> struct my_struct *ms = calloc(1, sizeof(struct my_struct));
>
> Would that be the same as doing:
>
> struct my_struct *ms = malloc(sizeof(struct my_struct));
> ms->str1 = ms->str2 = NULL;
>


Not necessarily. The portable solution is to assign NULL to all pointers.

>
> Thanks,
> Aaron


Mac

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      02-29-2004
In 'comp.lang.c', "Mac" <(E-Mail Removed)> wrote:

>> I was just wondering, if I have:
>>
>> struct my_struct {
>> char *str1;
>> char *str2;
>> };
>>
>> and then elsewhere do:
>>
>> struct my_struct *ms = calloc(1, sizeof(struct my_struct));
>>
>> Would that be the same as doing:
>>
>> struct my_struct *ms = malloc(sizeof(struct my_struct));
>> ms->str1 = ms->str2 = NULL;


> Not necessarily. The portable solution is to assign NULL to all pointers.


Of course, you meant 'a' portable solution. Here is an alternative :

struct my_struct *ms = malloc (sizeof *ms);

if (ms != NULL)
{
/* clear the struct */
static const struct my_struct z = {0};
*ms = z;

/* the rest of the code... */
}

--
-ed- http://www.velocityreviews.com/forums/(E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Pointers to structure and array of structure. Excluded_Middle C Programming 4 10-26-2004 05:39 AM
zeroing an array richard C Programming 20 09-07-2003 05:34 PM
zeroing in on a listbox item rsine ASP General 2 09-02-2003 10:44 PM



Advertisments