Sign in to follow this  

New / delete confounded by strtok?

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this