ustckun / USTC-Software2013

iGEM
http://2013.igem.org/Team:USTC-Software
Other
4 stars 8 forks source link

A memory leak in EasytoDebug.cpp #24

Open FreeBirdLjj opened 11 years ago

FreeBirdLjj commented 11 years ago

The lines 12~13 in EasytoDebug.cpp:

    char *s=new char;
    fscanf(temp,"%d %s",&number,s);

I think this will make memory leak, because s is just allocated one char but is used as a string.

ustckun commented 11 years ago

Thank you for your advice! This code has already abandoned. Recently, we are working on the GUI of our software. Codes on github's master branch has not been updated for a long time. The code in "connect" contains most newest code and we will collate our code on git soon. Welcome to give valuable suggestions at that time~ Best wishes!

Kun

2013/8/24 Junjie LU notifications@github.com

The lines 12~13 in EasytoDebug.cpp:

char *s=new char;
fscanf(temp,"%d %s",&number,s);

I think this will make memory leak, because s is just allocated one char but is used as a string.

— Reply to this email directly or view it on GitHubhttps://github.com/ustckun/USTC-Software2013/issues/24 .

Condensed Matter Physics University of Science and Technology of China Hefei, Anhui 230026 P. R. China

jinyangustc commented 11 years ago

I looked "fscanf", actually also "printf", at www.cplusplus.com. The web gives a piece of code in a similar way: /* fscanf example */

include

int main () { char str [80]; float f; FILE * pFile;

pFile = fopen ("myfile.txt","w+"); fprintf (pFile, "%f %s", 3.1416, "PI"); rewind (pFile); fscanf (pFile, "%f", &f); fscanf (pFile, "%s", str); fclose (pFile); printf ("I have read: %f and %s \n",f,str); return 0; }

http://www.cplusplus.com/reference/cstdio/fscanf/?kw=fscanf

在 2013-8-24,下午6:06,Junjie LU notifications@github.com 写道:

The lines 12~13 in EasytoDebug.cpp:

char *s=new char;
fscanf(temp,"%d %s",&number,s);

I think this will make memory leak, because s is just allocated one char but is used as a string.

— Reply to this email directly or view it on GitHub.

jinyangustc commented 11 years ago

Oh, you're right. Character array is different from character pointer. It may lead to memory leak. But it's not inevitable, is it? If I have initialized and, according my teammate, given '\0' to the end of it, is it still a problem?
在 2013-8-24,下午6:06,Junjie LU notifications@github.com 写道:

The lines 12~13 in EasytoDebug.cpp:

char *s=new char;
fscanf(temp,"%d %s",&number,s);

I think this will make memory leak, because s is just allocated one char but is used as a string.

— Reply to this email directly or view it on GitHub.

FreeBirdLjj commented 11 years ago

In fact, new char only allocates one byte space, but the fscanf() function will copy at least two bytes(because you at least input one byte and '\0' will occupies another one byte), the rest bytes are written to the memory you don't request, so it may make memory leak. You may mistake new char with new char[80] or char *s with string s. The legacy codes may make mistakes when the memory next to this one byte allocated to s is another variable or it can't be written. In a word, what will happen depends on the memory map.

jinyangustc commented 11 years ago

I see. I should review some basic chapters. Thank you. 在 2013-8-24,下午7:59,Junjie LU notifications@github.com 写道:

In fact, new char only allocates one byte space, but the fscanf() function will copy at least two bytes(because you at least input one byte and '\0' will occupies another one byte), the rest bytes are written to the memory you don't request, so it may make memory leak. You may mistake new char with new char[80] or char *s with string s. The legacy codes may make mistakes when the memory next to this one byte allocated to s is another variable or it can't be written. In a word, what will happen depends on the memory map.

— Reply to this email directly or view it on GitHub.