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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:52:56+00:00 2026-06-13T11:52:56+00:00

memcpy is behaving in a strange way in my program. My function is called

  • 0

memcpy is behaving in a strange way in my program. My function is called twice, so the memcpy line is run twice, the first time it works no problem and the second time around I get a seg fault at that line (using gdb). I am confused because I don’t understand why it would work once but not twice…also, the two names I input are the same length.

This is what I have…

typedef struct _item_
{
    char name[500];
}item;


int my_function(char *name)
{

    item *myitem = malloc(sizeof(item));

    char* temp = myitem->name;

    strcpy(temp, name);

    /* code here to write add item to a global structure */

    return 0;

}

in testing code…

int i;
i = my_function("test1");
.
.
.
i = my_function("test2");

Then i changed it to strcpy and the same problem occurs

strcpy(temp, name);

Any ideas as to why this might not be working?

  • 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-13T11:52:57+00:00Added an answer on June 13, 2026 at 11:52 am

    Oh, okaaay then. Well, you need to look at your code. Specifically at what the destination pointer you give memcpy or strcpy is pointing to. Your message clearly indicates that you’re trying to write to memory you don’t own. Here’s a minimal compilable version that uses the code you supplied. It works just fine. 20,000 times the function is called and a valid result returned. This is verified when all 20,000 elements are printed out.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct _item_
    {
        char name[500];
    }item;
    
    item *my_function(char *name)
    {
        item *myItem = (item*)malloc(sizeof(item));
        strcpy(myItem->name, name);
        return myItem;
    }
    
    int main()
    {
        const int max = 10000;  // 10,000 iterations
        item *itemList[max*2];  // 2 operations per loop iteration
        int j, index = 0;
        for (j=0; j<max; j++)
        {
            itemList[index++] = my_function("test1");
            itemList[index++] = my_function("test2");
        }
    
        index = 0;
        for (j=0; j<max; j++)
        {
            printf("%d. - %s\n", 1+index, itemList[index]->name);
            index++;
            printf("%d. - %s\n", 1+index, itemList[index]->name);
            index++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a memcpy function that look strange on how it was used. I
I fall in some problem. I need to write some function like memcpy(void*, const
I recently stumbled across an article that claims Microsoft is banning the memcpy() function
Better way to do it (without QImage )?: QImage image(width, height, QImage::Format_RGB888); memcpy(image.bits(), m_frameRGB->data[0],
Possible Duplicate: C# memcpy equivalent What is the equivalent of the memcpy function in
I am writing a program and using memcpy to copy some bytes of data,
I have a function that is doing memcpy, but it's taking up an enormous
I have written a function to implement memcpy #include <stdio.h> #include <stdlib.h> #include <string.h>
I am interposing the memcpy() function in C because the target application uses it
Is there any asynchronous memcpy function in linux? I want it to work with

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.