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 8844041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:23:14+00:00 2026-06-14T11:23:14+00:00

If I am reading a C string such as: char myData[100]; and I want

  • 0

If I am reading a C string such as:
char myData[100];
and I want to process this data and produce a copy out of it, so my code looks like:

char myData[100], processedData[50];

loop
    fill myData from file...
    setProcessedData(myData, processedData);
    store processedData to file...

where setProcessedData is a function that returns a processed string. let’s say for simplicity it returns a substring

void setProcessedData (char *myData, char *processedData) {
     memCopy( processedData, myData, 5);
}

Is what I am doing something wrong? Like creating extra objects/strings? is there a better way to do it?

Let’s say I read string from file which contains *
I am* A T*est String* But Ho*w to Process*.
I want to get substring which has the first 3 s. So my processedData
I am
A t*est String*

and I want to do this for all lines of a file as efficient as possible.

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-14T11:23:15+00:00Added an answer on June 14, 2026 at 11:23 am

    Problem is that your function is inherently unsafe, this because you make assumption about the allocated memory by parameters you pass to the function.

    If someone is going to call setProcessedData by passing a string which is shorter than 5 bytes then bad things will happen.

    In addition you are copying memory with memcpy by using a raw dimension, a safer approach, even if it is quite picky in this situation, is to use sizeof(char)*5.

    Best thing you can do, though, is to follow the same approach used by safer functions of standard library like strcpy vs strncpy: you pass a third parameter which is the maximum length that should be copied, eg:

    void processData(const char *data, char *processedData, unsigned int length) {
      memcpy(processedData,data,length*sizeof(char));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ok..so i'm reading from an sqlite db data into a string...it works..but when the
I've been reading a bit about immutable structures such as string. Simply put, they
I am reading data sent to a serial port (that is, COM3) using code
I've written several ints, char[]s and the such to a data file with BinaryWriter
I am reading string data from a PLIST which I am using to create
I am reading JSON code from the database and then parsing the string using
I have little doubt about string reading in C. string reading functions like gets,
Hi there I'm reading a string and breaking each word and sorting it into
I have a log file that I am reading to a string public static
After reading about the problem of passing empty std::string objects between DLLs and EXEs,

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.