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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:01:51+00:00 2026-05-30T13:01:51+00:00

valgrind report the invalid write of size 8 at 0x4007A0:ArrayCstringPush by 0x4008F8:main Address 0x4A0A450

  • 0

valgrind report the invalid write of size 8

at 0x4007A0:ArrayCstringPush

by 0x4008F8:main

Address 0x4A0A450 is 0 bytes after a block of size 8 alloc’d

at 0x4905D27: calloc

by 0x…….: ArrayCstringNew

by 0x…….: main

why report this error ? and how to fix the problem.
thanks!

  //arrayOfCstring.h

  typedef struct {
  int numOfElems;
  int size;
  int allocSize;
  char** elems;
  //size_t elemAllocSize;

 } ArrayCstring;

void ArrayCstringNew(ArrayCstring *s,int allocS)
{
 s->allocSize=allocS;
 s->numOfElems=0;  
 s->size=0;
 s->elems=(char **)calloc(s->allocSize,sizeof(int));
 assert(s->elems!=0);
 }
 void ArrayCstringGrow(ArrayCstring *s){
     if(((s->numOfElems)+1)>(s->allocSize)){
     s->allocSize=(s->allocSize)*2;
     s->elems=(char**)realloc(s->elems,(s->allocSize)*sizeof(int));
      }
 }
   void ArrayCstringPush(ArrayCstring *s,char *elem,int lengthOfElem){
    ArrayCstringGrow(s);
    //(s->elems)[s->numOfElems]=(char *)malloc(lengthOfElem);

    (s->elems)[s->numOfElems]=(char *)calloc(lengthOfElem,sizeof(int));
    printf("start to realloc numOfElem is %i, allocSize is %i\n",s->numOfElems,s->allocSize); 
    strcpy((s->elems)[s->numOfElems],elem);
    //assert((s->elems)!=0);
    printf("push %s\n",s->elems[s->numOfElems]);
    s->numOfElems+=1;
  }
  char *ArrayCstringIndex(ArrayCstring *s,int i)
  {
  //assert((s->numOfElems)>i);
    return s->elems[i];
    }

 void ArrayCstringDelete(ArrayCstring *s)
 {
  int a=0;
  for(;a<(s->numOfElems);++a){
   free((s->elems)[a]);
                        }
   free(s->elems);
   }


    //MAIN FUNCTION
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    #include "arrayOfCstring.h"

       int main(void){
        ArrayCstring *ep;
        ep =(ArrayCstring *)malloc(1000);
        ArrayCstringNew(ep,2);
        ArrayCstringPush(ep,"ysdfsd",7);
        printf("start to \n");
        ArrayCstringPush(ep,"1213423",8);
        int a;
        for(a=0;a<2;++a){
         char *str=ArrayCstringIndex(ep,a);
         printf("string is %s\n",str);
                  }
         ArrayCstringDelete(ep);
        }
  • 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-30T13:01:52+00:00Added an answer on May 30, 2026 at 1:01 pm

    There are several misuses of malloc() and calloc() in the code which may be the cause of the invalid write.

    Change:

    ep =(ArrayCstring *)malloc(1000);
    
    s->elems=(char **)calloc(s->allocSize,sizeof(int));
    
    (s->elems)[s->numOfElems]=(char *)calloc(lengthOfElem,sizeof(int));
    

    to:

    ep = malloc(sizeof(ArrayCstring));
    
    s->elems= calloc(s->allocSize,sizeof(char*));
    
    (s->elems)[s->numOfElems]= strdup(elem);
    

    respectively.

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

Sidebar

Related Questions

Valgrind detect an invalid write of size 1 in this piece of code. In
For some reason valgrind keeps throwing the following error: ==6263== Invalid read of size
Valgrind reports error Invalid read of size 8 in the following code. I have
I am running valgrind as follows:- /usr/local/bin/valgrind process_name After excecution its giving me following
I know there is already at least a dozen valgrind reports invalid read questions,
When I run valgrind on the following (example) code it reports an Invalid free()
When I run valgrind on this function, it says I've definitely lost 4 bytes.
When I use this valgrind --leak-check=yes ./Main I have about 185236 errors. It said
Valgrind throws me out this error: ==11204== Syscall param write(buf) points to uninitialised byte(s)
I'm using the valgrind to know how many bytes my linux application is using.

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.