Page 1 of 1

Difference between Linux C and C++

PostPosted: February 14th, 2014, 5:59 am
by technogeek12
You cannot implicitly assign from a void* to any other type.
int *x = malloc(sizeof(int) * 10);
This will compile in C but not C++

HOWEVER this will work in C++
int an_int;
void *void_pointer = &an_int;
double *double_ptr = void_pointer;
*double_ptr = 5;


Because Code NO.1 Is not a "safe" type.
This means that you can have a void* that points to anything at all, and if you then assign the address stored in that void* to another pointer of a different type, there isn't any warning at all about it.

In C, there's only one major memory allocation function: malloc. You use it to allocate both single elements and arrays:
int *x = malloc( sizeof(int) );
int *x_array = malloc( sizeof(int) * 10 );

and you always release the memory in the same way:
free( x );
free( x_array );

In C++, however, memory allocation for arrays is somewhat different than for single objects; you use the new[] operator, and you must match calls to new[] with calls to delete[] (rather than to delete).
int *x = new int;
int *x_array = new int[10];
delete x;
delete[] x_array;

The short explanation is that when you have arrays of objects, delete[] will properly call the destructor for each element of the array, whereas delete will not.

Although most good C code will follow this convention, in C++ it is strictly enforced that all functions must be declared before they are used. This code is valid C, but it is not valid C++:
#include <stdio.h>
int main()
{
foo();
return 0;
}

int foo()
{
printf( "Hello world" );
}

You must declare functions before use


C++ has a much larger library than C, and some things may be automatically linked in by C++ when they are not with C. For instance, if you're used to using g++ for math-heavy computations, then it may come as a shock that when you are using gcc to compile C, you need to explicitly include the math library for things like sin or even sqrt:
% g++ foo.cc

or

% gcc foo.c -lm


C does not provide a native boolean type. You can simulate it using an enum, though:
typedef enum {FALSE, TRUE} bool;


In C++, you are free to leave off the statement 'return 0;' at the end of main; it will be provided automatically:
int main()
{
printf( "Hello, World" );
}

but in C, you must manually add it:
int main()
{
printf( "Hello, World" );
return 0;
}




P.S. Not related to topic
This forum seems to be a bit unstable. I noticed much security loopholes both in login and registration mode. Also, it seems to be using the free or premium( i don't know) shared hosting package from x10hosting. This is seriously not recommended. A forum this heavily modded will crash soon in a shared hosting package. X10 hosting does not allow the use of over 0.20 server load for over 60 seconds. Once the forum grows and bots start visiting, these MODs will cause a serious CPU hog. This would force X10hosting to terminate the website as stated in their acceptable use policy.

Re: Difference between Linux C and C++

PostPosted: February 14th, 2014, 10:13 pm
by Jessica
Great topic even though I have no idea what most of the stuff is talking about :lol:

technogeek12 ยป Feb 14 2014, 3:59 am wrote:
P.S. Not related to topic
This forum seems to be a bit unstable. I noticed much security loopholes both in login and registration mode. Also, it seems to be using the free or premium( i don't know) shared hosting package from x10hosting. This is seriously not recommended. A forum this heavily modded will crash soon in a shared hosting package. X10 hosting does not allow the use of over 0.20 server load for over 60 seconds. Once the forum grows and bots start visiting, these MODs will cause a serious CPU hog. This would force X10hosting to terminate the website as stated in their acceptable use policy.


I am using x10hosting Prime. As far as I know, I'm not using a shared hosting package. I've ran this board for over 3 years and never had issues with x10 needing to terminate the website...I've had issues in the past (many instances) when the board goes down because of "Resource Limit Hit" messages...but it's been quite a while since I've even gotten one of those messages. Any other issues are server or host related....