Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

circle buffer with malloc and free?

Status
Not open for further replies.

phoenixusa

Technical User
Jul 10, 2005
1
US
Hi there!

I am trying to get this puzzle out of this program for days now. Everytime I run the program, it reads the input file which has 21 lines. The lines are stored in a buffer of size 10. At the 11th line, it goes back to buffer [0], starting over, a circular buffer. However, at line 16 the program crashes and a core dumped is created. So any suggestion would be appreciated. Here is the code so far:

#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
#define MAX_BUFFER 10

char *ptr_array[MAX_BUFFER];
void insert_line(char *);
int getline(char s[], int lim);
void print_lines(char** ptr);


main (int argc, char *argv[ ])
{
int c, number = 0;

int i, optn;
char * ptr;
char line[MAXLINE];
char * temp_ptr_array[MAX_BUFFER];

/* code to enter data */

while(getline(line, MAXLINE)){
ptr = (char *) malloc(strlen(line +1));
printf("%p\n", ptr);
strcpy(ptr, line);
insert_line(ptr);
}
/* print customer data */
for (i = 0; i < MAX_BUFFER; i++)
temp_ptr_array = ptr_array;

print_lines(temp_ptr_array);

}
/* function to update the buffer with new customer data */
void insert_line(char * y)
{
static int new_index;

if(ptr_array[new_index])
{
free(ptr_array[new_index]);
ptr_array[new_index] = NULL;
}
ptr_array[new_index] = y;
new_index ++;
printf("%d\n", new_index);
if (new_index >= MAX_BUFFER)

new_index = 0;
}

/* function to print data */
void print_lines(char **ptr)
{
int i,j;
char * temp;
for (i = 0; i < MAX_BUFFER && ptr != NULL ; i++)
printf("%s", ptr);
}

/* function to get a line */
int getline(char s[], int lim)
{
int c, i;
i = 0;
while(--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s = '\0';
return i;
}
 
What exactly is the purpose of the program? I'm no guru but I'm certain that that would allow us to help you better.
 
This is a sure line of distress.
Code:
ptr = (char *) malloc(strlen(line +1));
 
the lines below is the output I got from borland
5.0 executable.

I did not get any crash and I carried it through the
loop 20 or 30 times. the only thing I had to modify
was add

#include <stdlib.h>

for the malloc


skla;sdjfklasdf
11A1:0004
1
asdfasdf
11A3:0004
2
asdfasdf
11A4:0004
3
asdff
11A5:0004
4
asdf
11A6:0004
5
asdfasdfasdfasfasdf
11A7:0004
6
asdfasfasdf
11A9:0004
7
asdfasdf
11AA:0004
8
 
cant find no output file.

what the f*#!.

Code:
while(--lim > 0 && (c=getchar()) != EOF && c != '\n')

it compiles but I cant make headds or tails of it.

I kinda sorta got it but I think you are trying to do
to much on one line of code. this line could use some
"( )" to make it more explicit.

how are you breaking out of this
Code:
while(getline(line, MAXLINE)){
        ptr = (char *) malloc(strlen(line +1));
        printf("%p\n", ptr);
        printf("%p\n", ptr);
        strcpy(ptr, line);
        insert_line(ptr);
 }

to get to the rest of the code.

tomcruz.net
 
Correct
Code:
ptr = (char *) malloc(strlen(line +1));
to
Code:
ptr = (char *) malloc(strlen(line)+1);
You allocate too narrow slots for your strings then overwrite allocated blocks (then memory crash raised etc)...

Don't invent the wheel, your getline() is exactly:
Code:
fgets(line,size,stdin)

Your print_lines() does not know anything about ring nature of the buffer, alas...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top