Memory allocation problem


 
Thread Tools Search this Thread
Top Forums Programming Memory allocation problem
# 1  
Old 04-03-2009
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??

Code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

#define LINESIZE 256

void failif (int condition, char * message)
{
    if (condition)
    {
         printf("%s\n", message);
         exit(EXIT_FAILURE);
    }
}

void read_lines (char * * lines_array, FILE * fp, int start, int end)
{
    char line[LINESIZE];
    int i = 0, line_counter = 1;

    while(fgets(line, sizeof(line), fp) != NULL)
    {
        //Once line_counter matches up with start, "start recording" till end.
        if((line_counter >= start) && (line_counter <= end))
        {
            lines_array[i++] = strdup(line);
        }
        line_counter++;
    }
}

int main (int argc, char const *argv[])
{
    FILE *fp;
    int start,end,i;
    char * * lines_array;
    
    failif (argc != 4, "this program requieres three arguments");

    fp = fopen(argv[1], "rb");
    failif (!fp, "Could Not Open File");
    
    // The line range I want
    start   = atoi(argv[2]);
    end     = atoi(argv[3]);
    
    // allocate storage for the lines:
    lines_array = (char **) malloc((end - start) * sizeof(char *));
    failif (!lines_array, "memory allocation failed");

    read_lines (lines_array, fp, start, end);

    for(i = 1; i <( end - start)+1; ++i)
    {
        printf("\n%d.%s",i, lines_array[i]);
    }

     new code: free memory used by the allocated storage
    for (i = 0; i < end - start; i++)
        free (lines_array[i]);
    free (lines_array);

    return 0;
}

this is the output i get


Quote:
[root@wer ~]# ../a.out "ug2.io" "8" "15"

1.wintmyd3 - D3 sponsor car

2.davidchoeart - David Choe sponsor car

3.tunejapantuning - Japantuning sponsor car

4.gimmechingy - Chingy Navigator sponsor car

5.needmybestbuy - Best Buy vinyl in Career mode

6.goforoldspice - OldSpice vinyl in Career mode

7.gottahavebk - BurgerKing vinyl in Career mode
*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x0878c170 ***
======= Backtrace: =========
/lib/i686/nosegneg/libc.so.6[0xbfd651]
/lib/i686/nosegneg/libc.so.6(cfree+0x90)[0xc00cd0]
./a.out[0x8048722]
/lib/i686/nosegneg/libc.so.6(__libc_start_main+0xe0)[0xba9390]
./a.out[0x80484b1]
======= Memory map: ========
003a0000-003ab000 r-xp 00000000 08:07 7662226 /lib/libgcc_s-4.1.2-20070925.so.1
003ab000-003ac000 rwxp 0000a000 08:07 7662226 /lib/libgcc_s-4.1.2-20070925.so.1
00407000-00408000 r-xp 00407000 00:00 0 [vdso]
00b74000-00b8f000 r-xp 00000000 08:07 7662217 /lib/ld-2.7.so
00b8f000-00b90000 r-xp 0001a000 08:07 7662217 /lib/ld-2.7.so
00b90000-00b91000 rwxp 0001b000 08:07 7662217 /lib/ld-2.7.so
00b93000-00ce9000 r-xp 00000000 08:07 7662218 /lib/i686/nosegneg/libc-2.7.so
00ce9000-00ceb000 r-xp 00156000 08:07 7662218 /lib/i686/nosegneg/libc-2.7.so
00ceb000-00cec000 rwxp 00158000 08:07 7662218 /lib/i686/nosegneg/libc-2.7.so
00cec000-00cef000 rwxp 00cec000 00:00 0
08048000-08049000 r-xp 00000000 08:07 4816575 /root/a.out
08049000-0804a000 rw-p 00000000 08:07 4816575 /root/a.out
0878c000-087ad000 rw-p 0878c000 00:00 0
b7e00000-b7e21000 rw-p b7e00000 00:00 0
b7e21000-b7f00000 ---p b7e21000 00:00 0
b7f9f000-b7fa0000 rw-p b7f9f000 00:00 0
b7fbc000-b7fbf000 rw-p b7fbc000 00:00 0
bff49000-bff5f000 rw-p bff49000 00:00 0 [stack]
Aborted




this is the ug2.10 file


Quote:
NOTE: These are the main Need for Speed: Underground 2 cheats that are usually applied by pressing down the right key combination. Please find the instructions on how to use these cheats below.

Enter one of the following codes before pressing [Enter] to get into the main menu to activate the corresponding cheat function:

opendoors - The Doors sponsor car
yodogg - Snoop Dogg sponsor car
wannacapone - Capone sponsor car
shinestreetbright - ShinestStreet sponsor car
wintmyd3 - D3 sponsor car
davidchoeart - David Choe sponsor car
tunejapantuning - Japantuning sponsor car
gimmechingy - Chingy Navigator sponsor car
needmybestbuy - Best Buy vinyl in Career mode
goforoldspice - OldSpice vinyl in Career mode
gottahavebk - BurgerKing vinyl in Career mode
gotmycingular - Cingular vinyl in Career mode
gottaedge - Edge vinyl in Career mode
needperformance1 - Unlock performance parts level 1
needperformance2 - Unlock performance parts level 2
gimmevisual1 - Unlock visuals level 1
gimmevisual2 - Unlock visuals level 2
ordermebaby - $1,000 in Career mode, RX-8 and Skyline in quick race mode
regmybank - $200 in Career mode
regmebaby - $20,000 in Career mode
# 2  
Old 04-03-2009
It looks like this line:
Code:
lines_array = (char **) malloc((end - start) * sizeof(char *));

Is not allocating the right amount, I would think that you want something like
Code:
lines_array = (char **) malloc((end - start) * LINESIZE);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Memory allocation problem

I am using ubuntu. I have written a program to calculate prime factors. it works perfectly fine till entered number is less than 9989 (or so ) but when one enters a number higher than that, for example 15000, it does not work. Can anyone guide me whats the problem ? although new codes are welcome,... (2 Replies)
Discussion started by: Abhishek_kumar
2 Replies

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

3. Programming

memory allocation for string in C

hi in the following code, how the memory is allocated for a1 which holds the values of a2 after cpy function call. #include <stdio.h> #include <string.h> void cpy(char* d, const char* s){ while(*d++=*s++); } main(){ char* a1; char* a2="done"; cpy(a1,a2); ... (3 Replies)
Discussion started by: mprakasheee
3 Replies

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

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

6. Programming

memory allocation in subroutine

Hi everyone, I'm not new to C programming, but I'm having question regarding the memory allocation of a pointer variable which, for instance, will be declared in main(), but its memory will be allocated in subroutine. To clearify my question, I provide a small working example: #include... (1 Reply)
Discussion started by: MIB_Maik
1 Replies

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

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

9. HP-UX

HP-UX memory usage allocation

Hi all, I have a HP-UX Server with 4 gigabytes of physical RAM. When I use the 'Glance' utility to see what my memory utilization is, my memory usage shows up maxed out at 99%. I shut off all the known processes that I'm running on that box and the memory utilization is still at 78% (with Swap... (3 Replies)
Discussion started by: dehuang83
3 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