• Welcome to Valhalla Legends Archive.
 

Couple questions

Started by PunK, April 08, 2009, 01:38 AM

Previous topic - Next topic

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:


#include <iostream>
using namespace std;

void testing()            //optional???

int main ()
{
     testing;
}

void testing ()
{
     cout << "Oh my";
}


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

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


The console just freezes. I have a couple more questions, but ultimately these 2 kind of intrigue me.

Yegg

#1
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.

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.

Yegg

Quote from: FrostWraith on April 08, 2009, 09:47 AM
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.

I believe you are correct.

brew

Quote from: FrostWraith on April 08, 2009, 09:47 AM
I could be wrong, but I think your array is allocated on the stack, and not the heap.
Yes, you are wrong. It's heap memory.
To dynamically allocate stack memory, check out _alloca().

Quote from: FrostWraith on April 08, 2009, 09:47 AM
In C, its equivalent is malloc/free.
It's not equivalent, it IS a wrapper for malloc/free.

Quote from: FrostWraith on April 08, 2009, 09:47 AM
If you are afraid if your program will leak, just run it through valgrind or something and take a look yourself.
Valgrind is linux only, last time I checked. If there's a windows port, that'd be pretty neat though.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

brew

I've just realized I've neglected to respond to the topic at hand (PunK's post), sorry.

Quote from: PunK on April 08, 2009, 01:38 AM
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...

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 from: PunK on April 08, 2009, 01:38 AM
Second question is when I delete an array, it completely halts the console.

Example

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

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!?!!
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

bulletproof tiger

Quote from: brew on April 08, 2009, 10:56 AM
Quote from: FrostWraith on April 08, 2009, 09:47 AM
I could be wrong, but I think your array is allocated on the stack, and not the heap.
Yes, you are wrong. It's heap memory.
To dynamically allocate stack memory, check out _alloca().



Quote from: brew on April 08, 2009, 11:11 AM
Quote from: PunK on April 08, 2009, 01:38 AM
Second question is when I delete an array, it completely halts the console.

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!?!!

brew

Quote from: chyea on April 10, 2009, 03:27 PM
Quote from: brew on April 08, 2009, 10:56 AM
Quote from: FrostWraith on April 08, 2009, 09:47 AM
I could be wrong, but I think your array is allocated on the stack, and not the heap.
Yes, you are wrong. It's heap memory.
To dynamically allocate stack memory, check out _alloca().



Quote from: brew on April 08, 2009, 11:11 AM
Quote from: PunK on April 08, 2009, 01:38 AM
Second question is when I delete an array, it completely halts the console.

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!?!!

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().
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Warrior

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 from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

brew

Quote from: Warrior on April 10, 2009, 08:46 PM
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).
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.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P