New / delete confounded by strtok?

Started by
7 comments, last by Evil Steve 19 years, 8 months ago
I assume that this is my lack of understanding here,as I seldom use strtok. At the start of my function I have the following declaration char* data = new char[bytesToRead]; char* buffer = new char[bytesToRead]; The data is then read from a file into data, and it is a number of fields seperated by %, and is split up using code like below. buffer = strtok(data,"%"); However, when I call delete[] data; delete[] buffer; I get a memory leak the size of bytesToRead, and it is indicating that the offending char* is buffer. Why does this happen, and how do I stop it? Cheers Bp
Advertisement
Ok, I have fixed this after a fashion, but Im sure theres a more elegant (and likely obvious way)

strtok changes the address of buffer to point to the next data element between 2 %'s, so all I needed to do was to store the buffers start address, and reset buffer to that before calling delete[]

is there a more elegant way of solving this?

Bp
Just keep the original buffer address around, or don't use strtok().
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
char* data = new char[bytesToRead];
char* buffer = 0;

buffer = strtok(data,"%");

delete[] data;


I didn't know this function. Just heard about it. It sure is shit.
It may be used like that:

char *data = new char[bytesToRead];     *token = data;     *dren = data;// you loading code filling data;while ( token ){  token = strtok( dren, "%" );  dren = 0;  // your use of null-terminated string 'token'}delete[] data;


Hope that'll help.
You may also parse 'data' yourself, instead of using this shitty function.
- AdMiRaLP.S. sorry, I'm french, so i may not always speak english perfectly.
strtok is sweet. Ah, memories of parsing command line arguments in unix. Probably should use something in the C++ stream library now, but before C++ I loved this function.
From the UNIX man pages:
Quote:
NAME
strtok - extract token from string

SYNOPSIS
#include <string.h>

char *strtok(char *s, const char *delim);

DESCRIPTION
A `token' is a nonempty string of characters not occurring
in the string delim, followed by \0 or by a character
occurring in delim.

The strtok() function can be used to parse the string s
into tokens. The first call to strtok() should have s as
its first argument. Subsequent calls should have the first
argument set to NULL. Each call returns a pointer to the
next token, or NULL when no more tokens are found.

If a token ends with a delimiter, this delimiting charac-
ter is overwritten with a \0 and a pointer to the next
character is saved for the next call to strtok. The
delimiter string delim may be different for each call.

BUGS

Never use this function. This function modifies its first
argument. The identity of the delimiting character is
lost. This function cannot be used on constant strings.

RETURN VALUE
The strtok() function returns a pointer to the next token,
or NULL if there are no more tokens.


"Never use this function" - I'd say it was pretty clear [wink]
One "man"'s opinion (heh heh). The function says right up front it's non-const, so you know it's gonna trash your data. It's kinda like the slutty girl (or guy) you're with for a while because you need something purely physical. Sure you'd never take her home to meet your parents, and it ended disasterously, but deep down you have to admit to yourself that the sex was great.

Maybe that's just me.
Quote:Original post by Stoffel
One "man"'s opinion (heh heh). The function says right up front it's non-const, so you know it's gonna trash your data. It's kinda like the slutty girl (or guy) you're with for a while because you need something purely physical. Sure you'd never take her home to meet your parents, and it ended disasterously, but deep down you have to admit to yourself that the sex was great.

Maybe that's just me.

ROFL!
Rating++ for that comment [grin]

This topic is closed to new replies.

Advertisement