Dynamically allocated structures in C


 
Thread Tools Search this Thread
Top Forums Programming Dynamically allocated structures in C
# 1  
Old 07-17-2009
Dynamically allocated structures in C

I have a pointer to a structure containing an integer pointer:

struct members {
int id;
int *neigh;
};

The number of members N and its neighbors M change as the code runs, so I allocate the memory dynamically:

members *grid = malloc(sizeof(members)*N);
for(i=0;i<N;i++) grid.neigh = malloc(sizeof(int)*M);

where M generally is a function of N.

This works well most of the time, but sometimes grid.neigh contains weird (and wrong) values. Is there anything fundamentally wrong in this way of allocating memory space?
# 2  
Old 07-17-2009
Did you mean:

for(i=0;i<N;i++) grid[i].neigh = malloc(sizeof(int)*M);

If so, I don't see anything fundamentally wrong with it. The newly-allocated memory is not guaranteed to be blank, however:

Code:
for(i=0;i<N;i++)
{
  grid[i].neigh = malloc(sizeof(int)*M);
  memset(grid[i].neigh, 0, sizeof(int) * M);
}

# 3  
Old 07-18-2009
Quote:
Originally Posted by Corona688
Did you mean:

for(i=0;i<N;i++) grid[i].neigh = malloc(sizeof(int)*M);
Yes, that is of course what i meant. Thanks for the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Retreving the dynamically allocated values from bdb using C

In one of the assignment which i am working on, i am am trying to insert keys and values into BDB by reading the input records from a input file as below. Here keys i am inserting as character buffer and for values i am dynamically allocating the memory using malloc and then inserting into bdb.... (1 Reply)
Discussion started by: AmbikaValagonda
1 Replies

2. UNIX for Advanced & Expert Users

Retreving the dynamically allocated values from bdb using C

In one of the assignment which i am working on, i am am trying to insert keys and values into BDB by reading the input records from a input file as below. Here keys i am inserting as character buffer and for values i am dynamically allocating the memory using malloc and then inserting into bdb.... (1 Reply)
Discussion started by: AmbikaValagonda
1 Replies

3. Shell Programming and Scripting

Help with variables for filesystems allocated

Hi all, I am interning in a unix department and am very new to programming. I am supposed to write a script that counts the amount of filesystems a server has allocated, and the amount free. This is what I was given to start with: #!/bin/ksh df -m | grep -v ":"|grep -v Free|grep -v "/proc"|... (6 Replies)
Discussion started by: compan023
6 Replies

4. Solaris

Can be changeed the allocated space

i am working with solaris 9 and my disk usages are # df -k Filesystem kbytes used avail capacity Mounted on /dev/dsk/c0t0d0s0 2148263 1902721 202577 91% / /proc 0 0 0 0% /proc mnttab 0 0 0 ... (3 Replies)
Discussion started by: smartgupta
3 Replies

5. Solaris

xntpd[28781]: too many recvbufs allocated

Hi, I have a server that is getting the following alarm a couple times a day: Mar 25 10:56:54 hostname xntpd: too many recvbufs allocated (30) Mar 25 10:56:54 hostname xntpd: too many recvbufs allocated (30) I know this is some sort of NTP related issue but I need to gauge the severity of... (0 Replies)
Discussion started by: BrewDudeBob
0 Replies

6. Linux

about system structures

hello can any1 plz tell me about the system defined structures (like sysinfo) which wil give system and n/w charecteristics (ex: freeram in sysinfo). (1 Reply)
Discussion started by: jeenat
1 Replies

7. Solaris

Memory allocated

Hi, How to find out what is the maximum memory allocated to TOMCAT server in SunOS 5.8? The Tomcat server crashes down during peak times.... Regards (1 Reply)
Discussion started by: baanprog
1 Replies

8. Programming

Programming using Structures

Hi All, I was given a format of a file, and was asked to write a program which displays the data contained in the file in that purticular format. Its all so confusing. Please find the example of the format as well the code I have written in the attachment. I hope any one of u guyz can... (0 Replies)
Discussion started by: jazz
0 Replies

9. Programming

pointer to structures

Dear friends I have a bit basic doubts in pointers and the structures inter relationships. the first one. static struct apvt { int dead; int pending; int abouttograb; }*agents=NULL; what agents pointer is... (1 Reply)
Discussion started by: tech_voip
1 Replies

10. Programming

reallocating structures dynamically in functions

I've recently started using structures, but I am having problems in allocating the structure dynamically. In the code below if i allocate the structure in the main program it works fine, and i get the expected output. However if i use the function rper below to increase the size of the structure i... (0 Replies)
Discussion started by: cezaryn
0 Replies
Login or Register to Ask a Question