Indefinite-length array as member of struct; sizeof(char)

Gregory Kemnitz kemnitz at mitisft.Convergent.COM
Wed Jul 19 17:23:33 AEST 1989


In article <14474 at dartvax.Dartmouth.EDU| ari at eleazar.dartmouth.edu (Ari Halberstadt) writes:
|I sent the following article, but it seems to have arrived without its
|body [probably would have forgotten its head if it wasn't attached :-)].
|I'm still new to the news networks, so I hope it doesn't appear twice.
|The article follows:
|
|In article <821 at fozzy.UUCP> ellis at fozzy.UUCP (Randy Ellis) writes:
|>In article <7360 at c3pe.UUCP>, charles at c3pe.UUCP (Charles Green) writes:
|>> When I know how long the string is I'm pushing onto the stack, I say:
|>> 	nodeptr = malloc(strlen(data)+5);
|>> to cover the struct node* and terminating NULL, and then simply
|>> 	strcpy(nodeptr->string, data);
|>
|>Does this fit your needs?  Change string into a char pointer, then allocate
|>dynamic memory for the string and save the pointer into nodeptr->string.
|>	nodeptr = malloc(sizeof(struct node));
|>	nodeptr->string = malloc(strlen(data)+1);
|>	strcpy(nodeptr->string,data);
|
|The sollution given by Randy Ellis is incomplete. Specifically, it omits
|a type cast from type "char *" to type "struct node *". The correct line
|should read:
|	nodeptr = (struct node *) malloc(sizeof(struct node));
|
|I also have a question about memory allocation. Are characters guaranteed
|to be only one memory address long? This is very important in expressions
|such as:
|	nodeptr->string = malloc(strlen(data)+1);
|Should the above line really be written as:
|	nodeptr->string = malloc( (strlen(data)+1) * sizeof(char) );
|I have seen the latter in several books, but it really wasn't clear from the
|examples what should be done.
|

On page 188 in K & R (First Edition):

      The "sizeof" operator yields the size, in bytes, of its operand.  (A
byte is undefined in the language except in terms of the value of sizeof.
However, in all existing implementations a byte is the space required to
hold a "char".)

In my experience, I have never seen an implementation of C where
sizeof(char) != 1.  (Is there any out there????)  However, I have seen many
where a "byte" is not eight bits, like the Unisys 1100 and the Honeywell 6000
computers (both extremely old architectures).  Since I have not used an ANSI
C compiler, I don't know what ANSI says about sizeof(char), but if it is not
1 the ANSI committee should all be fired :-).

The only other size definition I have seen (this has been talked about at
length on another thread) is

sizeof char <= sizeof short <= sizeof int <= sizeof long

|If possible, please include a source (e.g., book name) with your response.
|Ari Halberstadt '91, "Long live short signatures"

----------------------------------+--------------------------------------
Greg Kemnitz                      | Software without hardware is an idea.
kemnitz at Convergent.COM            | Hardware without software is a space heater.
				  |
                                  | --Unknown author



More information about the Comp.lang.c mailing list