【问题标题】:why my c string stack have the invalid write reported by valgrind-3.1.1为什么我的 c 字符串堆栈有 valgrind-3.1.1 报告的无效写入
【发布时间】:2012-02-28 14:53:24
【问题描述】:

valgrind 报告大小为 8 的无效写入

在 0x4007A0:ArrayCstringPush

通过 0x4008F8:main

地址 0x4A0A450 是一个大小为 8 的块分配后的 0 个字节

在 0x4905D27: calloc

按 0x.......: ArrayCstringNew

按 0x.......: 主要

为什么要报这个错误?以及如何解决问题。 谢谢!

  //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);
        }

【问题讨论】:

    标签: c


    【解决方案1】:

    代码中有几个malloc()calloc()的误用可能是导致无效写入的原因。

    变化:

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

    到:

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

    分别。

    【讨论】:

    • 这能解决这个 valgrind 报告吗? - 对malloc 的调用在#include 之后
    • 到 awoodland:"arrayOfCstring.h" 包含在 #include 之后
    【解决方案2】:

    而不是像分配你的数组

    ep = (ArrayCstring *) malloc (1000);
    

    您必须注意分配的大小是 ArrayCstring 的倍数,否则在访问最后一个元素时可能会超出分配的内存。

    我建议

    ep = (ArrayCstring *) malloc (1000 * sizeof (ArrayCstring));
    

    【讨论】:

    • c 中不需要强制转换。
    • 演员表不是这里的问题。
    猜你喜欢
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2013-02-07
    • 2014-03-17
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    相关资源
    最近更新 更多