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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:59:00+00:00 2026-06-10T06:59:00+00:00

The following program segfaults when v2 is printed but not during the array copy.

  • 0

The following program segfaults when v2 is printed but not during the array copy. Does anyone know why?

#include <stdio.h>
#include <stdlib.h>

void cpyarray (void* dst, void* src, size_t memsize, size_t size) {

    for (size_t i = 0; i < size; i++) {

        *(((char*) dst) + i*memsize) = *(((char*) src) + i*memsize);
    }
}

int main () {

    size_t N = 10;

    double* v1 = (double *) malloc(N * sizeof(double));
    double* v2 = (double *) malloc(N * sizeof(double));

    for (size_t i = 0; i < N; i++) *(v1+i) = i;

    printf("\n\nv1: ");
    for (size_t i = 0; i < N; i++) printf("%g ", v1[i]);

    cpyarray(&v2, &v1, sizeof(double), N);

    printf("\n\nv2: ");
    for (size_t i = 0; i < N; i++) printf("%g ", v2[i]); // program crashes here

    return 0;
}

EDIT: the code does not crash if I copy arrays of ints instead of doubles.

  • 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-10T06:59:01+00:00Added an answer on June 10, 2026 at 6:59 am

    v1, v2 are pointers to memory blocks that you want to operate on. But you’re passing their addresses to your cpyarray function.

    So you’re operating on the wrong memory blocks, stepping on memory around the v2 variable, and changing what v2 points to.

    cpyarray(v2, v1, sizeof(double), N);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following program does not work as expected: module Main where import Graphics.UI.Gtk import
Following program gives error #include<stdio.h> int main () { int a=10,b; a>=5?b=100:b=200; printf(\n%d,b); }
The following program compiles with ifort (version 12) but not with GFortran (up to
following program should print error but its printing success.why? #include<iostream> using namespace std; int
The following program has undefined behavior: #include <stdio.h> int main(void) { unsigned int x
The following simple program segfaults on my mac (Lion) running gcc 4.2.1: #include <iostream>
Why does the following program segfault? int main() { main(); } Even though it
The following program doesn't build in VS11 beta, gcc 4.5, or clang 3.1 #include
I wrote the following example program but it crashes with segfault. The problem seems
The following program used the implementation of atomic locks from 'Cuda By Example', but

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.