Re: How to assign memory dynamically to a array of structures
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 11 Aug 2006 21:15:35 GMT
skumar434@xxxxxxxxx writes:
I am faceing problem while assigning the memory dynamically to a array
of structures .
This look awfully familiar. Haven't you started several other threads
about this same problem? Why not just post a followup in the original
thread?
Suppose I have a structure
typedef struct hom_id{
int32_t nod_de;
int32_t hom_id;
int32_t hom_type;
int32_t hom_pid;
} hom_data;
Your struct tag "hom_id" is the same as the name of one of your struct
members. This is legal but confusing.
If you insist on using a typedef for your structure, and it doesn't
contain any pointers to the same type, you don't actually need the
tag:
typedef struct {
/* members */
} hom_data;
Or you can use the same identifier for the struct tag and the typedef:
typedef struct hom_data {
/* members */
} hom_data;
Personally, I'd drop the typedef and just refer to the type as "struct
hom_data", but a lot of people like typedefs for various reasons.
I created array of structures in my program so that i can store the
data in array
hom_data arr[];
I want to know how to assign memory to this array of structure at
runtime.
Can anybody point waht is wrong with this code .
I able to compile it but its printing garbage values.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
Ok.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
These last 4 headers are not defined in standard C; they're
Unix-specific. If you actually used anything from them, I'd tell you
to try comp.unix.programmer. But as far as I can tell you're not, so
you can just delete them (at least in code you post here).
typedef struct hom_id{
int32_t nod_de;
int32_t hom_id;
int32_t hom_type;
int32_t hom_pid;
} hom_data;
int main()
Better:
int main(void)
{
FILE *fout;
hom_data hom[30];---->how to allocate memory to this array of stuct
dynamically.
int i=0;
/* open input file and test to see if it is empty */
if ((fout = fopen("momtext.txt", "r")) == NULL) {
printf("error ---- file read is empty\n\n");
exit(1); }
exit() is declared in <stdlib.h>; you need a #include for it. If your
compiler didn't warn you about that, turn up the warning level.
exit(1) is non-portable. Use exit(EXIT_FAILURE).
/* open output file */
fout = fopen("momtext.txt", "r");
What? You just opened this file (and checked the result of fopen();
good for you). Now you open it again for reading, but your commment
says "open output file", and you don't check the result.
And why do you call it "fout" when it's used for input?
while (fscanf (fout ,"%08x %08x ", &hom[i].nod_de, &hom[i].hom_id)
!= 2)
{
fscanf(fout ,"%08x %08x", &hom[i].hom_type, &hom[i].hom_pid );
You checked the result of the first fscanf(), but not the second.
Always check the result of fscanf(). I think you're assuming there
will be no errors in your input file; that's a dangerous assumption.
Maybe you should use a single fcanf() to read all 4 value.
(I'd use fgets() followed by sscanf(); use fscanf() directly should
work, but it doesn't give you much flexibility in dealing with
errors.)
printf("%08x %08x ", hom[i].nod_de, hom[i].hom_id);
The "%08x" format for both fscanf() and printf() specifies an argument
of type unsigned int (or pointer to unsigned int for fscanf()).
You're using arguments of type int32_t, which may or may not be the
same type. <inttypes.h> provides format specifiers for int32_t, but
it's unwieldy. For output, use "%08lx" and cast the arguments to
unsigned long. For input, you can read into temporaries of type
unsigned long and then assign to your uint32_t members.
printf("%s %s %s\n", hom[i].hom_type, hom[i].hom_pid);
You tell printf you're going to give it three char* arguments pointing
to strings, then you give it two int32_t arguments. Of course you're
getting garbage.
i++;
}
return 0;
}
Your real question was about how to dynamically allocate an array of
structures, when the number of structures you need is going to depend
on an input file (i.e., you don't know in advance).
You can either use malloc() to allocate an array, then use realloc()
to grow it as needed (and *always* check the value returned by both
functions), or you can use malloc() allocate one record at a time and
add each one to a linked list. (The latter isn't an array, which is
what you asked for, but it might suit your purposes. If not, you
could also build the linked list, then once you know how big it is you
can use a single malloc() to allocate an array and copy the linked
list elements into it. This does increase your memory requirements,
though.)
For a first draft, though, you might just declare a fixed-size array
and store values into it, keeping track of how many elements are being
used. This will fail if the input is too big, but it should be easier
to implement, and it will give you a chance to fix all the other
errors in your code before you start worrying about memory allocation.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: How to assign memory dynamically to a array of structures
- From: skumar434
- Re: How to assign memory dynamically to a array of structures
- References:
- How to assign memory dynamically to a array of structures
- From: skumar434
- How to assign memory dynamically to a array of structures
- Prev by Date: Re: passing a union's field to a function
- Next by Date: Re: passing a union's field to a function
- Previous by thread: Re: How to assign memory dynamically to a array of structures
- Next by thread: Re: How to assign memory dynamically to a array of structures
- Index(es):
Relevant Pages
|