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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:47:10+00:00 2026-06-14T19:47:10+00:00

I am beginner at UNIX C development and need little bit of help. I

  • 0

I am beginner at UNIX C development and need little bit of help. I have these two functions:

void edit_char(){

     int i;
     int length = strlen(expression);
     char *tmp = (char *) malloc((length+1)*sizeof(char));
     pom[0]='*';
     for(i=1;i<length+1;i++){
         tmp[i] = expression[i-1];
     }
     strcpy(expression,tmp);
     free((void *) tmp);
 }



 char *edit_char2(char *string){

     int i;
     int length = strlen(string);
     char *tmp = (char *) malloc((length+1)*sizeof(char));
     tmp[0]='/';
     for(i=1;i<length+1;i++){
         tmp[i] = string[i-1];
     }
     strcpy(string,tmp);
     free((void *) tmp);
     return string;
  }

edit_char() edits global variable char *expression – it puts a symbol “*” at the beginning. Second edit_char2() do almost the same, but instead of editing global variable, it edits string from argument.

First function works fine, the problem is with the tmp variable of second function. Malloc doesn’t return empty char array with size of (length+1). It returns “xd\372\267xd\372\267\020”.

What could cause this?

  • 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-14T19:47:11+00:00Added an answer on June 14, 2026 at 7:47 pm

    malloc just returns a pointer to the newly allocated memory, which is not to be considered “empty”. If you want to “empty” it – i.e, fill your memoy with NULLs, you need to do it manually need to use calloc or fill it manually, for instance with:

    • bzero (deprecated)
    • memset

    EDIT: Also I think that since you are adding a character to each string, you shouldn’t use

    newstring = malloc((strlen(string) + 1) * sizeof(char))
    

    but instead

    newstring = malloc((strlen(string) + 2) * sizeof(char))
    

    To allocate space both for the new character AND the terminating \0 into account.

    EDIT2: Which also means that your functions can’t work / are not safe ! You are trying to make both string and expression contain 1 more character than they were probably initially allocated for !

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

Sidebar

Related Questions

Am a beginner in unix and I have just landed into a snag. The
Beginner in Android development. My code crashes. I have made a simple Java method
JS beginner here. I need help with a script to place different content in
Beginner question here, any help much appreciated. I have a table of users and
I am a beginner in UNIX. I am finding some difficulty in input/output redirection.
Beginner iOS dev here. I have a problem in my array. Im making an
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner help needed :) I am doign an example form a php book which
Beginner question: I have a dictionary where the values are lists of (a variable
Python beginner here, I have a list of lists and want to refer to

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.