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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:24:46+00:00 2026-05-27T18:24:46+00:00

This is my first time posting a question to the forum, by the way.

  • 0

This is my first time posting a question to the forum, by the way.
I’m having problems implementing an algorithm, and I’ve narrowed it down to the following lines of code:

int Jacobi( double** A, double* b, int n, double* x0,
double tol, int maxInt ) {
    int i = 0;
    int j = 0;
    int done = 0;
    int loopCount = 0;

    /*previous x variable*/
    double* xPrev = 0;

    /*update information*/
    double** T = 0;
    double* c = 0;

    /*initialize x previous to a very large value*/
--->xPrev = ( double* )malloc( sizeof( double ) * n );
    for( i = 0; i < n; i++ ) {
            xPrev[ i ] = 5000.0;
    }
    ...
}

By inspection via gdb, I have found that the line with the arrow pointing to it is the one that is causing the trouble. Before that line is executed, x0[ 1 ] = 1. Afterwards, it is somehow changed to x0[ 1 ] = (an extremely small number that I think is the minimum double precision value). I can’t figure out why this is happening, or how it is possible. Does anyone have any insight?

Here is the gdb run to prove it:

(gdb) break 88
Breakpoint 1 at 0x804881f: file linsys.c, line 88.
(gdb) run
Starting program: /home/stu1/s11/gaw9451/Courses/AP/hw4/linsys_test

Breakpoint 1, Jacobi (A=0x804b008, b=0x804b048, n=2, x0=0x804b060,
tol=9.9999999999999998e-13, maxInt=8) at linsys.c:88
88              xPrev = ( double* )malloc( sizeof( double ) * n );
(gdb) display x0[ 1 ]
1: x0[ 1 ] = 1
(gdb) next
89              for( i = 0; i < n; i++ ) {
1: x0[ 1 ] = 5.3049894774131808e-313

On a possibly related note, I get an error at run time when I free the variable xPrev at the end of the function. I had to comment it out to see any output from my program.

Summary: Does anyone have any idea how malloc can edit data in a completely different variable field?

Thanks in advance,
phoenixheart6

  • 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-27T18:24:47+00:00Added an answer on May 27, 2026 at 6:24 pm

    I am pretty sure you messed up a previous malloc, something like allocating less than needed and now malloc overwrites what never belonged to you.

    Picture the malloc memory like this.

       +-----------------------------------------------------+
       |xxxxxxxxxx|!!!!!!!|??????????????????????????????????|
       +-----------------------------------------------------+
    
    • The X region represents what you asked from malloc
    • The ! region represents what you wrote past the legal size
    • The ? region represents unused memory

    Now when you do a second malloc, it will feel perfectly entitled to give away “your” ! part.

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

Sidebar

Related Questions

this is my first time posting here, I have a question which I have
This is the first time I am posting a question on stackoverflow, so please
First time posting, please let me know if this question has already been answered!
this is my first time posting a question here - I've searched for ones
use this website a lot but first time posting. My program creates a number
This is my first time posting to stackoverflow, but these threads have helped me
this is my first time posting on here and have read alot of helpful
First time posting a question on StackOverflow, so please go easy on me :)
First time posting here after having great results searching for other answers on stackoverflow.
this is the second time I am posting this on stack overflow forum, the

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.