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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:25:51+00:00 2026-06-08T10:25:51+00:00

Code: char* data = NULL; data = new char[lengthOfParam]; //lengthOfParam = 3 //after allocation

  • 0

Code:

char* data = NULL;
data = new char[lengthOfParam];   //lengthOfParam = 3
                               //after allocation **data = ¥¥¥¥Ü\r**
memcpy(data,&buffer[offset],lengthOfParam);   //**data = pki¥Ü\r**

Why i am getting that junk values??? How to avoid or remove those extra values bcs if i try to assign that value to any other array
ex:

obj[1] = data;

then the whole value with junk’ll be assigned to that variable.

  • 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-08T10:25:54+00:00Added an answer on June 8, 2026 at 10:25 am

    Strings in C need to be NUL terminated. This means you need to add a zero value byte to the end of the string to indicate the end of the string. Because you have no indication of the end of the string when you view/print the value you are reading on past the end of your array into whatever memory is after it.

    If the source data contains a NUL terminator you can simply allocate and copy 1 more byte, but assuming it is a fixed length field with no NUL termination you will need to manually add one:

    data = new char[lengthOfParam+1];
    
    memcpy(data, &buffer[offset], lengthOfParam);
    data[lengthOfParam] = 0;
    

    Also further more looking at this line you posted:

    obj[1] = data;
    

    I maybe wrong here and sorry if I am but I strongly suspect this line is not doing what you think it is. This will store a pointer to your string in obj[1] not copy the data from your string. Hence if you delete data, obj[1] would no longer be valid either.

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

Sidebar

Related Questions

I have the following code: glShaderSource(shader, 1, (const char **)data.c_str(), NULL); But it makes
How to validate data in RPG code like if a field is of char
I have the following code: tmp_data = simulated_data[index_data]; unsigned char *dem_content_buff; dem_content_buff = new
Consider this code: char buffer[] = abcdefghijklmnopqrstuvwxyz, *val = malloc(10), *pbuf = buffer, *pval
I got the following code: char buffer[2047]; int charsRead; do { if(fscanf(file, %2047[^\n]%n%*c, buffer,
I am working on some code that reads in a data file. The file
I'm getting parse errors in my code. I'm probably missing something silly.. but after
I have a combobox that I don't want users adding new data too, but
Here's my data table in my SQL DB, and below that is the code
In code: struct Rep { const char* my_data_; Rep* my_left_; Rep* my_right_; Rep(const char*);

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.