Gladiator
👍
14•08
14•08
#include <stdio.h> #include <stdlib.h> /* required for the malloc and free functions */
int main() {
int number;
int *ptr;
int i;
printf("How many ints would you like store? ");
scanf("%d", &number);
ptr =malloc(number*sizeof(int));
/* allocate memory */
if(ptr!=NULL) { for(i=0 ; i<number ; i++) { *(ptr+i) = i; }
for(i=number ; i>0 ; i--) { printf("%d\n", *(ptr+(i-1)));
/* print out in reverse order */ }
free(ptr); /* free allocated memory */
return 0; }
else { printf("\nMemory allocation failed - not enough memory.\n");
return 1; }
}
14•08
Ishud copy in pastebin
Anonymous
No
you got two groups of people , people that C guys and C++ guys that hate on C and say that C style of coding is not good . OOP lovers , and there are some who are not so bias
14•08
Anonymous
14•08
Ok
Anonymous
Anonymous
14•08
https://pastebin.com/SEd6AuRy
David🇨🇺ElChino
14•08
klimi
I thought clang has c++17 support too
Francisco
Francisco
It's libc++ what's not fully completed
klimi
Hm
Anonymous
Francisco
Hm
https://en.cppreference.com/w/cpp/compiler_support
Anonymous
after an update, they made it a pain in the ass to install GNU
Francisco
Well, clang is a really good compiler after all
David🇨🇺ElChino
So Open Source compilers won't fully support c++17? I don't think so
Francisco
I use both gcc and clang
Anonymous
klimi
14•08
professor
if a memory leak becomes a dangling pointer after delete how can I fix this ?
Anonymous
After you delete a pointer p pointing to some allocated memory, just don't use the value in the pointer anymore: either don't use the variable at all or overwrite it with something else.
professor
int* ptr = new int(5); memory leak
delete ptr # fix memory leak
if we dont add ptr = NULL we got information leak right? # info leak fix
return ptr
professor
is this what you mean?
Raz
Anonymous
^It's not a memory leak
Anonymous
A memory leak would be:
Anonymous
int *ptr = new int[5];
//Do stuff
ptr = nullptr; //We lose the originally allocated 5 ints, and there's no way to delete them; We 'leak' memory
Raz
Anonymous
I'm showing him what a memory leak is: to show what it is you wouldn't use unique_pointers
professor
in a few words after delete , free becomes a dang pointer and a new vuln information leak , and we need to point to null to fix right ?
Raz
professor
If i do this then it will keep the info leak bug
Raz
professor
I will check and smart pointers too
Anonymous
unique_pointer is a smart pointer; there's a couple more
David🇨🇺ElChino
Anonymous
except if you're stuck with pre-C++11 C++ for some reason
David🇨🇺ElChino
professor
professor
I am just checking appsec c++ that’s why delete , new , free
Anonymous
free is a C function from stdlib.h
David🇨🇺ElChino
professor
Some mate says smart pointers and other thing
Anonymous
You don't delete memory leaks, since a memory leak is when you have memory to which you have no pointer anymore; You 'delete' allocated (with new) memory.
David🇨🇺ElChino
Anonymous
Anonymous
No, i mean the delete keyword.
Raz
Some mate says smart pointers and other thing
If you want to protect against info leaks
Just assign your raw pointer to nullptr/NULL after deleting it. Also, assign your new allocated memory, if it has not been assigned yet.
David🇨🇺ElChino
Anonymous
Both are used for same purpose, but still they have some differences, the differences are: delete is an operator whereas free() is a library function. delete free the allocated memory and calls destructor. But free() de-allocate memory but does not call destructor.
Anonymous
from google , i see they are similar
Anonymous
i learning how to deal with memory leaks now
14•08
#cpp
14•08
/notes
Bennie
/get gcc9
Bennie
/get stop-teaching-c
Bennie
😏 no notes on digraph and trigraph sequences - https://www.google.com/amp/s/developerinsider.co/what-is-digraphs-trigraphs-and-tokens-c-cpp-programming-language/amp/
Daniyar
Hi, which course you recommend on "coursera" to learn c++?
Tokin
Who here has experience with WinAPI?
Anonymous
Anonymous
Anonymous
bandhan
i need to limit android app data limit
like by editing the apk pls help
bandhan
like 100 kb/s speed
Anonymous
Anonymous
I want the insertion point to be up and not down every time I press ENTER
Anonymous
I am using the Codeblocks IDE