Author Topic: Using delete ...  (Read 3537 times)

0 Members and 1 Guest are viewing this topic.

efernal

  • Newt
  • Posts: 198
Using delete ...
« on: June 15, 2011, 09:45:04 AM »
I'm new with c++...
Should I use "delete" for "saida"?
It's not working if I put delete saida; after fclose()...

Code: [Select]
void EFernal_ReadFile (char *nome)
{
FILE *fp = NULL;

if (fopen_s(&fp, nome, "r") != 0)
{
sds_alert(L"Erro:\n\n\n\tNão foi possível abrir o arquivo para leitura...");
sds_retvoid();
return;
}

char linha[255] = "";
wchar_t *saida = new wchar_t[255];

while(!feof(fp))
{
fgets(linha, 254, fp);
if((strlen(linha) > 5) && (linha[0] != ';'))
{
   saida = CA2W(linha);
sds_alert(saida);
}
}

fclose(fp);

Arquivo_Chm = CA2W("D:\\PlanetaCad_Loel\\PlanetaCAD_Loel.chm");
Help();

sds_retvoid();
}
e.fernal

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 7012
  • AKA Daniel
Re: Using delete ...
« Reply #1 on: June 15, 2011, 07:30:27 PM »
yes in this case you would use delete[] .
You also have the possibility of a dangling pointer, the way you have saida being assigned inside of a loop

efernal

  • Newt
  • Posts: 198
Re: Using delete ...
« Reply #2 on: June 15, 2011, 07:39:14 PM »
ok, thanks...
I was using only delete, not delete[]...
e.fernal

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 7012
  • AKA Daniel
Re: Using delete ...
« Reply #3 on: June 15, 2011, 07:51:23 PM »
this line is an error

Code: [Select]
saida = CA2W(linha);

efernal

  • Newt
  • Posts: 198
Re: Using delete ...
« Reply #4 on: June 15, 2011, 10:31:36 PM »
it´s working...
how would be the correct way?

Code: [Select]
saida = CA2W(linha);
e.fernal

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 7012
  • AKA Daniel
Re: Using delete ...
« Reply #5 on: June 16, 2011, 11:45:00 AM »
its pointers 101. if you assign a pointer a new value how can you call delete on it?
Example:
Code: [Select]
wchar_t *saida = new wchar_t[255];now the address of saida is pointing to a location in memory .. say 0x123456. now you call:
Code: [Select]
saida = CA2W(linha);which assigns a new address to saida, so saida might now be pointing to 0x123659.
now you want to call delete to free the memory you created at 0x123456,
but wait.... you can't because siada now points to 0x123659.

so now you have a "memory leak"   :police:


efernal

  • Newt
  • Posts: 198
Re: Using delete ...
« Reply #6 on: June 16, 2011, 02:35:51 PM »
This is a test, just for learning...
I changed it to:

Code: [Select]
void EFernal_ReadFile (char *nome)
{
FILE *fp = NULL;

if (fopen_s(&fp, nome, "r") != 0)
{
sds_alert(L"Erro:\n\n\n\tNão foi possível abrir o arquivo para leitura...");
sds_retvoid();
return;
}

char linha[255] = "";

while(!feof(fp))
{
fgets(linha, 254, fp);
if((strlen(linha) > 5) && (linha[0] != ';'))
{
sds_alert(CA2W(linha));
}
}

fclose(fp);

sds_retvoid();
}

it's working...
is this correct?

thanks for your help...
e.fernal

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 7012
  • AKA Daniel
Re: Using delete ...
« Reply #7 on: June 16, 2011, 07:30:53 PM »
better  :-)

pkohut

  • Bull Frog
  • Posts: 433
Re: Using delete ...
« Reply #8 on: June 17, 2011, 01:47:51 PM »
Code: [Select]
while(!feof(fp))
{
fgets(linha, 254, fp);
if((strlen(linha) > 5) && (linha[0] != ';'))
{
sds_alert(CA2W(linha));
}
}

it's working...
is this correct?

thanks for your help...

The while statement is bad form, leaving the possibility of an infinite loop if the error flag is raised on fp. Better to check if fgets returns NULL, then later check if the error flag is raised in fp.
Code: [Select]
while(fgets(linha, 255, fp)) {
// do your work here
}

if(ferror(fp)) {
// report error message.
}

Also, use the same buffer allocation size for linha and fgets. And since you posted this in the ARX group I'll assume you are using C++. So get in the habit of using namespaces and drop the EFernal_ prefix.

efernal

  • Newt
  • Posts: 198
Re: Using delete ...
« Reply #9 on: June 20, 2011, 08:31:30 AM »
Thank you, pkohut
I will study this way...
e.fernal