English 中文(简体)
两个中点返回相同的指针值
原标题:two mallocs returning same pointer value
  • 时间:2012-05-27 19:20:16
  •  标签:
  • c
  • malloc

I m filling a structure with data from a line, the line format could be 3 different forms:
1.-"LD "(Just one word)
2.-"LD A "(Just 2 words)
3.- "LD A,B "(The second word separated by a coma).
The structure called instruccion has only the 3 pointers to point each part (mnemo, op1 and op2), but when allocating memory for the second word sometimes malloc returns the same value that was given for the first word. Here is the code with the mallocs pointed:

instruccion sepInst(char *linea){
instruccion nueva;
char *et;

while(linea[strlen(linea)-1]==32||linea[strlen(linea)-1]==9)//Eliminating spaces and tabs at the end of the line
    linea[strlen(linea)-1]=0;
et=nextET(linea);//Save the direction of the next space or tab
if(*et==0){//If there is not, i save all in mnemo
    nueva.mnemo=malloc(strlen(linea)+1);
    strcpy(nueva.mnemo,linea);
    nueva.op1=malloc(2);
    nueva.op1[0]= k ;nueva.op1[1]=0;//And set a "K" for op1
    nueva.op2=NULL;
    return nueva;
}
nueva.mnemo=malloc(et-linea+1);<-----------------------------------
strncpy(nueva.mnemo,linea,et-linea);
nueva.mnemo[et-linea]=0;printf("
j%xj",nueva.mnemo);
linea=et;
while(*linea==9||*linea==32)//Move pointer to the second word
    linea++;
if(strchr(linea, , )==NULL){//Check if there is a coma
    nueva.op1=malloc(strlen(linea)+1);//Do this if there wasn t any coma
    strcpy(nueva.op1,linea);
    nueva.op2=NULL;
}
else{//Do this if there was a coma
    nueva.op1=malloc(strchr(linea, , )-linea+1);<----------------------------------
    strncpy(nueva.op1,linea,strchr(linea, , )-linea);
    nueva.op1[strchr(linea, , )-linea]=0;
    linea=strchr(linea, , )+1;
    nueva.op2=malloc(strlen(linea)+1);
    strcpy(nueva.op2,linea);printf("
2j%xj2",nueva.op2);
}
return nueva;
}

When I print the pointers it happens to be the same number. note: the function char *nextET(char *line) returns the direction of the first space or tab in the line, if there is not it returns the direction of the end of the line.

sepInst () 在一个程序里被多次调用, 只有在它被调用过几次后才开始失效。 在我的所有程序里这些 mallocs 都让我头疼。

问题回答

主要有两种可能性。

您要么在程序( 搜索 < code> free 或 < code> realloc 的呼叫) 中其它地方释放内存。 在此情况下, 您看到的效果是完全无害的 。

或者,您可能会遭受记忆腐败, 很可能是缓冲溢出。 短期的解药是使用专门工具( < a href=" http:// en.wikipedia. org/ wiki/ Memory_ debugger" rel= “ nofollow” >memory debuger ) 。 选择您平台上可用的工具 。 该工具需要重新校正( 重新连接), 并最终告诉您您您的代码在先前定义的缓冲限制之外的确切位置 。 可能有多个违规代码位置 。 将每个代码视为严重的缺陷 。

一旦你厌倦了这种研究, 学会使用 < code > aconst 修饰符, 并在所有变量/ 参数声明中使用它来进行清洁。 这无法完全防止缓冲溢出, 但它会将它们限制在想要成为可写缓冲的变量上( 例如, 您问题所涉及的变量/ 参数显然不是 ) 。

顺便提一句,我个人认为,你应该更加努力地把商场叫做小商场。 这是一个良好的业绩理念,也减少了腐败。

nueva.mnemo=malloc(strlen(linea)+1);
strcpy(nueva.mnemo,linea);
nueva.op1=malloc(2);

应该是

// strlen has to traverse your string to get the length,
// so if you need it more than once, save its value.
cbLineA = strlen(linea); 
// malloc for the string, and the 2 bytes you need for op1.
nueva.mnemo=malloc(cbLineA + 3);
// strcpy checks for  again, so use memcpy
memcpy(nueva.mnemo, linea, cbLineA);  
nueva.mnemo[cbLineA] = 0;
// here we avoid a second malloc by pointing op1 to the space we left  after linea
nueva.op1 = nueva.mnemo + cbLinea + 1;

只要您能够通过预算来减少圆柱数.... do it。 您正在使用 C! 这不是滥用堆肥或垃圾收集的更高层次的语言!





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签