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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:51:01+00:00 2026-05-23T09:51:01+00:00

What is the issue with this code (It is printing arr[0] correctly but I

  • 0

What is the issue with this code (It is printing arr[0] correctly but I am getting issues with arr[1]… printing some weird characters):

using namespace std;
char ** setName() {
    char * arr[2];
    for (int i=0;i<2;i++)
       arr[i] = (char*)malloc(100);
    arr[0] = strdup("Robert");
    arr[1] = strdup("Jose");
    return arr;
}
int main()
{
    char **arr;
    arr = setName();
    printf("First name is %s\n", arr[0]);
    printf("Second name is %s\n", arr[1]);
    return 0;
}

If it matters, i am running this code in Windows using Visual Studio 8.

  • 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-23T09:51:02+00:00Added an answer on May 23, 2026 at 9:51 am

    You have two problems in that code:

    1. An auto array such as your char * arr[2] is not automatically created with new[]; its storage goes away when the function returns. This is the source of your garbage. You should malloc() or new[] it.

      char **arr = malloc(2 * sizeof (*char));
      
    2. strdup() does a malloc(), so you are pointlessly malloc()ing storage that is then lost because you overwrite the pointer.

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

Sidebar

Related Questions

This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
So basically this code was working fine before. I had some computer issues and
What is the issue with this code? Here we have two files: classA.h and
I've an issue with this piece of code: NSURL *url = [[NSURL alloc] initWithString:@http://authenticate.radonsystems.net/products.xml];
This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
I spent about 4 hours yesterday trying to fix this issue in my code.
I am having an issue converting type. I was trying code like this (minimal,
This is more of a licencing issue than a code question. I really like
This is a simplified partial version of my code to demonstrate the issue: function
(EDIT: This question is now outdated for my particular issue, as Google Code supports

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.