strsep without modifying original buffer

Discussion in 'C Programming' started by Ram, Jan 15, 2010.

  1. Ram

    Ram Guest

    Hi,

    is it possible to implement strsep without modifying original buffer?

    for Ex:

    buf = malloc(30);
    if (buf == NULL) {
    DEBUG_PRINT("\nCould'nt allocate memory block @ %s:%d",
    __FILE__, __LINE__);
    return -1;
    }

    elem = strsep(&buf, ",");

    I don't want disturb original 'buf' in this case..

    please help me!

    Thanks in advance,
    Rammohan
    Ram, Jan 15, 2010
    #1
    1. Advertising

  2. Ram

    Ram Guest

    On Jan 15, 6:53 pm, pete <> wrote:
    > Ram wrote:
    > > Hi,

    >
    > > is it possible to implement strsep without modifying original buffer?

    >
    > > for Ex:

    >
    > >     buf = malloc(30);
    > >     if (buf == NULL) {
    > >         DEBUG_PRINT("\nCould'nt allocate memory block @ %s:%d",
    > > __FILE__, __LINE__);
    > >         return -1;
    > >     }

    >
    > >    elem = strsep(&buf, ",");

    >
    > > I don't want disturb original 'buf' in this case..

    >
    > Then why are you calling strsep?
    >
    > What is it that you do want to happen?
    >
    > --
    > pete


    Hi,

    when i try to free() the buf, my device crashes becos it's trying to
    free the memory which is not with in process heap area which this
    process allocated...

    it rarely happens, becos strsep() is disturbing initial pointer
    address.. when i try to free() it and if the modified pointer address
    out of process space.. free() will crash.. that's what happening
    exactly in my case..

    please provide any ideas to solve it..
    Thanks,
    Ram
    Ram, Jan 15, 2010
    #2
    1. Advertising

  3. Ram

    Ram Guest

    On Jan 15, 8:35 pm, pete <> wrote:
    > Ram wrote:
    > > On Jan 15, 6:53 pm, pete <> wrote:

    >
    > >>Ram wrote:

    >
    > >>>Hi,

    >
    > >>>is it possible to implement strsep without modifying original buffer?

    >
    > >>>for Ex:

    >
    > >>>    buf = malloc(30);
    > >>>    if (buf == NULL) {
    > >>>        DEBUG_PRINT("\nCould'nt allocate memory block @ %s:%d",
    > >>>__FILE__, __LINE__);
    > >>>        return -1;
    > >>>    }

    >
    > >>>   elem = strsep(&buf, ",");

    >
    > >>>I don't want disturb original 'buf' in this case..

    >
    > >>Then why are you calling strsep?

    >
    > >>What is it that you do want to happen?

    >
    > >>--
    > >>pete

    >
    > > Hi,

    >
    > > when i try to free() the buf, my device crashes becos it's trying to
    > > free the memory which is not with in process heap area which this
    > > process allocated...

    >
    > > it rarely happens, becos strsep() is disturbing initial pointer
    > > address.. when i try to free() it and if the modified pointer address
    > > out of process space.. free() will crash.. that's what happening
    > > exactly in my case..

    >
    > > please provide any ideas to solve it..
    > > Thanks,
    > > Ram

    >
    >      buf = malloc(30);
    >      p = buf;
    >      elem = strsep(&p, ",");
    >      free(buff);
    >
    > --
    > pete


    Hi Pete,

    Thanks for your response.. But sorry, the problem i am facing with
    same implementation as you posted..

    please correct me if i am wrong.. we are passing address of original
    pointer to temporary pointer.. in this cases the starting address of
    originally malloced pointer will also changes.. So we are not freeing
    original address of malloced pointer..

    once again thanks!
    Ram
    Ram, Jan 15, 2010
    #3
  4. Ram

    Seebs Guest

    On 2010-01-15, Ram <> wrote:
    > please correct me if i am wrong.. we are passing address of original
    > pointer to temporary pointer.. in this cases the starting address of
    > originally malloced pointer will also changes.. So we are not freeing
    > original address of malloced pointer..


    You are wrong. strsep() in this case will modify p, but it will not
    modify buf, because p and buf are separate pointers. You do need to save
    a copy of the original pointer to free, so if you're using strsep, use
    a temporary pointer.

    If you don't want to walk through the pointer, you probably shouldn't be
    using strsep(). You might be able to use strtok() in this case (or strtok_r()
    on a system where that exists and matters). Or strchr().

    -s
    p.s.: I believe strsep() is an extension common to BSD and Linux systems,
    but didn't make it into the standard. One of the few things I think C99 got
    wrong.
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Jan 15, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    12
    Views:
    24,328
    John Harrison
    Jun 21, 2004
  2. Victor Bazarov
    Replies:
    0
    Views:
    1,019
    Victor Bazarov
    Jun 14, 2005
  3. Alex Vinokur
    Replies:
    0
    Views:
    497
    Alex Vinokur
    Jun 15, 2005
  4. Replies:
    9
    Views:
    740
  5. Keith Thompson

    strtok and strsep

    Keith Thompson, Nov 4, 2011, in forum: C Programming
    Replies:
    6
    Views:
    1,160
    Seebs
    Nov 4, 2011
Loading...

Share This Page