Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

useing realloc() many times or less times.

Status
Not open for further replies.

sagar474

Full Member level 5
Joined
Oct 18, 2009
Messages
285
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,318
Location
India,kakinada
Activity points
3,122
out of which which one is the better


Code:
file=fopen(argv[1],"r");

arr=(unsigned int *)malloc(4);

    while(!feof(file))
        {

++arr_size;
arr=(unsigned int *)realloc(arr,arr_size*sizeof(unsigned int));
fscanf(file,"%u\n",&arr[arr_size-1]);

        }
fclose(file);


Code:
arr=(unsigned int *)malloc(some_size*sizeof(unsigned int));


file=fopen(argv[1],"r");

arr=(unsigned int *)malloc(4);

    while(!feof(file))
        {

++arr_size;

fscanf(file,"%u\n",&arr[arr_size-1]);

        }
fclose(file);
 

Hi sagar

There is no much difference between that two snippets expect that realloc allocates the memory contiguously from starting element...so based on memory you can use any one in that two functions.....upto my knowledge first one is better...
 
if we execute realloc() more thean 10000 times even then we can use first one?
 

no..because there might be cause run time errors like memory violations....
 

if we execute realloc() more thean 10000 times even then we can use first one?

The realloc() function utilizes both the malloc() and free() within its code. It is in effect combining the action of both functions into one. Therefore apart from some minor overhead due to pointer checking, etc, it is as if you freed the previous allocation and then allocated another block memory.

Checkout the source in your compiler.

BigDog
 

when i eliminate using realloc() and allocated a large memory with malloc() then the program execution is taking little more time. why?
actually the program execution time must reduced.
 

It's quite obvious, that the incremental realloc() will be basically slower. It's only advantage is not needing to estimate a file size.

In a real world, the decision would depend on parameters like:
- single or multi-tasking enviroment
- maximum file size
- static or dynamic (e.g. swapping) memory resources
- performance objectives
 

when i eliminate using realloc() and allocated a large memory with malloc() then the program execution is taking little more time. why?
actually the program execution time must reduced.

That is odd. Is it the same size block of memory?

Can you find the source for realloc() on your system?
 

if i use if condition to test if the memory is sufficient or not and then re allocate then i think i cant take advantage of pipelining due to if statements. and when i tried to eliminate realloc() the profiler is showing more execution time.
 

realloc.c

Code:
/* Work around bug on some systems where realloc (NULL, 0) fails.
   Copyright (C) 1997 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* written by Jim Meyering */

#if HAVE_CONFIG_H
# include <config.h>
#endif
#undef realloc

#include <sys/types.h>

char *malloc ();
char *realloc ();

/* Change the size of an allocated block of memory P to N bytes,
   with error checking.  If N is zero, change it to 1.  If P is NULL,
   use malloc.  */

char *
rpl_realloc (p, n)
     char *p;
     size_t n;
{
  if (n == 0)
    n = 1;
  if (p == 0)
    return malloc (n);
  return realloc (p, n);
}
 

i think second one is better between these two. but just looking at those two functions writing fscanf inside loop is useless.
i think it would be better if we do it in this way...

char *arr;
unsigned int length;

file=fopen(argv[1],"r");

fseek(file,0,SEEK_END);
length=ftell (file);
arr=(char*)malloc((length+1)*sizeof(char));
fscanf(file,"%u\n",&arr[length]);
fclose(file);
 
Last edited:

Yeah exactly that caught my eye After writing the post.. get an answer is to write a post I guess all that's required to .. especially if you're debugging your application for nearly 4 hours to find a problem.
Thanks!
**broken link removed**
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top