Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 722489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:59:43+00:00 2026-05-14T05:59:43+00:00

I’m writing a C code for a class. This class requires that our code

  • 0

I’m writing a C code for a class. This class requires that our code compile and run on the school server, which is a sparc solaris machine. I’m running Linux x64.

I have this line to parse (THIS IS NOT ACTUAL CODE BUT IS INPUT TO MY PROGRAM):

while ( cond1 ){ 

I need to capture the “while” and the “cond1” into separate strings. I’ve been using strtok() to do this. In Linux, the following lines:

char *cond = NULL;
cond = (char *)malloc(sizeof(char));
memset(cond, 0, sizeof(char));
strcpy(cond, strtok(NULL, ": \t\(){")); //already got the "while" out of the line

will correctly capture the string “cond1”.Running this on the solaris machine, however, gives me the string “cone1”.

Note that in plenty of other cases within my program, strings are being copied correctly. (For instance, the “while”) was captured correctly.

Does anyone know what is going on here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:59:44+00:00Added an answer on May 14, 2026 at 5:59 am

    The line:

    cond = (char *)malloc(sizeof(char));
    

    allocates exactly one char for storage, into which you are then copying more than one – strcpy needs to put, at a bare minimum, the null terminator but, in your case, also the results of your strtok as well.

    The reason it may work on a different system is that some implementations of malloc will allocate at a certain resolution (e.g., a multiple of 16 bytes) no matter what actual value you ask for, so you may have some free space there at the end of your buffer. But what you’re attempting is still very much undefined behaviour.

    The fact that the undefined behaviour may be to work sometimes in no way abrogates your responsibility to avoid such behaviour.

    Allocate enough space for storing the results of your strtok and you should be okay.

    The safest way to do this is to dynamically allocate the space so that it’s at least as big as the string you’re passing to strtok. That way there can be no possibility of overflow (other than weird edge cases where other threads may modify the data behind your back but, if that were the case, strtok would be a very bad choice anyway).

    Something like (if instr is your original input string):

    cond = (char*)malloc(strlen(instr)+1);
    

    This guarantees that any token extracted from instr will fit within cond.

    As an aside, sizeof(char) is always 1 by definition, so you don’t need to multiply by it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For testing the correctness of code you can use unit… May 14, 2026 at 6:26 pm
  • Editorial Team
    Editorial Team added an answer The exe will install Tomcat as a Windows Service. The… May 14, 2026 at 6:26 pm
  • Editorial Team
    Editorial Team added an answer Thanks for all you help guys, but i found the… May 14, 2026 at 6:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.