Dynamic memory allocation


 
Thread Tools Search this Thread
Top Forums Programming Dynamic memory allocation
# 8  
Old 09-02-2008
This is one of those problems that I've always enjoyed because most of the text processing utilities we use have statically defined or user defined limits in moving parts. (Think gawk, perl, etc...).
The Heathfield link is as good an interface as I've seen.
A quick (and easily broken) data structure and some logic for working with the idea.
Code:

typedef struct _line {
int len;
long lineno;
char *record;
struct _line *prv;
} LINE;

LINE *newallocline(int l, long rno, char *data, LINE *prv) {
LINE *new = NULL;

                    if ( (new = malloc(sizeof(new))) == NULL) {return NULL;}
                    new->record = malloc(l * sizeof(char));
                    if (new->record == NULL) {free(new); new = NULL; return NULL;}
                    strncpy(new->record,data,l);
                    new->lineno = rno;
                    new->prv = prv;
                    return new;
}                    
                                      
/*pseudo code
*prv = NULL;
* while (gets input into absurdly static large_buffer (500000 characters) for line)
        if ( (cnew = newallocline(strlen(large_buffer),cnt++,large_buffer,prv)) == NULL) {error();}
        prv = cnew;
}
*/

# 9  
Old 09-03-2008
I have a programming test where it is mandatory that only dynamic allocation should be done for any file manipulations.
# 10  
Old 09-03-2008
Quote:
Originally Posted by naan
I have a programming test where it is mandatory that only dynamic allocation should be done for any file manipulations.
Is it some kind of classwork problem ?
# 11  
Old 09-03-2008
Rules for these UNIX forums:
  • Don't ask about homework
  • Don't ask about homework
  • Don't ask about homework

Nonetheless, this is a really important question that should be answered, because, by golly, I was working on it today.

Actually, my problem is slightly different. I'm augmenting a shared-library routine which parses the command-line arguments. My task is to pass those arguments to sprintf(). How do I make sure:
  1. I have enough space allocated for all the arguments?
  2. I don't write past the end of memory?
  3. I don't run out of memory

Imagine a directory with 10,000 files in it. (This happens quite a lot in Bioinformatics and cluster computing). Then I do a "echo *". Let's say I'm augmenting the command "echo". I have to make sure it can handle 10,000 arguments, which means dynamically allocating a very long string.

One way is to statically allocate a large chunk of memory, and if it's not enough, report that the program is out of memory or that there are too many arguments and process what can be processed.

Another way is to dynamically allocate memory byte-by-byte (or in my case, argument-by-argument). You can do this easily enough with the realloc() call. A good realloc() implementation is very efficient, and actually reserves memory in pools, so that realloc'ing 1 more byte does nothing more than increment some internal counter somewhere. The realloc() call can be used on a NULL pointer in place of malloc(), so you can just it from the start.

If the realloc() call is inefficient, you can create one yourself by allocating memory in chunks. You allocate, say, 256 bytes at a time, and you have a counter of how long the buffer actually is and another counter tracking how much is being used. Every time you read in (or in my case, copy in) a new value (or argument), you check to see if you have enough space in the buffer. If you don't, you go out and allocate some more.
# 12  
Old 09-03-2008
Allocating memory in 256 byte chunks may seem efficient until you move past userspace.
I'm smiling here so please don't be offended.

This is what I was trying to get at earlier. Most 'smart' text processing utilities/languages depend on the user to tell them: 'How many records should I read and what is a record', or 'How long is a line and how do I determine it?' either at compile time, or at runtime.

As most of us know from sad experience many standard utilities come with static
limitations.

This is a challenging problem and looking at the mailing lists for comp.lang.awk and
other text processing languages is always educational.

Last edited by ramen_noodle; 09-03-2008 at 02:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C++/ROOT Memory Allocation?

Hello, I am new to C++ programming, so I'm still getting a feel for things. I recently wrote a simple C++ program (to be used as a ROOT Macro) to conduct a statistical analysis of a varied version of the Monty Hall problem (code below). Basically, the programs runs a few simple calculations to... (7 Replies)
Discussion started by: Tyler_92
7 Replies

2. Shell Programming and Scripting

memory allocation to a variable

hello all.. i'm a beginner in shell scripting. I need to know what is really happening when we are creating a variable in shell scripting? how memory is allocated for that variable? (3 Replies)
Discussion started by: aarathy
3 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

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 /*my tiny tail, it prints the last 5 line of a file */ #include<stdio.h>... (4 Replies)
Discussion started by: lucasclaus
4 Replies

6. Programming

Is there a problem with the memory allocation???

I have a scenario like the client has to search for the active server.There will be many servers.But not all server are active.And at a time not more than one server will be active. The client will be in active state always i.e, it should always search for an active server until it gets one.I... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

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

8. Programming

Memory allocation problem

I have a program that will fetch some particular lines and store it in a buffer for further operations.The code which is given below works but with some errors.I couldn't trace out the error.Can anybody help on this plz?? #include <stdio.h> #include <stdlib.h> #include<string.h> #define... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

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

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