LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2019, 01:12 PM   #1
Myphre
LQ Newbie
 
Registered: Oct 2018
Posts: 21

Rep: Reputation: Disabled
Question Doubt - Memory, Linux, Output of a C code (?)


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.


Sorry for my english, i'm from brazil.
 
Old 06-24-2019, 01:20 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
Code:
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.

Did you try to use a debugger (like ddd)? That will help you to catch it.

I have no idea how could this code print in Portuguese.
 
1 members found this post helpful.
Old 07-02-2019, 09:20 PM   #3
Myphre
LQ Newbie
 
Registered: Oct 2018
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Code:
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 View Post
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.
 
Old 07-03-2019, 01:58 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
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 .....
 
1 members found this post helpful.
Old 07-03-2019, 06:44 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Myphre View Post
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.

Last edited by rtmistler; 07-03-2019 at 06:51 AM.
 
Old 07-03-2019, 06:45 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 07-03-2019, 07:08 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
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.

Last edited by NevemTeve; 07-03-2019 at 11:20 AM.
 
Old 07-03-2019, 11:57 AM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,200

Rep: Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307
FWIW:

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.

Last edited by dugan; 07-03-2019 at 12:08 PM.
 
Old 07-03-2019, 01:33 PM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
Quote:
Originally Posted by dugan View Post
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.

Last edited by pan64; 07-03-2019 at 01:37 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Doubt - Memory, Linux, Output of a C code (?) Myphre Linux - Newbie 1 06-04-2019 10:49 PM
Doubt in the output when modules dependency builded using busybox script depmod.pl ayyasprings Linux - Embedded & Single-board computer 2 09-04-2014 02:58 AM
Memory Size doubt karlochacon Linux - Newbie 7 03-19-2010 08:46 AM
Doubt with shared memory (IPC). webquinty Linux - Newbie 0 11-05-2008 10:51 AM
Doubt about how a linker builds a memory image funkymunky Programming 4 02-18-2005 11:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration