using NEW in C

weezergirl

Diamond Member
May 24, 2000
3,366
1
0
yes, the mark of a true nerd...programming on a saturday nite. :p

anyways, i forgot how to use new? do i have to include a header? because it's not recognizing "new" ...

anyone know? this is in C btw.

thanks.
 

beer

Lifer
Jun 27, 2000
11,169
1
0
I believe you only use it when you are dynamically allocating memory, usually when you're using pointers.

It should be part of standard ansi C, with no special libs requried.
 

weezergirl

Diamond Member
May 24, 2000
3,366
1
0
argh weird. i do

char *temp1;

temp1 = new char[strlen(pathtoken)+2+strlen(command.name)];

and it doesn't recognize the new there...
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: weezergirl
argh weird. i do

char *temp1;

temp1 = new char[strlen(pathtoken)+2+strlen(command.name)];

and it doesn't recognize the new there...

Use malloc: temp1 = (char*)malloc(strlen(pathtoken)+2+strlen(command.name));
 

Noriaki

Lifer
Jun 3, 2000
13,640
1
71
Originally posted by: weezergirl
argh weird. i do

char *temp1;

temp1 = new char[strlen(pathtoken)+2+strlen(command.name)];

and it doesn't recognize the new there...

That's C++ syntax...some compilers are pretty forgiving on intermixing C/C++ so you might have been using it for a long time and thought it was fine C, but technically it's not. You've gotta use malloc in true C.
 

kherman

Golden Member
Jul 21, 2002
1,511
0
0
Originally posted by: singh
Originally posted by: weezergirl
argh weird. i do

char *temp1;

temp1 = new char[strlen(pathtoken)+2+strlen(command.name)];

and it doesn't recognize the new there...

Use malloc: temp1 = (char*)malloc(strlen(pathtoken)+2+strlen(command.name));

You are correct sir!
malloc
calloc (I think)
and a bunch more. Hrey, look what google found!!! That should help clear things up.
 

weezergirl

Diamond Member
May 24, 2000
3,366
1
0
while we're on the subject of C...

can somebody explain fflush() to me? i think i sort of know what it does but do i need to do it?

in examples i see it being used after u fprintf(stdout, blahblah)

but i used it and din't do the fflush thing after...does it matter? when do you want to do it and not?

 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: weezergirl
while we're on the subject of C...

can somebody explain fflush() to me? i think i sort of know what it does but do i need to do it?

in examples i see it being used after u fprintf(stdout, blahblah)

but i used it and din't do the fflush thing after...does it matter? when do you want to do it and not?

Usually when you output something, it may be 'buffered'. If so, the results will not immediately display on the screen. Using fflush() forces the stream to be immediately written to the screen.
 

kherman

Golden Member
Jul 21, 2002
1,511
0
0
Originally posted by: singh
Originally posted by: weezergirl
while we're on the subject of C...

can somebody explain fflush() to me? i think i sort of know what it does but do i need to do it?

in examples i see it being used after u fprintf(stdout, blahblah)

but i used it and din't do the fflush thing after...does it matter? when do you want to do it and not?

Usually when you output something, it may be 'buffered'. If so, the results will not immediately display on the screen. Using fflush() forces the stream to be immediately written to the screen.

Well, that's most of it. You can overflow this buffer and upon doing so you will crash your program. In my experience on Unix, it freezes the program. So frequent flushing is recomended!
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: kherman

Well, that's most of it. You can overflow this buffer and upon doing so you will crash your program. In my experience on Unix, it freezes the program. So frequent flushing is recomended!

OK, only in Unix then...
 

Noriaki

Lifer
Jun 3, 2000
13,640
1
71
Yeah...and if your program crashes or something the buffered output might not get sent to the file.

If you are using small amounts of output and then close the file it doesn't matter so much if you use fflush or not, becuase it will empty the buffer on close.
But if you are doing large amounts of output it's a good idea to flush the buffer, so you don't get overflows and so you don't lose data if your program crashes.
 

kherman

Golden Member
Jul 21, 2002
1,511
0
0
Originally posted by: singh
Originally posted by: kherman

Well, that's most of it. You can overflow this buffer and upon doing so you will crash your program. In my experience on Unix, it freezes the program. So frequent flushing is recomended!

OK, only in Unix then...

At school, everyone had to use UNIX to compile/text programs. So it's where my experience is.
In industry, UNIX is VERY popular.

Also, please keep in mind that Microsoft does not follow the ANSI C or ANSI C++ documentation. It is very possible that Visual C++ adds flushes for you.

In school, it's important to learn the ANSI spec more than how a certain compiler interprets the spec, or just ignores it (see Microsoft:Disgust;)