Sponsored Content
Top Forums Programming reallocating structures dynamically in functions Post 36355 by cezaryn on Tuesday 27th of May 2003 11:12:59 PM
Old 05-28-2003
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 get totally wrong results. Am i passing the structure wrong to the function? From what i understand I am just passing the pointer and realloc just chages the pointer, which then should be returned to the main function. But it seems that the allocation of elem, poi ,sign messes something up. Thanks for your help
Cezary

Program:
#include<stdlib.h>
typedef struct peri{
int no;
int *elem;
int *poi;
int *sign;
int reduced;
int type;
} peri ;
void rper(int size,peri *per);

int main (int argc, char *argv[]){
int nnods=10,i;
peri *per;
per= (peri *) malloc((2)*sizeof(peri));
per[1].no=2;
for(i=1;i<=nnods;i++){
rper(i,per);
/* per=realloc(per,(i+1)*sizeof(peri));
per[i].elem=(int*)malloc(2*sizeof(int));
per[i].poi=(int*)malloc(2*sizeof(int));
per[i].sign=(int*)malloc(2*sizeof(int));
per[i].no=i;*/
printf(" for i %d per.no is %d\n",i,per[i].no);
}
return;
}
void rper(int size,peri *per){
per=realloc(per,(size+1)*sizeof(peri));
per[size].elem=(int*)malloc(2*sizeof(int));
per[size].poi=(int*)malloc(2*sizeof(int));
per[size].sign=(int*)malloc(2*sizeof(int));
per[size].no=size;

}

Results if realloc in main:
for i 1 per.no is 1
for i 2 per.no is 2
for i 3 per.no is 3
for i 4 per.no is 4
for i 5 per.no is 5
for i 6 per.no is 6
for i 7 per.no is 7
for i 8 per.no is 8
for i 9 per.no is 9
for i 10 per.no is 10

Results if realloc in function:
for i 1 per.no is 1
for i 2 per.no is 0
for i 3 per.no is 0
for i 4 per.no is 0
for i 5 per.no is 0
for i 6 per.no is 0
for i 7 per.no is 0
for i 8 per.no is 0
for i 9 per.no is 0
for i 10 per.no is 0
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. Programming

How to use structures in functions which is presented in another file..

Hi .... i m having a file called deque.c which contains structure deque and i pass the structure to function declaration push . the function push is defined in some other file .. and i added the header file also .. i list the codings below... //deque.c #include<stdio.h> #include... (0 Replies)
Discussion started by: rkarthi2k5
0 Replies

5. UNIX for Dummies Questions & Answers

Reallocating disk space

Ok. I just built a new box from a Flash Archive and it worked great, but when I partitioned the boot disk I didn't leave any space on slice 7 to create meta devices for the mirroring. DOH! My question. I assigned 8GB for SWAP on slice 1 - can I shink or reassign space from the slice and... (2 Replies)
Discussion started by: Probos
2 Replies

6. Programming

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++)... (2 Replies)
Discussion started by: brinch
2 Replies

7. Shell Programming and Scripting

Perl Data Structures

Here is what i need to do. @data #has all column wise data so say info for col 1 location for all rows would be in this array $array = \@data But i need to create a file which should contain these information in a format for all columns even if i have got no values from some of the index... (0 Replies)
Discussion started by: dinjo_jo
0 Replies

8. Programming

Signalsafe data structures

Hello, I have a signal handler which manipulates a data structure. The data structure's operations aren't atomic. So if two threads/processes are in a critical section at the same time the data structure will be broken. With threads you can avoid this stuff with semaphores etc. However,... (10 Replies)
Discussion started by: littlegnome
10 Replies

9. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

10. Shell Programming and Scripting

Shell script to apply functions to multiple columns dynamically

Hello, I have a requirement to apply hashing algorithm on flat file on one or more columns dynamically based on header sample input file ID|NAME|AGE|GENDER 10|ABC|30|M 20|DEF|20|F say if i want multiple columns based on the header example id,name or id,age or name,gender and hash and... (13 Replies)
Discussion started by: mkathi
13 Replies
ARES_FREE_DATA(3)					     Library Functions Manual						 ARES_FREE_DATA(3)

NAME
ares_free_data - Free data allocated by several c-ares functions SYNOPSIS
#include <ares.h> void ares_free_data(void *dataptr) cc file.c -lcares DESCRIPTION
The ares_free_data(3) function frees one or more data structures allocated and returned by several c-ares functions. Specifically the data returned by the following list of functions must be deallocated using this function. ares_get_servers(3) When used to free the data returned by ares_get_servers(3) this will free the whole linked list of ares_addr_node structures returned by ares_get_servers(3). ares_parse_srv_reply(3) When used to free the data returned by ares_parse_srv_reply(3) this will free the whole linked list of ares_srv_reply structures returned by ares_parse_srv_reply(3), along with any additional storage associated with those structures. ares_parse_mx_reply(3) When used to free the data returned by ares_parse_mx_reply(3) this will free the whole linked list of ares_mx_reply structures returned by ares_parse_mx_reply(3), along with any additional storage associated with those structures. ares_parse_txt_reply(3) When used to free the data returned by ares_parse_txt_reply(3) this will free the whole linked list of ares_txt_reply structures returned by ares_parse_txt_reply(3), along with any additional storage associated with those structures. ares_parse_soa_reply(3) When used to free the data returned by ares_parse_soa_reply(3) this will free the ares_soa_reply structure, along with any additional storage associated with those structure. RETURN VALUE
The ares_free_data() function does not return a value. AVAILABILITY
This function was first introduced in c-ares version 1.7.0. SEE ALSO
ares_get_servers(3), ares_parse_srv_reply(3), ares_parse_mx_reply(3), ares_parse_txt_reply(3), ares_parse_soa_reply(3) AUTHOR
Yang Tse Copyright 1998 by the Massachusetts Institute of Technology. Copyright (C) 2004-2010 by Daniel Stenberg. 5 March 2010 ARES_FREE_DATA(3)
All times are GMT -4. The time now is 04:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy