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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:29:32+00:00 2026-06-02T01:29:32+00:00

I am creating a linked list for a class project that stores some stock

  • 0

I am creating a linked list for a class project that stores some stock market data. I was trying to store some data onto the stack instead of mallocing to the heap. I am trying to do this using memcpy. My code is like this:

struct trade{

int a,b;
float c;
struct trade *n;
};
char stack[100];
int i = 0;

void newNode(struct trade **head, int a, int b, float c){
 struct trade *node;
 if(i<99){
  memcpy(&a,&stack[i],4);
  i = i + 4;
  node = (struct lnode*) malloc(16);
 }

  else 
   node = (struct lnode*) malloc(20);
}
.....
.....
}

My newnode function is called whenever I create a new node and I need to malloc space for it.

I copy the int into the stack array if there is still space in the stack array else I malloc into the heap. I use 20 and 16 because if I am storing the int in the stack then I need to malloc space for the remaining 16 bytes in my struct else I malloc space for 20 bytes.

For some reason I get a segfault when I do this. I would appreciate it if someone could point me in the right direction.

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-02T01:29:34+00:00Added an answer on June 2, 2026 at 1:29 am
    1. You’ve got your memcpy arguments swapped. The destination should be the first argument:

      memcpy(&stack[i],&a,4);
      

      From the manpage:

      SYNOPSIS
      
      void * memcpy(void *restrict s1, const void *restrict s2, size_t n);
      
      DESCRIPTION
      
      The memcpy() function copies n bytes from memory area s2 to memory area
      s1.  If s1 and s2 overlap, behavior is undefined.  Applications in which
      s1 and s2 might overlap should use memmove(3) instead.
      
    2. If you’re compiling for anything but 32-bit x86, integers and pointers will not be 4 bytes, but e.g. 8 for 64-bit, which would cause problems. You should really use sizeof(int). This will also affect the 16 and 20, which you can probably replace with sizeof(lnode). These are set to the correct values at compile time, so won’t affect speed.

    3. Besides the issue with defining “stack” versus heap: why do you define your stack as a char array rather than int if you’re putting ints in there? It’s possible to use a char array, but it’s a lot easier and less error-prone to just assign rather than memcpy to an array of the same type.

    4. Valgrind is your friend for this kind of debugging. I find myself using it as a standard debugging tool for segfaults and memory leaks.

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

Sidebar

Related Questions

I am creating an Abstract Data Type, which create a doubly linked list (not
I am creating a student list (linked list) that can add, view and edit
I am looking to use the C# Linked list class instead of creating my
I'm creating a doubly-linked list in Objective-C. I would like my node class to
I'm in the process of creating a double-linked list, and have overloaded the operator=
I'm dabbling with creating a PHP extension for a personal project. Beyond what's linked
I wrote a linked list implementation for my java class earlier this year. This
I'm currently creating a circular doubly-linked list as exercise. The exercise is templating the
I am trying to implement a linked-list in C++. Currently, I have the following
I have a Core Data store which contains a number of MediaItem entities that

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.