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
|
|
|
|
Reply
|
bigpig (1)
|
5/31/2007 7:38:25 PM |
|
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
|
|
|
|
Reply
|
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
|
|
|
|
Reply
|
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
|
|
|
|
Reply
|
CBFalconer
|
5/31/2007 9:41:03 PM
|
|
|
3 Replies
94 Views
(page loaded in 0.104 seconds)
Similiar Articles: segmentation fault when shared object using STL is statically ...Note that EXE and so are linked with same gcc version and with same C runtime ... On UNIX, memory allocated (via malloc(), calloc(), realloc(), etc.) by one DSO can ... data structures on shared memory? - comp.unix.programmer ...Hi, I've got a bit of a complex data structure based on linked lists, which I use for ... Dear Gordon, > > Been looking at them, at gnu malloc, Doug's Lea malloc (which has ... Determining the stack size at run-time - comp.os.ms-windows.ce ...the maximum memory size allowed in malloc - comp.lang.c Determining the stack size at ... is not the ... the ... out at least: It looks like when the stack size ... runtime ... Malloc & sbrk - comp.unix.programmerHi, could someone explain me how malloc() (particularly doug lea's) deals with ... malloc() in CURRENT has been rewritten by Jason Evans to use mmap(). > The runtime ... templates + RTTI + shared library = impossible? - comp.lang.c++ ...so, different class-info structures for the same class will ... issues can also manifest in other ways as well: malloc ... Runtime Type Checking in C++ without RTTI « Ciaran ... Calculating member offset at runtime - comp.lang.c++.moderated ...But if I don't mind doing the offset calculation at runtime, would a straightforward ... You can use something like this where no structure needs to be instantiated (same ... error C2375 - redefinition; different linkage - comp.lang.c ...I have used the technique in the past to hijack malloc in a file that implements a data structure when I want to enable more detailed anaylsis of allocation associated ... Solaris 10: How can I get a date that is exactly 30 days back ...... date_format=(char *)malloc(strlen(argv[2])); > > date_format=(char *)malloc(strlen ... called for "exactly 30 days back" so in C you could calculate program start,runtime ... Static reflection - a base for runtime reflection? - comp.lang.c++ ...I have seen some post on the newsgroup but they are old and most is on runtime ... it with C++03 or C++0x, by exposing that information as compile-time data structures. How to check whether malloc has allocated memory properly in case ...i want to check after calling malloc whether allocation is successful or not ... I've seen rewrites of electronic structure codes for cray computers that would have ... C dynamic memory allocation - Wikipedia, the free encyclopedia... of block scope having sizes determined at runtime. ... is allocated as "chunks", an 8-byte aligned data structure ... TCMalloc, a malloc developed by Google, has garbage ... C Programming Tutorial: Dynamic Memory Allocation - randu.orgBut what if we want to create a 2D array of doubles at runtime ... Therefore, we have our desired 2D array structure. The first MALLOC call instructs malloc to ... 7/10/2012 4:33:47 AM
|