L
Lawrie
Hi,
I am fairly new to C programming and would like to ask a little help
with the following problem.
I am writing a simple ring buffer, implemented as an array of char *.
char* ringbuffer[BUFFLEN];
When I receive a new message (of type char*) I need to make a copy of
the contents of the string in case anyone tampers the original char* (I
have done this by writing a safe string copy routine that takes into
account that the original string may not be null terminated and my
attempt to malloc enough space for the copy may fail).
All well and good so far. I have allocated space for my copy of the
original string and inserted it into my ring buffer at the correct
position.
ringbuffer[end] = message;
When I pop my message off of the ring buffer I do the following
char* message = ringbuffer[start];
Now, here is where I have a problem. I may not be able to process the
whole of my message. In this case I have to replace whatever remains of
the string back in the ringbuffer. I do this by shifting the message
pointer (array index) along by the number of bytes I have processed.
int processed_bytes = dosomething(message);
if (processed_bytes < strlen(message)) {
/* increment message poinmter
message = message + processed_bytes)
}
else {
/* Message processing complete, free memory */
free(message);
}
Will me call to free() free all of the memory allocated to message
during the malloc() call if I have subsequently incremented the message
pointer or will I get a memory leak?
Many thanks for any help.
Lawrie
I am fairly new to C programming and would like to ask a little help
with the following problem.
I am writing a simple ring buffer, implemented as an array of char *.
char* ringbuffer[BUFFLEN];
When I receive a new message (of type char*) I need to make a copy of
the contents of the string in case anyone tampers the original char* (I
have done this by writing a safe string copy routine that takes into
account that the original string may not be null terminated and my
attempt to malloc enough space for the copy may fail).
All well and good so far. I have allocated space for my copy of the
original string and inserted it into my ring buffer at the correct
position.
ringbuffer[end] = message;
When I pop my message off of the ring buffer I do the following
char* message = ringbuffer[start];
Now, here is where I have a problem. I may not be able to process the
whole of my message. In this case I have to replace whatever remains of
the string back in the ringbuffer. I do this by shifting the message
pointer (array index) along by the number of bytes I have processed.
int processed_bytes = dosomething(message);
if (processed_bytes < strlen(message)) {
/* increment message poinmter
message = message + processed_bytes)
}
else {
/* Message processing complete, free memory */
free(message);
}
Will me call to free() free all of the memory allocated to message
during the malloc() call if I have subsequently incremented the message
pointer or will I get a memory leak?
Many thanks for any help.
Lawrie