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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:07:45+00:00 2026-05-28T11:07:45+00:00

for(k=i; k<MAXRECORDS; k++) { if(slist->servers_ptr[k+1] != NULL) { slist->servers_ptr[k] = slist->servers_ptr[k+1]; } else slist->servers_ptr[k]

  • 0
for(k=i; k<MAXRECORDS; k++) {
    if(slist->servers_ptr[k+1] != NULL) {
        slist->servers_ptr[k] = slist->servers_ptr[k+1];
    } else slist->servers_ptr[k] = NULL;
}

When I run valgrind, I get an error of invalid size 8.

I assume this has something to do with a border case in my for loop, but I don’t understand logically how it’s happening.

EDIT: It was pointed out that on the last round of the for loop, accessing servers_ptr[k+1] is outside of the array, causing valgrind errors. I have since updated my code to:

for(k=i; k<MAXRECORDS-1; k++) {
        if(slist->servers_ptr[k+1] != NULL) {
                slist->servers_ptr[k] = slist->servers_ptr[k+1];
                if(k==MAXRECORDS-2)slist->servers_ptr[k+1] = NULL;
        } else slist->servers_ptr[k] = NULL;
}

I still get the errors in valgrind. Did I update it incorrectly?

  • 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-28T11:07:45+00:00Added an answer on May 28, 2026 at 11:07 am

    It’s almost certainly because you’re going beyond the end of the array. The maximum value of k is MAXRECORDS-1 and you’re using k+1 in your expressions.

    That means you’ll be accessing array[MAXRECORDS] where the index should be limited to between 0 and MAXRECORDS - 1 inclusive.

    It’s difficult to see what you’re trying to do without more context but the fix may be as simple as using k < MAXRECORDS - 1 as the for loop continuing condition (the bit in the middle):

    for (k = i; k < MAXRECORDS - 1; k++) {
    

    The other possibility is an invalid value of i, like -1 for example, which would cause the problem at the other end of the array. This is probably less likely since I’m assuming you’re deleting element i by shifting all the other elements down (as in: i will be set to a valid index).


    That’s not a memory leak by the way, simply a memory corruption. Memory leaks are when you allocate memory and then lose the pointers to them so that they can never be freed, something like:

    char *x;
    for (i = 0; i < 10; i++)
        x = malloc (64);
    

    where only the last allocation is accessible.


    By the way, if a shuffling deletion is what you’re after, it would be better done (in my opinion) as:

    // For every element where there's a non-NULL next element,
    // shift that element down. Then force the last element to
    // be NULL (it will have been shifted down already).  
    
    for (k = i; (k < MAXRECORDS - 1) && (slist->servers_ptr[k+1] != NULL); k++)
        slist->servers_ptr[k] = slist->servers_ptr[k+1];
    slist->servers_ptr[k] = NULL;
    

    The extra condition stops where the next element is NULL and places NULL into that position. That should work fine and has the advantage of being less complex.

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

Sidebar

Related Questions

I'm not sure if this is a VB.NET error or SQL Server. But I
I found this in an error log and am trying to work out how
I get the following error when trying to use the SelectCommand of an SqlDataSource
I am using MS SQL Server 2008, and I get the following error. How
I am doing an MxRecordLookup. I am getting an error when calling the DnsRecordListFree
In PhpMyAdmin I use this query: SELECT * FROM myrecords where TITLE LIKE %\\\%
I am doing an MxRecordLookup. I am getting an error when calling the DnsRecordListFree
This might be a silly question? but are you able to perform a SQL
I've seen examples like this: public class MaxSeconds { public static final int MAX_SECONDS
I have a web service that looks like this [WebMethod] public int Import(System.Collections.Generic.List<Record> importRecords)

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.