Thread: problem using 'memset'

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    problem using 'memset'

    Given the following code:

    Code:
       /* result */
       char result_ans[8];
       memset(&result_ans, ' ', (strlen(result_ans) - 1));
       result_ans[strlen(result_ans) - 1] = '\0';
    
       /* store */
       char store_ans[11];
       memset(&store_ans, ' ', (strlen(store_ans) - 1));
       store_ans[strlen(store_ans) - 1] = '\0';
    
       /* promptId */
       char promptId_ans[10];
       memset(&promptId_ans, ' ', (strlen(promptId_ans) - 1));
       promptId_ans[strlen(promptId_ans) - 1] = '\0';
    I'm getting a seg fault on the last memset (for promptId). I've isolated it to this last one by commenting out until it worked. I've also used just 'promptId' in the memset function instead of '&promptId' - but to no avail.

    Anyone see a problem?

    Thanks,
    Ezra

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You're using strlen on uninitialized stings. Did you mean to use sizeof in this instance?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char result_ans[8];
       char store_ans[11];
       char promptId_ans[10];
    
       memset(result_ans, ' ', sizeof result_ans - 1);
       result_ans[sizeof result_ans - 1] = '\0';
       printf("result_ans   = \"%s\"\n", result_ans);
    
       memset(store_ans, ' ', sizeof store_ans - 1);
       store_ans[sizeof store_ans - 1] = '\0';
       printf("store_ans    = \"%s\"\n", store_ans);
    
       memset(promptId_ans, ' ', sizeof promptId_ans - 1);
       promptId_ans[sizeof promptId_ans - 1] = '\0';
       printf("promptId_ans = \"%s\"\n", promptId_ans);
    
       return 0;
    }
    
    /* my output
    result_ans   = "       "
    store_ans    = "          "
    promptId_ans = "         "
    */
    Last edited by Dave_Sinkula; 03-17-2005 at 11:29 AM. Reason: [1] Added code. [2] Added color.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Thaaaat got it. Thanks for the quick response!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM