Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Accendentaly made program that prints random numbers without rand()

a.out returns something else everytime.
PHP Code

<?php 
int main
(int argc, char *argv[])
{
    
std::cout << (int)argv[2] + (int)argv[3] << std::endl;
   
} 
(Dec 12, 2018 11:57 AM)F.A.S.T Wrote:  a.out returns something else everytime.
PHP Code

<?php 
int main
(int argc, char *argv[])
{
 
   std::cout << (int)argv[2] + (int)argv[3] << std::endl;
 
  
} 

You are just casting random pointers to int. Nothing special and not worth making a thread about it.
using namespace std;

^ use this more often
!
(Dec 12, 2018 12:16 PM)wit Wrote:  using namespace std;

^ use this more often

Dont use that more often. Its a bad practice. Its fine for smaller projects but keep in mind that the std namespace has tons of identifiers, many of which are very common ones (think list, sort, string, iterator, etc.) which are very likely to appear in other code, too.
(Dec 12, 2018 12:34 PM)RascalPascal Wrote:  Dont use that more often. Its a bad practice. Its fine for smaller projects but keep in mind that the std namespace has tons of identifiers, many of which are very common ones (think list, sort, string, iterator, etc.) which are very likely to appear in other code, too.

The OP's code looks like something from a school assignment, in that case std is fine to use 99% of the time.
!
I tried this just now.

PHP Code

<?php 
#include <stdio.h>
#include <stdlib.h>

int main() {
    
    
short i = 10;
    
    while (
i) {
        
        
void *ptr = (int *)malloc(1);
    
        
printf("%p:%d\n", ptr, ptr);
        
        
free (ptr);
        
        --
i;
    }
    
    return 
0;
} 

Output:
Code
00000000002F7470:3110000
00000000003C76C0:3962560
00000000003C76C0:3962560
00000000003C76C0:3962560
00000000003C76C0:3962560
00000000003C76C0:3962560
00000000003C76C0:3962560
00000000003C76C0:3962560
00000000003C76C0:3962560
00000000003C76C0:3962560

Ahh yes, there is a VIRTUAL MEMORY !!
That's just ASLR in action.
(Dec 12, 2018 12:42 PM)wit Wrote:  The OP's code looks like something from a school assignment, in that case std is fine to use 99% of the time.

No it's just experiments. NO School assignment.

Anyway, I just wanted to know why that was happening. Thanks alot people.
(Dec 15, 2018 08:35 AM)F.A.S.T Wrote:  No it's just experiments. NO School assignment.

Anyway, I just wanted to know why that was happening. Thanks alot people.

Your welcome dude.
(Dec 12, 2018 12:42 PM)wit Wrote:  The OP's code looks like something from a school assignment, in that case std is fine to use 99% of the time.

Still, better to avoid bad practice for future sake than ignore what might be ahead and do it anyway. Maybe that philosophy is a bit much for simple stuff such as a statement for namespace usage, but it can tie in with anything and does with everything lol.

It actually makes me feel a bit better because I can see I'm not the only one specifying separate namespaces even on small tinkering projects like this.
[Image: chain.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by littleblueguy24Jun 14, 2022 12:03 AM 2 122 Jun 14, 2022 12:03 AM
Last Post: littleblueguy24
[C] by omy11Feb 17, 2021 02:17 PM 5 195 Feb 17, 2021 02:17 PM
Last Post: tux1
[Shell] by Nulla MisericordiaFeb 14, 2020 02:23 PM 0 163 Feb 14, 2020 02:23 PM
Last Post: Nulla Misericordia