f



Structures and malloc at runtime

Please could someone look at the following code and see if with some
modifications it is possible to make it work:
I just want to allocate some memory at runtime for 500 structures (in
this example) and use it as in the example

Thank you
Carlos


struct data {
  char name[10];
  long age;
} ENTRY;

struct ENTRY * list1 = (struct ENTRY *)malloc(500 * sizeof(ENTRY));

strcpy(list1[1].name, "Hello");    (1)
list1[1].age = 5;

(1) will give the following errors:
unknown size for type 'incomplete struct ENTRY defined at....
unknown field 'name' of 'incomplete struct ENTRY defined at...
type error in argument 1 to 'strcpy'; found 'int' expected 'pointer to
char'

0
bigpig (1)
5/31/2007 7:38:25 PM
comp.compilers.lcc 1279 articles. 0 followers. Post Follow

3 Replies
371 Views

Similar Articles

[PageSpeed] 9

Hello!

This isn't a compiler problem (and especially not specific to lcc), but
a basic problem of understanding C.

 <bigpig@webmail.co.za> wrote:

>struct data {
>  char name[10];
>  long age;
>} ENTRY;

So you define a type "struct data" and at the same time you define one
variable of that type. The variable is named ENTRY (which is a bit bad
style, usually you don't use all uppercase for variable, structure, or
type names).

For your usage, it'd be enough to say
	struct data {
		char name[10];
		long age;
	};

That way you define the structure type, but no variable of a single
struct data.

>struct ENTRY * list1 = (struct ENTRY *)malloc(500 * sizeof(ENTRY));

So this must be
	struct data *list1 = (struct data *) malloc(500 * sizeof(struct data));

Btw, for malloc, you should #include <stdlib.h>

>strcpy(list1[1].name, "Hello");    (1)
>list1[1].age = 5;

Those 2, then, should succeed after the previous change. But it accesses
the *second* of the 500 allocated elements (C indexes arrays starting
frmo zero). strcpy is from #include <string.h>, btw.

>(1) will give the following errors:
>unknown size for type 'incomplete struct ENTRY defined at....
>unknown field 'name' of 'incomplete struct ENTRY defined at...
>type error in argument 1 to 'strcpy'; found 'int' expected 'pointer to
>char'

In fact you should already have been given an error message for the
malloc line.

And, btw, your string handling will work in this example (with the other
corrections applied). But it's quite error prone.

For example the compiler would accept
	strcpy(list1[1].name, "quite a bit longer string");
quite well. But since the string is much longer than those 10 chars (and
remember the one additional char at the end of C style strings, having
'\0' as value!), the copy process will run over the end of the storage
you have for name (inside struct data). And what lies thereafter? First
the "age" of the same structure. And then? ... You can trash arbitrary
memory that way, creating obscure program bugs, and if the input string
is influenced by a possibly hostile party (e.g. a string read from a
network connection), you can create security holes in your software.
(Look for the term "buffer overflow").

Kind regards,

Hannah.
0
h12942
5/31/2007 7:48:01 PM
bigpig@webmail.co.za said:

> Please could someone look at the following code and see if with some
> modifications it is possible to make it work:
> I just want to allocate some memory at runtime for 500 structures (in
> this example) and use it as in the example
> 

#include <stdlib.h> /* malloc */
#include <string.h> /* strcpy */

> 
> struct data {
>   char name[10];
>   long age;
> } ENTRY;
> 
> struct ENTRY * list1 = (struct ENTRY *)malloc(500 * sizeof(ENTRY));

Ouch. What a complicated line.

Try this instead:

  struct data *list1 = malloc(500 * sizeof *list1);

  if(list1 != NULL)
  {

etc

-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
0
Richard
5/31/2007 7:55:27 PM
bigpig@webmail.co.za wrote:
> 
> Please could someone look at the following code and see if with
> some modifications it is possible to make it work:
> I just want to allocate some memory at runtime for 500
> structures (in this example) and use it as in the example
> 
> struct data {
>   char name[10];
>   long age;
> } ENTRY;

This declares a data item of type struct data named ENTRY.  Please
don't use upper case for data names, reserve the upper case for
macros.
> 
> struct ENTRY * list1 = (struct ENTRY *)malloc(500 * sizeof(ENTRY));

This should create a compiler error.  Replace "ENTRY" with "data". 
Also eliminate the cast.  You are obviously using C, not C++, and
the cast can only hide errors.  Have you #included <stdlib.h> ?
> 
> strcpy(list1[1].name, "Hello");    (1)
> list1[1].age = 5;
> 
> (1) will give the following errors:
> unknown size for type 'incomplete struct ENTRY defined at....
> unknown field 'name' of 'incomplete struct ENTRY defined at...
> type error in argument 1 to 'strcpy'; found 'int' expected
> 'pointer to char'

Now I expect the above code will work.

-- 
 <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
 <http://www.securityfocus.com/columnists/423>
 <http://www.aaxnet.com/editor/edit043.html>
 <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                        cbfalconer at maineline dot net



-- 
Posted via a free Usenet account from http://www.teranews.com

0
CBFalconer
5/31/2007 9:41:03 PM
Reply: