Anonymous
Note you will NEED to rename this variable if you want it to compile for C++
Anonymous
as new is C++ version of malloc
Sabyasachi
Ok
Anonymous
and delete is C++ version of free The is no C++ version of realloc
Golden Age Of
If you push an object that is dereferenced from a pointer the push_back will be that of a copy. So you will have to deallocate the memory allocated inside the function yourself.
But there is a problem , i need to return that object. I cant return it and deallocate, and i cant deallocate and return, there is a problem
Anonymous
If you push an object that is dereferenced from a pointer the push_back will be that of a copy. So you will have to deallocate the memory allocated inside the function yourself.
in other words Int * ptr = malloc(mem); v.push_back(ptr); // ptr is now referenced by both v and ptr free(v.back()); // valid free(ptr); // also valid, WILL result in double free if return of v.back is also freed
Anonymous
But there is a problem , i need to return that object. I cant return it and deallocate, and i cant deallocate and return, there is a problem
Which is why you should use smart pointers. In this case you can just return a smart pointer and it will take care of deallocation when required
Anonymous
Anonymous
I skimmed it
Well dont reply to someone's post unless you have read it and understood it.
Anonymous
preferably a minimal example and not tons of code
Anonymous
I thought on it, but it makes a lot problems then
Means you have not designed it well. Relook your design and work with smart pointers. With C++17 and above, the use of new and delete should be very limited.
Anonymous
unless you delete void*
Anonymous
Heh, constructors and destructors work just fine
Yes so the guys at ISOCPP who recommend using smart pointers dont know as much C++ as you do I guess 🙂
Anonymous
which deletes the memory WITHOUT calling its destructor
Anonymous
Hi
Anonymous
Can I send a picture of an error I am getting?
dipesh
How can I recursion in c? Help please.
Code WHIZ
Anonymous
How can I recursion in c? Help please.
Like this: unsigned long long factorial(unsigned char n){ if (n == 0) return 1ull; else return n*factorial(n-1); }
dipesh
OK I will try it
dipesh
Thanks
@𝑺𝒐𝒃𝒌𝒂
😭can u show me pls
You can use the first part (before the switch structure) of main function like this 👇 WITHOUT using ctype.h I mentioned. float num1,num2,result; int choice = 0; int err; do { printf("1.Addition\n"); printf("2.Subtraction\n"); printf("3.Multiplication\n"); printf("4.Division\n"); printf("5.Exit\n\n"); do{ printf("Enter Your Choice : "); err = scanf("%d",&choice); getchar(); if(err != 1) puts("Your choice must be an integer!"); } while(err != 1);
Anonymous
I'm not able to send pictures here. So I am facing this problem. I wrote a program and I got this error "static declaration follows non static declaration" So when I added static before my function, i got another error: invalid storage class for function What do I do
Sabyasachi
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node*next; }*head=NULL; void insert(int x) { struct node* newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=x; newnode->next=NULL; struct node* ptr=head; if(ptr==NULL) { ptr=newnode; head=newnode; } else { while(ptr->next!=NULL) ptr=ptr->next; ptr->next=newnode; } } void disp(struct node* start) { struct node* ptr=start; if(ptr==NULL) printf("Head Node is empty"); else { while(ptr!=NULL) { printf("%d->",ptr->data); ptr=ptr->next; } } } struct node* rotate() { struct node* ptr=head; struct node* prev=NULL; struct node* nex=ptr->next; struct node* nexnex=(ptr->next)->next; do { prev=ptr; nex->next=prev; ptr=nex; if(prev==head) prev->next=NULL; ptr=ptr->next; nex=nex->next; nexnex=nexnex->next; }while(nexnex!=NULL); return(nex); } int main () { int k, x; printf ("Enter the number of node"); scanf ("%d", &k); for (int i = 1; i <= k; i++) { printf ("Enter the data for nodes"); scanf ("%d", &x); insert(x); } disp(head); head=rotate(); disp(head); return(0); }
Sabyasachi
Now tell me. Done just as you prescribed
Anonymous
Couldnt find a solution on google. https://pastebin.com/XL9spgiq
At line 33 and line 56, you are trying to define a function within another function. This is not allowed.
Sabyasachi
Anonymous
Amruta
Thanks
Anonymous
You mean line 31 and 54? But hows that within the first function?
Use a good formatting tool or use an IDE. You will see that you have defined these functions within main function immediately after the switch block ends. The brace that is after the switch block isint the end of main function.
@𝑺𝒐𝒃𝒌𝒂
Can u tell me what method is this?
int err take the returning value of scanf(), which is 1 if conversion is properly. getch() is to handle "\n", when the user press enter The puts() function is very much similar to printf() function. It is used to print the string on the console
Anonymous
Use a good formatting tool or use an IDE. You will see that you have defined these functions within main function immediately after the switch block ends. The brace that is after the switch block isint the end of main function.
Oh okay I solved that. I ended the main function after the switch block by adding a brace, but now I have a new error: implicit declaration of function start(); (line 47)
Anshul
Int primes[ 10000000 ]; long long int N=727282828188282; primes[ 100000 ]=1909372; primes[10000]=188375; If(primes[ 100000 ]*primes[10000]<N) { .... } in this case if the primes(I)* primes (j) exceeds limits of int Then do I need to typecast it to long long
GHAMDAN_NSHWAN
Hello guys
Anshul
Or as N is long long int so it'll not be needed to typecast it to long long
Anshul
I'm not assigning the multiplication to anything, then why do I need to typecast
Anonymous
I'm not assigning the multiplication to anything, then why do I need to typecast
Because you dont want it to overflow. You want the multiplication to be a long long multiplication instead which is why typecasting one of the numbers to long long is required.
Anshul
And also as I need to typecast then should I do this (Long long) (primes (I) * primes(j)) Or this primes (I) * ( long long )primes(j)
Anonymous
And also as I need to typecast then should I do this (Long long) (primes (I) * primes(j)) Or this primes (I) * ( long long )primes(j)
The second one. With the first one, you will still have the same problem of overflow. The integer multiplication will overflow (UB) and that result will be typecast to long long.
Anshul
If I do any int * other int then the product if overflow limit of int then it'll be converted to within the range of int????
Anonymous
Why not the first one
I replied to this in my previous post itself.
Amruta
#include<stdio.h> #include<stdlib.h> struct node{ int data; struct node *next; }*head,*newnode,*q,*prev; void insert(int c){ newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=c; newnode->next=NULL; if(head==NULL){ head=newnode; } else{ q=head; while(q->next!=NULL) q=q->next; q->next=newnode; } } void print(){ q=head; while(q!=NULL){ printf("%d",q->data); q=q->next; } } void reverse() { prev=NULL; q=newnode=head; while(newnode!=NULL) { newnode=newnode->next; q->next=prev; prev=q; q=newnode; } head=prev; } int main(){ head=NULL; int c,ch; do{ printf("\nenter no \t"); scanf("%d",&c); printf("\nenter 1 to continue\t"); scanf("%d",&ch); insert(c); printf("\nyour result\t"); print(); }while(ch); printf("\nenter reverse node\t"); reverse(); print(); }
GHAMDAN_NSHWAN
Send me: Example of the class Please
Anonymous
Can you post the modified version?
https://pastebin.com/yXXY5Mj2
Anonymous
Also, check out this https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function
So I gotta declare my functions before the main function, did I get it right?
Anonymous
If I do any int * other int then the product if overflow limit of int then it'll be converted to within the range of int????
It is Undefined behavior for signed int. For unsigned int, it will be brought within the limits of unsigned int using mod(2^(sizeof(unsigned int)*8))
Anshul
The second one. With the first one, you will still have the same problem of overflow. The integer multiplication will overflow (UB) and that result will be typecast to long long.
If I was assigning it to someone then?? Like assume this Int I=1000000000; Long long j=I*I ; Or I should be doing Long long j =(long long) I* I;
Anonymous
If I was assigning it to someone then?? Like assume this Int I=1000000000; Long long j=I*I ; Or I should be doing Long long j =(long long) I* I;
The first part will still overflow. The multiplication happens before the assignment. So assigning to a long long doesnt matter.
Anshul
Thanks 👍
Anonymous
Any one tell us how to work reflection in java
Anonymous
Any one tell us how to work reflection in java
This is a C/C++ community but since Reflection is being considered for the C++26 standard, I will tell you how it works in Java. Java compiles its code to byte code. While doing it, Java compiler generates a lot of support classes when it compiles your class. These support classes actually help you with reflection support but it is considered to be very slow because reflection queries involve a lot of pointer indirection and string comparisons which all add to overhead at runtime. For ex Java compiler when it is compiling class A will create a helper class MethodsA (not the actual name) which will use something like a hash map which maps the function names and their parameters to a pointer to this method. So when you query a class object at runtime for its methods, the Java compiler will generate the byte codes to call the methods in MethodA class instead. (To be accurate, this is not exactly the case. Actually there are multiple calls made through a hierarchy of classes with A.class (not the same as the class bytecode file generated by javac when it compiles A.java) being the top class). What I have summarized here is just a simplification. But the overall idea is the same.
Kalpak
In this group, who are the experts or teachers of c programming, whoever they are, raise your hand or type yes and send it. I am creating a group .. I add to it .. I need help for the exam ..