dynamic allocation vs static allocation in c


 
Thread Tools Search this Thread
Top Forums Programming dynamic allocation vs static allocation in c
# 1  
Old 05-31-2009
dynamic allocation vs static allocation in c

i wrote a tiny version of tail command using a large buffer statically allocated but, in a second time, i found another version in which i use a bidimensional array dynamically allocated.
here is the first version
Code:
 /*my tiny tail, it prints the last 5 line of a file
*/
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#define MAXSIZE 5000
void flush(char* buff,int n);
int main(int argc,char*argv[]){
    int i,n,fd,count;
    char buff[MAXSIZE];
    if (argc<2){printf("no file name inserted\n"),exit(1);}
    else if(argc>2) printf("too many parameters\n"),exit(1);
    else{    
        if((fd=open(argv[1],O_RDONLY))<0) {perror("open error"),exit(1);}
        /*file opened in readonly mode*/
        lseek(fd,-1,SEEK_END);/*the offset points the char before the end of file*/
        count=0;
        i=MAXSIZE;
        flush(buff,MAXSIZE);/*clean the buffer*/
        while(count!=5){
            if((read(fd,&buff[i],sizeof(char)))<0) perror("read error"),exit(1);
            if(buff[i]=='\n') ++count;/*count increments when the program meet a new line */
            --i;
            lseek(fd,-2,SEEK_CUR);/*read puts the offset one char forward */
            }
        for(n=i;n<MAXSIZE-1;++n)
            printf("%c",buff[n]);
        printf("\n");
        close(fd);
        }
    return 0;    
}
void flush(char* buff,int n){
    int i;
    for(i=0;i<n;++i) buff[i]=0;
}

the question is:
in program like this with large buffer is better dynamic allocation or static?
thanks
(PS: sorry for my bad english Smilie)

Last edited by lucasclaus; 06-01-2009 at 04:21 AM..
# 2  
Old 06-01-2009
What's the difference? Memory is memory.
# 3  
Old 06-01-2009
The size of the executable will generally be smaller with dynamic memory allocation.
# 4  
Old 06-01-2009
Can you say why you need the buffer...is it just for printing the last line. Dynamic allocation maybe slower than static allocation due to the overhead of calling malloc otherwise as noted memory is memory.
# 5  
Old 06-01-2009
Quote:
Originally Posted by fpmurphy
The size of the executable will generally be smaller with dynamic memory allocation.
Does that include stack allocation as in the example? Didn't think it had to put that in any data segment the way it would with globals.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Memory Allocation Query

When we dynamically allocate the memory say 100 integers say int *x = new int(1000); then does entire chunk of memory gets allocated at once after the completion of the statement? I mean will the the concept of page fault come into picture over here? (3 Replies)
Discussion started by: rupeshkp728
3 Replies

2. Solaris

Block-based allocation and Extent-based allocation in Solaris

Hi guys! Could you tell me what's this figure about? (See the attached figure below.) This is a representation of block allocation filesystem and extent allocation filesystem in Solaris. Does this mean that in a block-based allocation, data are placed in individual blocks while in... (0 Replies)
Discussion started by: arah
0 Replies

3. Programming

Dynamic Memory Allocation

Hello Guys I have a small confusion in the dynamic memory allocation concept. If we declare a pointer say a char pointer, we need to allocate adequate memory space. char* str = (char*)malloc(20*sizeof(char)); str = "This is a string"; But this will also work. char* str = "This... (2 Replies)
Discussion started by: tene
2 Replies

4. Programming

Memory allocation in C

Hi Experts I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables. First: int x; char a; printf("%u %u\n', &x, a); I got the addresses displayed... (2 Replies)
Discussion started by: unx_freak
2 Replies

5. Programming

global variables and dynamic allocation

Hi, is it possible in C to allocate dynamically a global variable?? (3 Replies)
Discussion started by: littleboyblu
3 Replies

6. Programming

array dynamic allocation

Hi, I have the following problem: i must allocate a dynamic array from a subroutine which should return such array to main function. The subroutine has already a return parameter so i thought of pass the array as I/O parameter. I tried the following program but it doesn't work (segmentation... (11 Replies)
Discussion started by: littleboyblu
11 Replies

7. Programming

Dynamic memory allocation

Hi, I am trying to process line by line of a file. But I should not be allocating static allocation for reading the contents of the file. The memory should be dynamically allocated. The confusion here is how do I determine the size of each line, put it into a buffer with the memory allocated... (11 Replies)
Discussion started by: naan
11 Replies

8. UNIX for Dummies Questions & Answers

HOw to get a variable allocation

HI Gurus, I had a requirement where i want to allocate a file name into a variable and get the file name in the subj of email. Suppose i have a file File002.pdx in the folder /home/pcs/system/files/File002.pdx Iam using a variable a = `ls /home/pcs/system/files/*.pdx` Iam using *... (2 Replies)
Discussion started by: pssandeep
2 Replies

9. Programming

Static variables memory allocation

Hi I want to know when and where memory for static variables are allocated in a C program. If it allocates during compilation will memory be allocated for the variable "i" during compilation itself. int count(); int main(){ printf("%d", count()); return 0; } int count() { ... (8 Replies)
Discussion started by: nathanmca
8 Replies

10. UNIX for Dummies Questions & Answers

memory allocation

I would like to know how I could allocate some more memory to a process. Please note that I am not the root user. (1 Reply)
Discussion started by: sagar
1 Replies
Login or Register to Ask a Question