Valhalla Legends Forums Archive | C/C++ Programming | Couple questions

AuthorMessageTime
PunK
Well, I'm returning to C++. It's roughly been a year so I am going to brush up and tune up my knowledge (which isn't much).

I've had some unanswered questions that I've googled and haven't found the best of results.

First..

I've noticed in C++ that you don't need to declare the function in the preprocessor but at the same time, you can.

Example:

[code]
#include <iostream>
using namespace std;

void testing()            //optional???

int main ()
{
     testing;
}

void testing ()
{
     cout << "Oh my";
}
[/code]

But if I don't declare testing, it still compiles error-free and runs just fine...




Second question is when I delete an array, it completely halts the console.

Example
[code]
int main ()
{
   char tit_sag[] = "Tits dangle";
   delete [] tit_sag;
}
[/code]

The console just freezes. I have a couple more questions, but ultimately these 2 kind of intrigue me.
April 8, 2009, 6:38 AM
Yegg
Even in C you don't need to do function prototypes. However, the compiler will not be able to do any compile-time error checking on that function. The prototype tells the compiler beforehand what datatype each argument is. This way if you were to call the function and provide invalid arguments, the compiler will error out and show you where the error took place.

As for the second issue, I've got a couple ideas but I'm at work right now so have no way to test something out.
April 8, 2009, 1:44 PM
FrostWraith
I don't typically use C++, I program in straight C (planning on making the move soon  ;)).  I do believe that the "delete" keyword should only be used when you are doing dynamic allocation, or in other words, when you allocate space to a pointer using the "new" keyword.  I could be wrong, but I think your array is allocated on the stack, and not the heap.

In C, its equivalent is malloc/free.

If you are afraid if your program will leak, just run it through valgrind or something and take a look yourself.
April 8, 2009, 2:47 PM
Yegg
[quote author=FrostWraith link=topic=17892.msg182237#msg182237 date=1239202065]
I don't typically use C++, I program in straight C (planning on making the move soon  ;)).  I do believe that the "delete" keyword should only be used when you are doing dynamic allocation, or in other words, when you allocate space to a pointer using the "new" keyword.  I could be wrong, but I think your array is allocated on the stack, and not the heap.

In C, its equivalent is malloc/free.

If you are afraid if your program will leak, just run it through valgrind or something and take a look yourself.
[/quote]

I believe you are correct.
April 8, 2009, 2:55 PM
BreW
[quote author=FrostWraith link=topic=17892.msg182237#msg182237 date=1239202065]
I could be wrong, but I think your array is allocated on the stack, and not the heap.
[/quote]
Yes, you are wrong. It's heap memory.
To dynamically allocate stack memory, check out _alloca().

[quote author=FrostWraith link=topic=17892.msg182237#msg182237 date=1239202065]
In C, its equivalent is malloc/free.
[/quote]
It's not equivalent, it IS a wrapper for malloc/free.

[quote author=FrostWraith link=topic=17892.msg182237#msg182237 date=1239202065]
If you are afraid if your program will leak, just run it through valgrind or something and take a look yourself.
[/quote]
Valgrind is linux only, last time I checked. If there's a windows port, that'd be pretty neat though.
April 8, 2009, 3:56 PM
BreW
I've just realized I've neglected to respond to the topic at hand (PunK's post), sorry.

[quote author=PunK link=topic=17892.msg182227#msg182227 date=1239172685]
I've noticed in C++ that you don't need to declare the function in the preprocessor but at the same time, you can.
....

But if I don't declare testing, it still compiles error-free and runs just fine...
[/quote]

I'm surprised that compiles. Usually, a semicolon is required to terminate the function prototype.
Function prototypes are NOT preprocessor directives as you alluded to in your post. They are a part of the actual language. They are required if the compiler does not know the arguments etc. If a function is declared before the reference, however,
the compiler knows the arguments, therefore eliminating the requirement of a function prototype.
What compiler are you using exactly? Your code should not crash at all. What you did there was something totally acceptable in C/++ ... any statement ending with a semicolon and a matching number of brackets is a complete statement. Therefore, you can expect code such as void main() { 1; 2; 3; } to compile, but it will do effectively nothing. By referencing the function "testing" without the parentheses, you merely referenced the address at which that function resides. It executed no actual code. As for the hang, my hunch is that the compiler you're using is crappy, and did not explicitly include the proper function epilogue for main since you are lacking the correct number of arguments and a return keyword.




[quote author=PunK link=topic=17892.msg182227#msg182227 date=1239172685]
Second question is when I delete an array, it completely halts the console.

Example
[code]
int main ()
{
   char tit_sag[] = "Tits dangle";
   delete [] tit_sag;
}
[/code]
[/quote]
You're missing the "new" keyword before char. In effect, you allocate stack memory for that string, then attempt to free() it as if it were heap memory. If anything it should cause a runtime error. I beg the question- WHAT compiler are you using!?!!
April 8, 2009, 4:11 PM
chyea
[quote author=brew link=topic=17892.msg182239#msg182239 date=1239206161]
[quote author=FrostWraith link=topic=17892.msg182237#msg182237 date=1239202065]
I could be wrong, but I think your array is allocated on the stack, and not the heap.
[/quote]
Yes, you are wrong. It's heap memory.
To dynamically allocate stack memory, check out _alloca().
[/quote]



[quote author=brew link=topic=17892.msg182240#msg182240 date=1239207106]
[quote author=PunK link=topic=17892.msg182227#msg182227 date=1239172685]
Second question is when I delete an array, it completely halts the console.
[/quote]

You're missing the "new" keyword before char. In effect, you allocate stack memory for that string, then attempt to free() it as if it were heap memory. If anything it should cause a runtime error. I beg the question- WHAT compiler are you using!?!!
[/quote]
April 10, 2009, 8:27 PM
BreW
[quote author=chyea link=topic=17892.msg182283#msg182283 date=1239395247]
[quote author=brew link=topic=17892.msg182239#msg182239 date=1239206161]
[quote author=FrostWraith link=topic=17892.msg182237#msg182237 date=1239202065]
I could be wrong, but I think your array is allocated on the stack, and not the heap.
[/quote]
Yes, you are wrong. It's heap memory.
To dynamically allocate stack memory, check out _alloca().
[/quote]



[quote author=brew link=topic=17892.msg182240#msg182240 date=1239207106]
[quote author=PunK link=topic=17892.msg182227#msg182227 date=1239172685]
Second question is when I delete an array, it completely halts the console.
[/quote]

You're missing the "new" keyword before char. In effect, you allocate stack memory for that string, then attempt to free() it as if it were heap memory. If anything it should cause a runtime error. I beg the question- WHAT compiler are you using!?!!
[/quote]
[/quote]

Yes. That is correct. I don't understand... Him missing the new keyword makes it a stack based string, new is a wrapper to malloc().
April 11, 2009, 12:37 AM
Quarantine
I think he was pointing out how you call FrostWraith wrong for claiming the char array is allocated on the stack, then you go to claim that the char array is allocated on the stack (Which is the basis for your criticism of him using the "delete" keyword).
April 11, 2009, 1:46 AM
BreW
[quote author=Warrior link=topic=17892.msg182285#msg182285 date=1239414387]
I think he was pointing out how you call FrostWraith wrong for claiming the char array is allocated on the stack, then you go to claim that the char array is allocated on the stack (Which is the basis for your criticism of him using the "delete" keyword).
[/quote]
Whoops, yep. I see my mistake. Could've sworn that read something on the lines of "arrays with new are allocated on the stack", or something.
Doesn't matter. It makes me that much more careful the next time to read before I write. Sorry, FrostWraith.
April 11, 2009, 2:52 AM

Search