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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:17:55+00:00 2026-06-09T11:17:55+00:00

typedef struct { void *elems;//address of the memory block int elemSize; // int logicLen;//number

  • 0
   typedef struct {
      void *elems;//address of the memory block
      int elemSize; //
      int logicLen;//number of existing elements in vector
      int allocLen;//allocated space for the vector
  } vector;

  static void InsertNumbers(vector *numbers, long n, long d)
  {
    long k;
    long residue;

    for (k = 0; k < d; k++) {
      residue = (long) (((long long)k * (long long) n) % d);
      VectorAppend(numbers, &residue);
    }
  }



void VectorAppend(vector *v, const void *elemAddr)
{
   void *target=(char*)v->elems + (v->logicLen * v->elemSize);

   if(v->logicLen==v->allocLen){
    v->allocLen*=2;
    v->elems=realloc(v->elems,v->allocLen*v->elemSize);
    assert(v->elems!=NULL);
   }
   memcpy(target,elemAddr,v->elemSize);
   v->logicLen++;
}

Then, I use the following sentence to call InsertNumbers()

vector aVector;
VectorNew(&aVector, sizeof(long),4);
long first=139269,second=3021377;
InsertNumbers(&aVector,first , second);

It seems like 3021377 is too big…
in v->elems=realloc(v->elems,v->allocLen*v->elemSize); I find that when v->allocLen=4096, the program crashes and says:This may be due to a corruption of the heap
why?

  • 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-09T11:17:57+00:00Added an answer on June 9, 2026 at 11:17 am

    This is not a problem with the code you posted, this is a problem somewhere else.

    What happens is your program corrupts the heap, and then realloc detects that the heap is corrupted.

    You will want to detect the corruption as follows:

    1. Make sure you enable debugging symbols

    2. Run your program through Valgrind

    Edit: There is a serious error is in the code you added.

    void VectorAppend(vector *v, const void *elemAddr)
    {
        void *target = (char *) v->elems + v->logicLen * v->elemSize;
    
        if (v->logicLen == v->allocLen) {
            v->allocLen *= 2;
            // Once you call 'realloc', the value of 'elems' might change
            // This means that 'target' is now INVALID
            // 'target' is based on the old value of 'elems'
            v->elems = realloc(v->elems,v->allocLen*v->elemSize);
            assert(v->elems != NULL);
        }
        memcpy(target, elemAddr, v->elemSize);
        v->logicLen++;
    }
    

    To fix it, move the calculation for target below the reallocation:

    void VectorAppend(vector *v, const void *elemAddr)
    {
        if (v->logicLen == v->allocLen) {
            v->allocLen *= 2;
            v->elems = realloc(v->elems,v->allocLen*v->elemSize);
            assert(v->elems != NULL);
        }
        void *target = (char *) v->elems + v->logicLen * v->elemSize;
        memcpy(target, elemAddr, v->elemSize);
        v->logicLen++;
    }
    

    Another error: There is an error in your comments, which is part of the code and I’d recommend taking comments seriously.

    VectorNew(&aVector, sizeof(long), 4); // allocate 4*4 bytes 
    

    The comment should not say, “allocate 4*4” bytes because that is misleading: someday you’ll compile the program on a 64-bit system that isn’t Windows, and it will be 8×4 bytes. You’re better off removing the comment and just reading the code.

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

Sidebar

Related Questions

Basically, I've defined and typedef'ed this struct: typedef struct{ void** elements; int numElements; int
Given two structure in c: typedef struct _X_ { int virtual_a; int virtual_b; void
I have a struct typedef struct { int id; string name; string address; string
#include <stdio.h> typedef struct point{ int x; int y; }; void main (void){ struct
I am writing a dynamic array in C. typedef struct __c_array { void**_elem; int
I have a simple struct: typedef struct { void *things; int sizeOfThings; } Demo;
55 typedef struct pidmap { 56 atomic_t nr_free; 57 void *page; 58 } pidmap_t;
I'm curious why C++ does not define void via : typedef struct { }
Consider the following: typedef struct { int a; int b; int c; int d;
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;

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.