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

  • Home
  • SEARCH
  • 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 7608061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:49:24+00:00 2026-05-31T00:49:24+00:00

Hello there I have the following code: struct Edge { Node * nodeA, *

  • 0

Hello there I have the following code:

struct Edge
{
    Node * nodeA, * nodeB;
    int weight;
};
.
.

This is typedef’d to simply Edge elsewhere.

.
.

int cmp(const void *b, const void *a)
{
    Edge * e1 = (Edge*)a;
    Edge * e2 = (Edge*)b;
    printf(" %d++", getWeight(e1));
    printf(" %d++", getWeight(e2));
    if (getWeight(e1) != getWeight(e2))
        return (getWeight(e1) < getWeight(e2)) ? -1 : 1;
    return 0;
}

void sortEdges(Edge ** edgeList, int numEdges)
{
    qsort(edgeList, numEdges, sizeof(Edge*), cmp);
}

Valgrind tells me that I have an Invalid read of size 4, and when I output the value for the the edge weights I get totally unexpected results. I can only conclude that there is something wrong with my cast to an Edge* or that the sizeof is somehow not doing what I think it is. If you need any information please let me know.

  • 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-31T00:49:25+00:00Added an answer on May 31, 2026 at 12:49 am

    There are two ways to correct your code.
    1. if edgeList is a array of (Edge *).

    int cmp(const void *b, const void *a)
    {
        Edge ** e1 = (Edge**)a;//notice
        Edge ** e2 = (Edge**)b;//notice
        printf(" %d++", getWeight(*e1));//notice
        printf(" %d++", getWeight(*e2));//notice
        if (getWeight(*e1) != getWeight(*e2))//notice
            return (getWeight(*e1) < getWeight(*e2)) ? -1 : 1;//notice
        return 0;
    }
    
    void sortEdges(Edge ** edgeList, int numEdges)
    {
        qsort(edgeList, numEdges, sizeof(Edge*), cmp);
    }
    

    2, if edgeList is a array of Edge.

    int cmp(const void *b, const void *a)
    {
        Edge * e1 = (Edge*)a;
        Edge * e2 = (Edge*)b;
        printf(" %d++", getWeight(e1));
        printf(" %d++", getWeight(e2));
        if (getWeight(e1) != getWeight(e2))
            return (getWeight(e1) < getWeight(e2)) ? -1 : 1;
        return 0;
    }
    
    void sortEdges(Edge * edgeList, int numEdges)//notice
    {
        qsort(edgeList, numEdges, sizeof(struct Edge), cmp);//notice
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: char *array1[3] = { hello, world, there. }; struct
Hello I have following c++ code class classBase { public: int get1(){return 1;} int
I have the following code: #include <stdio.h> int main() { printf ("hello world\n"); return
I have the following code jQuery.getJSON(encodeURI(strUrl), {address:address,state:state, zip:zip},function(data){ alert('HELLO World!'); }); If for some
Let's say I have the following HTML code <div id="outer"> <div id="inner">Hello World</div> </div>
Hello i'm practising C and i have a little problem with the following code.
I have the following code: class Hello(object): def __init__(self, names): self.names = names def
I have the following code: var html = <div class='test'><b>Hello</b> <i>world!</i></div>; var results =
I have the following code: function Session(name, index) { this.Messages = []; this.Name =
This is a continuation of another question I have. Consider the following code: char

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.