Ehsan
PCs/Laptops are 🙂
Maybe tablets too, if u have a keyboard; but you can’t really make a development environment with the apps provided :(
Talula
https://www.onlinegdb.com/online_c_compiler
Talula
\Device\NUL
xypher
\Device\NUL
xypher
Im in linux btw🤣
\Device\NUL
xypher
\Device\NUL
Suka
Suka
AmR
I need CMake help please ?
AmR
I try this from 3 days
Leovan
I'm studying dynamic memory in C\C++ and don't understand that: I'm declare char array of 5 elements, it doesn't matter in C or C++ style.
1) Why I can get access over the memory area (for example m[10]) without errors/warnings? Shouldn't the OS keep track of this?
2) Why m[6] (and another more than array size) value is empty? Shouldn't there be garbage then? Maybe this is malloc's protection mechanism?
Sorry for my english, thanks for your patience.
#include <iostream>
#include <cstdlib>
int main()
{
char * m = (char *)malloc(sizeof(char)*5);
// char * m = new char[5];
for (int i = 0; i < 5; ++i) m[i] = 'H';
for (int i = 0; i < 10; ++i) std::cout << m[i];
std::cout << ';' << std::endl; // HHHHH;
free(m);
// delete [] m;
return 0;
}
coal
you dont need to cast it to char*
coal
is redundant cause the void pointer is already safely promoted to any other pointer type
coal
coal
m[6] is not truly empty, it's just you trying to access memory outside of the scope that malloc created for you
coal
m is a literal pointer, so it covers basically the entirety of your virtual memory, and you can sum or subtract as much as you wish from the address
coal
m[6] is syntactic sugar to sum to the pointer that malloc returns
coal
coal
its unnecessary since you're in C++
coal
Leovan
Leovan
not C
All okey than, he is said about that in the C context
coal
if for some reason, in the memory there were two consecutive arrays of size 5, called "a" and "b"
coal
"a[6]" would be equivalent to "b[0]"
The Shadow Monarch
#include<stdio.h>
#include<math.h>
int main()
{
double x1; //Abscissa of point A
double y1; //Ordinate of point A
double x2; //Abscissa of point B
double y2; //Ordinate of point B
printf("Input the coordinates of point A ");
scanf("%f %f \n", &x1, &y1);
printf("Input the coordinates of point B ");
scanf("%f %f \n", &x2, &y2);
double x=x2-x1;
double X=x*x;
double y=y2-y1;
double Y=y*y;
double Z=X-Y;
printf("Distance between the two points is : %f \n", sqrtf(Z));
return 0;
}
coal
because it's a literal pointer, therefore you can access any other value with it
The Shadow Monarch
I m gettiin a error 1.#INF00
Leovan
coal
Leovan
like access to uninizialized variable
coal
are you currently in linux btw?
Leovan
yes
coal
but its not illegal to access to them
Leovan
yes
coal
and there's no garbage to collect
coal
you're confusing the memory addresses that you dont manage with "garbage"
coal
garbage is when you initialize a new pointer and dont delete it when you're done using it
Leovan
but
int a;
std::cout << a;
will print garbage which at the address of the variable
Leovan
garbage for me is values which is in memory after running other programs and no longer used
coal
Anonymous
How to write entered date of birth by the user to CSV file ?
Anonymous
printf("Enter Date of Birth(DD/MM/YYY): ");
scanf("%d/%d/%d", &day,&mon,&year);
Leovan
no, it prints whatever data was at that particular memory location
hm, but as far as i know: let's take memory cell (for example with the address 0x1a7c78b5) - 1 byte. It was used by other programs before and now it contains data which they no longer need (10110111 for example). Now we in our new program declare (but not initialize) variable and it got the same address (0x1a7c78b5) which still contain this data - 10110111 (garbage) until we initialize it.
coal
coal
the garbage collector doesnt collect random garbage at some points of memory that are out of your program scope
coal
that would also be a segmentation fault
coal
the term garbage has a concept in CS already and you're defining a different concept for it
Leovan
coal
yes, your program is not responsible for other program's garbage
coal
in your context, its not garbage until you use and throw it
coal
uninitialized variables are not garbage because they're not being used at all
Leovan
Leovan
And they dont need it more
coal
coal
data that is not allocated
by your program is not being used therefore its not garbage
Leovan
But its left in RAM and no one need it
coal
garbage is the memory which was allocated by the program, but is no longer referenced
coal
coal
even if its not empty
coal
it would be unefficient to set all that memory to 0 just because its no longer used
coal
when its no longer used, even if it has data the memory is free
coal
and then when you allocate it, its now being used by your program
coal
also i would like to mention that garbage is only considered garbage within your program scope, because its memory that is restricting your own program from allocating it because it was being referenced by some lost pointer
Lamar
Hey guys I know this is a group to discuss and help with C++ but I'm looking for a laptop for School , real estate , crypto , photos general purposes. I my friend has a $500 budget and 8gb of RAM and SSD is necessary. Sorry for the off topic
coal
coal
or buying online, there's amazon and ebay
coal
in this group you're likely a few thousand kilometers away from everyone else