ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sorry if it is not the best place to put that topic, but I'm with a doubt about some output of a code that I made in C and run in different distros.
Here is the code:
Code:
#include "stdlib.h"
#include "stdio.h"
#define MALLOC(type, quantity) (type*)malloc(sizeof(type)*quantity);
struct Produto{
int ID;
float price;
int QuantityinStock;
struct Produto *next;
};
struct Produto* CadastrarProduto(struct Produto* new){
printf("\nPut the ID:\n");
scanf("%d", &new->ID);
printf("Put the price:\n");
scanf("%f", &new->price);
printf("Put the quantity in stock:\n");
scanf("%d", &new->QuantityinStock);
new->next=NULL;
printf("\
ID: %d\n\
Price: %f\n\
Stock: %d",\
new->ID, new->price, new->QuantityinStock);
return new;
}
int main (){
struct Produto* produto=MALLOC(struct Produto, 5)
if(produto==NULL){
puts("No memory, closing!!");
exit(1);
}
for (int aux =0; aux <5; aux++) produto = CadastrarProduto(produto + aux);
}
If you run the code, you will see that in the fourth attribution something (a static memory???) went wrong: what i receive? Why that number isnt change after a reboot? Why he cant be modificated?
Code:
./a.out
Put the ID:
1
Put the price:
1
Put the quantity in stock:
1
ID: 1
Price: 1.000000
Stock: 1
Put the ID:
2
Put the price:
2
Put the quantity in stock:
2
ID: 2
Price: 2.000000
Stock: 2
Put the ID:
3
Put the price:
3
Put the quantity in stock:
3
ID: 3
Price: 3.000000
Stock: 3
Put the ID:
4
Put the price:
4
Put the quantity in stock:
4
ID: 544106784 // ??
Price: 4417162884147304202240.000000 // ??
Stock: 4
Put the ID:
5
Put the price:
5
Put the quantity in stock:
5
I noticed too that if i keep the code in portuguese the output changes too:
Code:
...
Informe o codigo do produto:
4
Informe o preco do produto:
4
Informe a quantidade do produto no estoque:
4
Codigo: 1701077348
Preco: 0.000000
Estoque: 4
...
When I was learning C i had been teached that when i try to atribute something to a memory that memory receives it and because of that pointers are too danger if used wrong. So.. What are these memory? Why the changes between the languages??
I know, the code produced an overflow, the function could be void, i started the code thinking to use list instead of arrays... but i'm new on computer science and Linux and cant imagine what is happening in the domain of memory - in the domain of C i understood the problem, i think.
for (int aux =0; aux <5; aux++) produto = CadastrarProduto(produto + aux);
this is not what you need, it is incorrect. That's why your code won't work.
im not interesting in why my C code wont work properly, this is the kind of problem i haved noticed. What im thinking that is strange is the static memory, after all, if I said to a memory to have a value this memory should receive that value, isnt?
Isnt that what i'm doing in the code ? Look the input and the output of the fourth initialization.
The code in portuguese is almost like the same to the code in english, what differ is the adjetives that are bigger than in english (like QuantityinStock = QuantidadenoEstoque). Probably, the compiler see and keep those variables by the name and because of that the memory differ, as I have noticied in the difference in the output in the code by the languages.
Supposing that the problem is a memory i will work with pointer to see what i will discovery. Appears to me that is something stupid, but i dont know what is.
Maybe isnt a question about linux and the system memory manager but about C. So, i think i need to post in Stackoverflow instead.
Quote:
Originally Posted by pan64
Did you try to use a debugger (like ddd)? That will help you to catch it.
Nice, didnt know about DDD. Appears to be a nice debugger.
You don't understand. The unexpected result comes because the code does something else, not what you want. It is not related to "how memory works", that works properly, as expected.
Quote:
if I said to a memory to have a value this memory should receive that value, isnt?
Yes, you are right.
The problem is that your code reads something else .....
im not interesting in why my C code wont work properly, this is the kind of problem i haved noticed. What im thinking that is strange is the static memory, after all, if I said to a memory to have a value this memory should receive that value, isnt?
Isnt that what i'm doing in the code ? Look the input and the output of the fourth initialization.
The code in portuguese is almost like the same to the code in english, what differ is the adjetives that are bigger than in english (like QuantityinStock = QuantidadenoEstoque). Probably, the compiler see and keep those variables by the name and because of that the memory differ, as I have noticied in the difference in the output in the code by the languages.
Supposing that the problem is a memory i will work with pointer to see what i will discovery. Appears to me that is something stupid, but i dont know what is.
Maybe isnt a question about linux and the system memory manager but about C. So, i think i need to post in Stackoverflow instead.
Nice, didnt know about DDD. Appears to be a nice debugger.
I think there's a misunderstanding here. pan64 is absolutely correct, you code is flawed and this is why they are suggesting that you inspect it using a debugger. Or at the very least, you should inspect it using some printf() statements, meaning more printf() statements than you are using to diagnose the memory locations, and your difference of opinion between what you expect to see, and what the resultant code is actually giving you. No one has complained about the variable names, and we appreciate very much you translating the information and your question, however this is not the problem. There's a bug in this code.
Recommend you inspect the exact block of code which pan64 identified for you. Also recommend that you not write that code on one line and instead use brackets to make it multi-line and add some debug output statements there to inspect the variables you are using. I know you've said that you do not wish any critique of your code, however the structure of it is very poor and I've found that when you (the programmer, you, or myself, or anyone is what I mean here) allow yourself to be non-disciplined about good coding practices, you eventually find that you introduce bugs to your code which are more difficult to diagnose and fix, because you've written things in such a quick and dirty manner. Just an opinion there. You also mention static memory a few times here and while the variable aux is a function local, it is not declared as static, the variable producto is allocated from the heap.
As you have been already told you mustn't overwrite 'produto' inside the cycle.
Code:
old: for (aux =0; aux <5; aux++) produto = CadastrarProduto(produto + aux);
new: for (aux =0; aux <5; aux++) CadastrarProduto(produto + aux);
or: struct Produto *lastprod= NULL;
for (aux =0; aux <5; aux++) lastprod= CadastrarProduto(produto + aux);
(I guess later you intend to build an actual list of these objects where the allocation would be moved into 'CadastrarProduto', so it will make sense to keep the return value.
Edit: in the original code, you have written into these elements of the array: [0], [1], [3], [6], [10]. The last two is above the allocated memory, so the result is unpredictable.
I ran the program and I entered your input. I didn't get the large numbers that you got, and it worked the way you intended it to.
that is quite surprising.
What is more interesting, it reproduced exactly the same output (I probably expected not the same, but something similar) - probably the result depends on the compiler too.
What NevemTeve posted will solve this issue, but without OP we cannot go much further.
To OP (and anyone interested): valgrind is the tool to catch this kind of errors and it really found it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.