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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:13:09+00:00 2026-06-11T04:13:09+00:00

I tried the following to reallocate a 2D float array whose size chages from

  • 0

I tried the following to reallocate a 2D float array whose size chages from 2X2 to 3X3. The code throws a segfault while trying to realloc memory for weights[2].

num_vertices = 2;
float **weights = malloc(num_vertices*sizeof(float *));      // weight array matrix
for(i = 0; i < num_vertices; i++){
    weights[i] = malloc(num_vertices*sizeof(float));
}

num_vertices = 3;
weights = realloc(weights, num_vertices*sizeof(float *));      // weight array matrix
for(i = 0; i < num_vertices; i++){       
    weights[i] = realloc(weights[i], num_vertices*sizeof(float));
}

Of course, I can free the 2D array and malloc again, but I was looking for a more elegant solution. Any ideas?

  • 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-11T04:13:10+00:00Added an answer on June 11, 2026 at 4:13 am

    The problem is that weights[2] contains garbage after you realloc weights.

    You probably want to do something like this:

    new_vertices = 3;
    weights = realloc(weights, new_vertices*sizeof(float *));
    for(i = 0; i < new_vertices; i++)
    {
        if (i >= num_vertices)
            weights[i] = NULL;
        weights[i] = realloc(weights[i], new_vertices*sizeof(float));
    }
    num_vertices = new_vertices;
    

    Note that you have a potential memory leak if realloc ever fails. Since you have no error checking yet though this probably doesn’t matter for now.

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

Sidebar

Related Questions

I tried following some of the code from here and here , but I
I am a newbie to PHP development. I tried following code sample from PHP5
I tried following code but I couldn't find success messages from it.Here commandText can
I tried following code : #include<iostream> #include<string> using namespace std; string f1(string s) {
I tried following code: <script type=text/javascript> $(document).ready(function(){ $(image).hover( function () { $(this).append($(<span> ***</span>)); },
I have tried following code, but it only positions dialogs left upper corner position
I tried following code to run timer in class library but timer_Elapsed wasn't fired.
I tried following code but it gave null reference error for second line. I
I have tried following method to get subject section abstractPolicy.getTarget().getSubjectsSection() but from that it
I tried some ways to get the page source code of the following website

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.