473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

zeroing structure of char pointers

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
Nov 14 '05 #1
3 1259

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

Nov 14 '05 #2
Mac
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

Nov 14 '05 #3
In 'comp.lang.c', "Mac" <fo*@bar.net> 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- em**********@noos.fr [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/
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
2
by: Adam Balgach | last post by:
Greetings everyone, ive got a problem ive been working with for quite a while and need some help. ive got a structure: struct Data { char *data1; char *data2; int val1; int val2;
4
by: dd | last post by:
Hello. My primary goal is to initialize array of pointers to structures like this: struct R aa={ {"asd",{"dsa","dda"},{"441","882"}}, {"ddd",{"aaa","666"},{"111","772"}},...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
9
by: Dadio | last post by:
Hi! I have to take some strings from a file and put them in a record... The various strings in the file are written on this way: string1|string2|string3|string4|string5| This is the program...
9
by: nano2 | last post by:
Hi , Have the following case passing in structure pointers and returning structure pointers can anyone spot whats wrong with this structure name is structCar void callfn( ){
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
8
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.