Thread: flushing/wiping a heap-based buffer

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    4

    flushing/wiping a heap-based buffer

    What is the most efficient way of doing this?

    My first thought was to do:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
      
      char *mybuf=(char *) malloc(1000);
      
      mybuf="scribble here";
      memset(mybuf, '\0', strlen(mybuf));
      
      return 0;
    }
    But memset only takes a stack-based buffer/array, apparently.


    Is it even possible to change the individual chars of a heap-based string?

    I notice that while you can assign *string a new value. Like:

    Code:
      char *mybuf=(char *) malloc(1000);
      mybuf="scribble here";
      printf("%s\n\n", mybuf);
      mybuf="write some more stuff";
      printf("%s", mybuf);
    you can't do something like:

    Code:
      char *mybuf=(char *) malloc(1000);
      
      mybuf="scribble here";
      int i;
      
      for (i=0; i < strlen(mybuf); ++i){
          mybuf[i]='a';
      }
      printf("%s", mybuf);
    Why?

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by what_the
    What is the most efficient way of doing this?

    My first thought was to do:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
      
      char *mybuf=(char *) malloc(1000);
      
      mybuf="scribble here";
    Here's your problem. Well, the first problem I noticed. You assigned mybuf so that it contains the memory address of the beginning of some block of memory allocated with malloc. Then you reassigned mybuf so that it contains the memory address of some array of characters located elsewhere.

    You meant to use the line
    Code:
    strcpy(mybuf, "scribble here");
    Remember that mybuf isn't a buffer. It's the memory address of a buffer. And "scribble here" isn't an array of characters. It's the memory address of an array of characters.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    See the FAQ regarding casting malloc().

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    Thanks for those tips. Taking them into account, I've kept trying to figure out the original problem. My main question was how to flush out a *string with (presumably) nulls. To do this I'd need to get at the individual chars in the *string, correct?

    I've been trying out stuff based around..:


    Code:
      int i;
    
      for (i=0; *mybuf != '\0'; ++mybuf){
    
         // printf("%d", &(*mybuf));
         // shows  &(*mybuf) as the address of the individual char
         // so utilize it here somehow 
          
          
      }
    Now, I've tried utilizing the address of the individual char (in the for loop) with memset(), strcopy(), and memcpy().. all of which crash.

    Any other ideas/methods out there?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you only want to zero out the memory after it's allocated, you can use calloc. For multiple resets, you can use memset:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( void )
    {
      char *mybuf = malloc ( 1000 );
      
      memset ( mybuf, '\0', 1000 );
      
      return 0;
    }
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    That works, thanks. That was actually my first plan, but I thought memset() wouldn't take *string, only string[].

    The reason I thought this was because the following would crash:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char *argv[])
    {
    
      char *blah="some stuff";
      memset(blah, 'x', strlen(blah));
      
      printf("%s", blah);
      
      return 0;
    }
    while its counterpart using an array instead of *string would work.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > char *blah="some stuff";
    > memset(blah, 'x', strlen(blah));
    That's because "strings like this" are (in your case at least) in read-only memory, so naturally, it blows up when you try and modify them.

    Code:
    char a[10];
    char *pa = a;
    memset ( pa, 0, 10 );
    Nothing wrong with pointers, you've just got to be more careful where you point them
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    Hmm, okay

    Code:
    int main(int argc, char *argv[])
    {
      
      char *mybuf=(char *) malloc(1000);
      strcpy(mybuf, "random data here");
      printf("%s  \n\n", mybuf);
      memset(mybuf, 'x', strlen(mybuf));
      printf("new buffer: %s", mybuf);
      
      return 0;
    }
    works. So what you're saying, Salem, is that putting data in a *string "like this" and by using strcpy() result in different memory permissions?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but I thought memset() wouldn't take *string, only string[]
    There's no difference between the two in this case.

    >The reason I thought this was because the following would crash
    That's because you're trying to modify a string literal, which isn't guaranteed to work, and very likely won't.

    >while its counterpart using an array instead of *string would work
    That's because a pointer to a string literal and an array initialized with a string literal are two different things.
    My best code is written with the delete key.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > char *mybuf=(char *) malloc(1000);
    In C, we write
    char *mybuf=malloc(1000);

    Read the FAQ like kermit said yesterday.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and heap objects
    By John_L in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 10:20 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM