A list of puns related to "Dangling pointer"
The code below compiles, despite having a bug in function g(), where a pointer to the instance is deleted and then a method is invoked.
Does ```g++ -std=c++20``` have a compiler option that can be used to flag a compile time error ? Any help will be appreciated.
#include <iostream>
class X {
int x;
public:
X ();
auto print () -> void const;
};
X::X () : x{0} {
;
}
auto X::print () -> void const {
std::cout << x << std::endl;
}
auto f (X *p) -> void {
delete p;
}
auto g () -> void {
X *q = new X;
f (q);
q->print();
}
auto main () -> int {
g ();
return 0;
}
And if it is, can you have multiple mut references at the same time?
fn main() {
let ptr = std::mem::align_of::<()>() as *mut ();
let ref1 = unsafe { &mut *ptr };
let ref2 = unsafe { &mut *ptr };
println!("{:?}, {:?}", ref1, ref2);
}
I have a struct called "Objects" that looks something like this:
typedef struct Object{
float[3] position;
float[3] rotation;
Mesh* mesh;
Material* material;
...
} Object;
Each object can have a pointer to a mesh, but a mesh can be shared between two different objects:
Mesh* mesh1;
Object* obj1
Object* obj2;
obj1->mesh = mesh1;
obj2->mesh = mesh1;
The problem arise when I free the mesh, the mesh pointers in each object is now pointing to an invalid memory location and I can't know because the pointer isn't NULL. Is there any simple way to solve this?
I'm not sure but I think the solution in c++ is weak pointers or smart pointers? I could also store each reference to the mesh in it's structure for cleanup but that would be cumbersome.
Hello community , I'm actually working on the rust cookbook and I didn't understand why there is a dangling pointer in this case.
fn dangle() -> &String { // dangle returns a reference to a String
let s = String::from("hello"); // s is a new String
&s // we return a reference to the String, s
} // Here, s goes out of scope, and is dropped. Its memory goes away. // Danger!
It said that " when the function is finished we deallocated the string ,but we tried to return a reference to it"
In my opinion ,we are first returning a reference to the string ,then it is deallocated because it's the end of the scope.
Hi,
I am trying to read garbage values of memory, character by character.
For that i am using dangling pointers, e.g. pointers that have not been initialized, and therefore point to random places in the memory.
If I try to "inspect" the stack memory:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 200
int main ()
{
char* ptr;
/* ptr = malloc(SIZE); */
for (int n = 0; n < SIZE; n++)
{
if (*(ptr + n) != 0)
printf("The value ptr[%d] is %d\n", n, *(ptr + n) );
}
printf("this is a test\n");
}
I get:
The value ptr[156] is -1
The value ptr[157] is 127
The value ptr[160] is 47
The value ptr[161] is -17
The value ptr[162] is -1
The value ptr[163] is -1
The value ptr[164] is -1
The value ptr[165] is 127
The value ptr[168] is 66
The value ptr[169] is -17
The value ptr[170] is -1
The value ptr[171] is -1
The value ptr[172] is -1
The value ptr[173] is 127
The value ptr[184] is 3
The value ptr[192] is 64
The value ptr[194] is 32
which is what i expect to get, since the places of the memory are random, their corresponding values should also be random.
However, if I try to inspect the heap memory, all the values pointed by the array of dangling pointers are 0:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 200
int main ()
{
char* ptr;
ptr = malloc(SIZE);
for (int n = 0; n < SIZE; n++)
{
if (*(ptr + n) != 0)
printf("The value ptr[%d] is %d\n", n, *(ptr + n) );
}
printf("this is a test\n");
}
I get the following output:
this is a test
which means all the values pointed by the arrays of dangling pointer (since i did not initialize the malloc memory block to anything) are 0.
Why do i get everything 0 for the heap memory? I would expect to read some garbage random values...
Is my understanding of "dangling pointers" correct?
Many thanks!
my question is simple ,according to my knowledge this example shouldn't work considering it returns a pointer to a dead object
const char* f() { const char* p="hi"; return p; }
so do strings get sepecial treatment and they get allocation in heap not in stack (implicitly ??) or is it due to some compiler optimization ? although i've tried to disable these . ps : i'm using g++ 6.3
and thnx :)
I have watched many youtube videos on it and still dont really understand. Could I get some input on this https://pastebin.com/s44XAf9M I feel if i delete current and prev every time it will delete the nodes they are pointed too and am just very unsure... thanks :D
An abstract question: Say I have a singly-linked list of the nodes:
root->[data|next]->[data|next]->NULL
In C, root is declared:
struct Node *root = NULL;
where *root is the Node pointer 'holding' the address "NULL".
Now, lets say that I want to remove the last node in the linked list, the following code will allow the computer to do such action:
//pop: pop removes last/latter node in linked list and sets new last node to NULL
void pop(struct Node *root){
struct Node *last;
struct Node *current = root; //current point to same node as root
while(current->next != NULL){
if(current->next == NULL) {break;} //premature break in traversing of linked list; will stop at last node
last = current; //last points to same node as current
current = current->next; //move current to next node
}
last->next = NULL; //point second-to-last node to NULL, making it defacto last node in list
free(current); //free last node to heap
}//end pop
after calling pop and passing root to the function, the new linked list looks like this:
root->[data|next]->NULL
If the program calls pop again, we should expect the linked list to look like this:
root->NULL
However, it does not! In the case of a linked list of integer elements in order, we will call pop until we observe strange behaviour:
List: 1 2 3
Call pop
List: 1 2
Call pop
List: 1
Call pop
List 1980765
The above is an example of undefined behavoir caused by a dangling pointer. Now the question is: How can the program avoid this behaviour and produce a side-effect that close to root->NULL from popping all nodes from the linked list until said list is empty?
Update:
struct Node *pop(struct Node *root){
int index = 0;
struct Node *last = root;
struct Node *new;
while(last->next != NULL){
new = last;
last = last->next; //move to next node
index++; //change index flag
}
if(index > 0){
new->next = NULL; //new last node points to NULL
free(last); //free last node
}
if(index == 0){return(NULL);} //return NULL
return(root); //return linked list
}//end burn
I've been learning about dynamic memory allocation, and have this snippet of code which demonstrates how deallocating memory may cause multiple dangling pointers:
int *ptr = new int;
int *otherPtr = ptr;
delete ptr;
ptr = nullptr;
otherPtr is a dangling pointer.
If I have understood correctly, ptr was the owner of the memory, so the delete operator was required to return the allocated memory back to the operating system.
otherPtr was not the owner of the memory, so the delete operator is not necessary, it just needs to be set to null.
otherPtr = nullptr;
Is that correct?
Hi there,
I'm currently implementing a priority queue in C, just to get to grips with how the structure works. I have an initialisation helper function, which allocates memory for the structure and returns a pointer to it. When I run the program with valgrind, I get a message saying that a memory leak has occurred in the init_pqueue() method:
by 0x40068D: init_pqueue
I'm pretty sure I know why this is happening - I'm declaring and initialising the pointer within the function and returning it, so any calls to free() would have to be made within the scope of the function. However, I wouldn't be able to return the pointer if this were the case. (Is this right?)
Could anyone show me how to initialise this structure without the memory leaks? Should I be using a helper function that returns a pointer it has created?
Code is here
Thanks for any help.
int *a = new int;
int *b = a;
delete b;
/* a and b are now dangling pointers */
*a = 4; /* Memory error: we may be overwriting another pointer's data */
Can someone explain this a bit further? I still don't quite understand what a dangling pointer is or how this is an example of it. What does new do, in this case, to the value of a?
Here's the Wiki article: http://en.wikipedia.org/wiki/Memory_safety#Dangling_pointer
Thanks!
Like so:
#include <studio.h>
main() {
void *flaccid = 0; // Is this right?
printf("%p\n", flaccid);
}
But he was just referring to a bunch of garbage.
That's the first time that a player had as many 3 pointers as 3 assists.
I don't want to step on anybody's toes here, but the amount of non-dad jokes here in this subreddit really annoys me. First of all, dad jokes CAN be NSFW, it clearly says so in the sub rules. Secondly, it doesn't automatically make it a dad joke if it's from a conversation between you and your child. Most importantly, the jokes that your CHILDREN tell YOU are not dad jokes. The point of a dad joke is that it's so cheesy only a dad who's trying to be funny would make such a joke. That's it. They are stupid plays on words, lame puns and so on. There has to be a clever pun or wordplay for it to be considered a dad joke.
Again, to all the fellow dads, I apologise if I'm sounding too harsh. But I just needed to get it off my chest.
Hi everyone,
I'm currently following the (great) "Crafting interpreters" book, by Robert Nystrom.
In the 16th chapter, the auteur implements a function (errorToken()
) which does something like this:
#include <stdio.h>
typedef struct {
const char * text;
} Str;
Str build_and_set_str(char* text) {
Str str = { .text = text };
return str;
}
Str get_str(void) {
char* local = "'get_str' local";
return build_and_set_str(local);
}
int main(void) {
Str str = get_str();
printf("%s\n", str.text);
}
If I compile and run this, I get this:
gcc test.c -o test -O0
./test
'get_str' local
My C is a bit rusty, and I don't understand why it works; in my understanding of C function, stack variables are "dropped" when the function returns, unless they are returned.
So a.text
should be a dangling pointer when get_str()
returns, leading to a segmentation fault.
I don't understand what I'm missing here, can someone help me out ?
EDIT:
I'm impatient and stupid. The author explain it right afterward:
> Again, we need to ensure that the error message sticks around long enough for the compiler to read it. In practice, we only ever call this function with C string literals. Those are constant and eternal, so weβre fine.
So If i understand correctly, char* local = "'get_str' local";
will not be stored on the stack, but on some static piece of memory.
Sorry for wasting your time.
Here is all the penis slang I could find, courtesy of the internet
A
Ace in the hole
Acorn Andy
Action Jackson
Adam Halfpint
Admiral Winky
African black snake
Afro man
AIDS baster
AIDS grenade, The
Alabama blacksnake
Albino cave dweller
All-day sucker
Anaconda
Anal impaler
Anal intruder
Anal Spear
Ankle spanker
Apple-headed monster
Ass blaster
Ass pirate
Ass wedge
Astralgod
Auger-headed gut wrench
B
Ba-donk-a-donk
Baby maker
Baby's arm holding an apple
Baby's arm in a boxing glove
Bacon bazooker
Bacon rod
Badboy
Bagpipe
Bald Avenger, The
Bald butler
Bald-headed beauty
Bald-headed giggle stick
Bald-headed hermit
Bald-headed Jesus
Bald-headed yogurt slinger
Baldy-headed spunk-juice dispenser
Ball buddy
Baloney pony
Banana
Bat and balls
Battering ram
Bayonet
Bavarian Beefstick
Beard splitter
Bearded burglar
Beastus maximus
Beaver buster
Beaver Cleaver
Bed snake
Beef baton
Beef bayonet
Beef belt buckle
Beef bugle
Beef bus
Beef missile
Beef soldier
Beef stick
Beefy McManstick
Bell rope
Belly stick
Best leg of three
(Big) Beanpole
Big Dick & the twins
Big Dickus
Big Jake the ene-eyed snake
Big Jim and the Twins
Big Johnson
Big Lebowski
Big number one
Big Mac
Big red
Big rod
Big Uncle
Biggus Dickus
Bilbo Baggins
Bishop, The
Bishop with his nice red hat
Bitch blaster
Bitch stick
Bits and pieces
Blind butler
Blind snake
Blood blunt
Blood slug
Blood sword
Blow pop
Blowtorch
Blue steel
Blue-veined jackhammer
Blue-veined junket pumper
Blue-veined piccolo
Blue-veined puss chucker
Blue-veiner
Blunt
Bob
Bob Dole
Bob Johnson
Bobo
Bone
Bone phone
Bone rollercoaster
Boneless beef
Boneless fish
Boner
Boney cannelloni
Bone-her
Bop gun
Bottle rocket
Bow-legged swamp donkey
Box buster
Boybrush
Bradford and the pair
Bratwurst
Breakfast burrito
Breakfast wood
Broom
Brutus
Bubba
Bulbulous big-knob
Bumtickler
Bush beater
Bush rusher
Bushwhacker
Buster Hymen
Buster McThunderstick
Butt blaster
Butt pirate
Butter churn
Butt
My boy Leo doesn't care about toys, aside from little wool balls he can chase around and carry in his mouth. He gives NO hecks about laser pointers, toys with bells, catnip anything, feathers on a string. He particularly doesn't care about interactive toys - he wants to play on his terms, by himself. He'll give you a token swat or two if you dangle something at him, but almost immediately loses interest. Despite this, he seems bored. I would really appreciate favourite toy suggestions that your bengals are obsessed with!
Alot of great jokes get posted here! However just because you have a joke, doesn't mean it's a dad joke.
THIS IS NOT ABOUT NSFW, THIS IS ABOUT LONG JOKES, BLONDE JOKES, SEXUAL JOKES, KNOCK KNOCK JOKES, POLITICAL JOKES, ETC BEING POSTED IN A DAD JOKE SUB
Try telling these sexual jokes that get posted here, to your kid and see how your spouse likes it.. if that goes well, Try telling one of your friends kid about your sex life being like Coca cola, first it was normal, than light and now zero , and see if the parents are OK with you telling their kid the "dad joke"
I'm not even referencing the NSFW, I'm saying Dad jokes are corny, and sometimes painful, not sexual
So check out r/jokes for all types of jokes
r/unclejokes for dirty jokes
r/3amjokes for real weird and alot of OC
r/cleandadjokes If your really sick of seeing not dad jokes in r/dadjokes
Punchline !
Edit: this is not a post about NSFW , This is about jokes, knock knock jokes, blonde jokes, political jokes etc being posted in a dad joke sub
Edit 2: don't touch the thermostat
Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.