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

  • Home
  • SEARCH
  • 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 8132103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:15:53+00:00 2026-06-06T09:15:53+00:00

I have the following piece of code compiling under gcc: int parseMsg(const char *msg_to_parse,

  • 0

I have the following piece of code compiling under gcc:

int parseMsg(const char *msg_to_parse, unsigned long *exp_input, unsigned long *sysTicks )
{
   int l_msg_size = strlen(msg_to_parse);
   if(l_msg_size <10)
          return -1;
    char l_exp_input_arr[10];
    char l_sys_ticks_arr[10];
    memcpy(l_sys_ticks_arr,msg_to_parse+12,10);

    memcpy(l_exp_input_arr,msg_to_parse,10);
   //l_msg_size = strlen(msg_to_parse);
    *sysTicks = strtoul(l_sys_ticks_arr,NULL,10);

   *exp_input = strtoul(l_exp_input_arr,NULL,10);



   return 0;
}

And I’m trying to test it in the following manner:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int parseMsg(const char *msg_to_parse, unsigned long *exp_input, unsigned long *sysTicks );

int main(void) {
char msg[] = "1234567890  59876543213";
unsigned long along1, along2;
along1 =0;
along2=0;
parseMsg(msg,&along1, &along2 );
printf("result of parsing: \n \t Along 1 is %lu \n \t Along 2 is %lu \n",along1, along2);
return 0;
}

But, I’m getting the following result:

result of parsing:
Along 1 is 1234567890
Along 2 is 4294967295

Why is the second unsigned long wrong?

Thanks

  • 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-06-06T09:15:54+00:00Added an answer on June 6, 2026 at 9:15 am

    The second integer you provide is too big to be represented in memory on your architecture. So, according to its API, strtoul is just returning you ULONG_MAX (=4294967295 on your architecture), along with setting errno to ERANGE

    strtoul API is here : http://www.cplusplus.com/reference/clibrary/cstdlib/strtoul/

    BUT it may also fail if you gave a smaller integer, because strtoul only stops parsing when it encounters a non-numerical character. Since you didn’t ensure that, you cannot be sure that strtoul will not try to parse whatever is in memory just after your strings. (So assuming random, you have 10 chance out of 256 to have a conversion error)

    Terminate your strings with \0, it will be ok then :

    char l_exp_input_arr[11]; // +1 for \0
    char l_sys_ticks_arr[11];
    
    memcpy(l_sys_ticks_arr, msg_to_parse+12, 10);
    l_sys_ticks_arr[10] = '\0';
    
    memcpy(l_exp_input_arr, msg_to_parse, 10);
    l_exp_input_arr[10] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have the following piece of code in C++. int arr[] = {1, 2,
I have following piece of code: class Test{ private: int id; public: Test(int v):id(v)
I have following piece of code: customObject* object; std::list<customObject> objects; for(int i = 0;
I have the following piece of code: #include <stdio.h> #include <stdlib.h> int main(int argc,
I have following piece of code: typedef uint8_t array_t[8]; static array_t _my_array; static const
I have following piece of code : public Hashmap<String,String> tempmap = new HashMap<String,String>(); and
I have the following piece of code taken from the PHP manual on the
I have the following piece of code: // setup the AJAX request var pageRequest
I have the following piece of code from a book. There is this function

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.