LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-02-2019, 06:40 AM   #31
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869

After 'free' every access to that memory is Undefined Behaviour (UB).
For generic usage, I suggest the following sequence:
Code:
foobarstruct *p= NULL;
...
p= malloc (sizeof *p);
memset (p, 0, sizeof *p);
...
if (p) {
    memset (p, 0xff, sizeof *p);
    free (p);
    p= NULL;
}

Last edited by NevemTeve; 10-02-2019 at 06:43 AM.
 
1 members found this post helpful.
Old 10-02-2019, 11:37 PM   #32
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
While thank you NevemTeve for your response, and while that gives me a little more info; I'm still not clear about the questions I asked in post #30. So if anyone could enlighten me, I'd be grateful.
 
Old 10-03-2019, 01:48 AM   #33
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
It would be clearer if you re-stated your question (with sourcecode), but this time using meaningful names instead of variations of 'something'.
 
1 members found this post helpful.
Old 10-03-2019, 08:36 AM   #34
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Quote:
Originally Posted by NevemTeve View Post
It would be clearer if you re-stated your question (with sourcecode), but this time using meaningful names instead of variations of 'something'.
I'm sorry NevemTeve, and I don't mean to rude or difficult but; I don't know how to ask what I've asked any clearer than what I did in post #30. There is three questions there, two of which are about the same line of code. That same code has already been posted in full in post #25, and the first two questions also apply to the original code Hazel posted in post #21. The whole point of that program was so I could see how one would use the wrong pointer, and therefore get the wrong results and/or errors where gcc wouldn't necessarily flag it as an error. And with all respect, I would have thought that while it would (and is where pointers are concerned - particularly with structures in the mix as well) be confusing to me, someone like yourself, with the experience you have with C (which is much more than what I have), some similarly named variables wouldn't mean it would be unclear to you what I'm trying to ask. My third question is not in relation to any code posted here either, it's a general question that does have an answer, an answer I tried looking up, and that is once again just not clear to me.

So again, and with all respect; I don't see how restating the questions I've asked, when I tried to ask them as clearly as I possibly could, along with re-posting the same code that's already been posted in full (twice now already), is going to make my questions any clearer. Like I said, I honestly tried to be as clear as I could be, I don't know how to ask them any clearer, the code I'm referring to is once again already posted in full. So I guess maybe one day I'll know, maybe I won't, as I'm still unclear :sigh:
 
Old 10-03-2019, 08:42 AM   #35
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
With the same effort you could have re-posted the code in question, with meaningful names. No pressure though, do it your way.
 
Old 10-03-2019, 08:57 AM   #36
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Well I guess I'll never know, I don't know why I even bloody bother...
 
Old 10-03-2019, 12:55 PM   #37
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
@jsbjsb001 - I have tried, somewhat unsuccessfully, to catch up with this thread and reread your post #30, and the difficulty for others is that instead of stating a clear, self contained question with minimal example code, it refers to "something = *somethingPtr; under the second call to malloc()", "the first call to malloc()", and "the Wikipedia article I linked in post #27".

It is very difficult for others to understand the question being asked when they must piece together the context from other posts and links to other sites, particularly when the code you are referencing has been changed.

Although you have the whole context in mind for each post and each question, others do not and must rebuild it with each glimpse provided by your own posts at disjoint times.

The difficulty is compounded by the scope of the thread being "Some questions", instead of "A question", which makes for a potentially wide ranging discussion.

So for this thread, please try to contain the questions to single well defined code examples which illustrate a single test case, and which can be confidently referenced by yourself and others in subsequent discussion.

Your skills have developed to the point that it is probably time to transition to asking more specific questions in a single thread, more easily followed, and opening additional threads for additional, well formed questions. That will help you further organize and understand the concepts in depth, and will help others to follow each thread they choose to participate in and improve the usefulness of all posts.

All effort to this end appreciated!
 
2 members found this post helpful.
Old 10-03-2019, 05:21 PM   #38
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by jsbjsb001 View Post
I don't know how to ask what I've asked any clearer than what I did in post #30
Ouch, really? I've tried to read that several times, and I just get completely lost after the first sentence every time.
 
Old 10-04-2019, 06:08 AM   #39
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
I get what you're saying astrogeek, so I may as well just mark this thread as solved, as I can't think of a clearer way to ask what I'm trying to ask, sorry.

Quote:
Originally Posted by ntubski View Post
Ouch, really? I've tried to read that several times, and I just get completely lost after the first sentence every time.
With all respect ntubski, not sure what's with the "Ouch, really?" comment. So not sure how I'm supposed to take that. I'm sorry what I asked didn't make sense to you. I'm not honestly lying when I said I don't know how else to ask, as the only other way I can think of to ask wouldn't make any more sense, and likely even less sense for that matter.

In any case, it wasn't my intention to offend/upset anyone, or appear ungrateful for anyone's help. And I don't wish to argue with anyone either, I didn't start this thread to do any of that. I certainly don't come to LQ to argue with people, I can think of far better places to do that than here. I also don't wish to cause any extra work for the mod's/LQ either, so like I said above, I think I'll just mark this thread as solved and if it makes sense one day it does, if it doesn't, it doesn't.

Thanks for all of the help.
 
Old 10-04-2019, 10:51 AM   #40
Katemonster
LQ Newbie
 
Registered: Aug 2019
Posts: 12

Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
I think you're confusing yourself with the subtle differences between declaring the variable and assigning the variable outside of a declaration statement.
Code:
int val, *ptr = &val;
Is NOT the equivalent of:
Code:
int val, *ptr;

*ptr = &val;
NOT AT ALL. What is the equivalent is:
Code:
int val, *ptr;

ptr = &val;
Actually that should be correct, from what I understand he's writing the equivalent of:
Code:
int val;
int *ptr = &val;
What's wrong is your second snippet.
Code:
int val, *ptr;

*ptr = &val;
*ptr means to dereference ptr. You're attempting to set the dereferenced value (type int) to the address of val. ptr is uninitialized so this will likely crash. You meant to write "ptr = &val;".

Last edited by Katemonster; 10-04-2019 at 09:00 PM.
 
1 members found this post helpful.
Old 10-04-2019, 11:14 AM   #41
Katemonster
LQ Newbie
 
Registered: Aug 2019
Posts: 12

Rep: Reputation: Disabled
Quote:
Originally Posted by jsbjsb001 View Post
Sorry for my late reply. I've been playing around with the example Hazel posted (I added a few things to what Hazel originally posted above). While I think I get Hazel's point now, I have a couple of questions; I can't figure out why the line below highlighted in bold is there, as it makes no difference if I comment it out or leave it there uncommented, but it does make a difference if I comment out the same lines in the second and third calls to malloc() - which is what I think Hazel's point was.

Also, as you'll see in my output of that code below the code itself at the bottom of this post; after the second malloc() call somethingPtr prints nil (which I assume is because that pointer was freed with the free() function), but after the third malloc() call, it gets an address. Also, am I right in assuming that after the second and third malloc() calls, if I comment out the line highlighted in bold below, that memory still gets assigned to the "something" structure, just not dynamically with malloc() ?

Code:
#include <stdio.h>
#include <stdlib.h>

int main(void) {

    typedef struct {
               short int a;
               short int b;
               short int c;
    } test;

    typedef struct {
	       float a;
	       float b;
	       float c;          
    } test1;	       

    test something;
  //  test1 somethingelse;
    test *somethingPtr;
    test1 *something_pointer;
    
    printf("somethingPtr = %p\n", somethingPtr);
    printf("something_pointer = %p\n\n", something_pointer);
            
    puts("first malloc call\n"); //This reserves space but doesn't typecast it

    somethingPtr = malloc(sizeof(test));
    something = *somethingPtr;
         
    something.a = 100;
    something.b = 200;
    something.c = 300;
    
    printf("somethingPtr = %p\n", somethingPtr);
    printf("something_pointer = %p\n\n", something_pointer);
    
    printf("something.a = %i\n", something.a);
    printf("something.b = %i\n", something.b);
    printf("something.c = %i\n", something.c);

    free(somethingPtr);
    
    puts("\nsecond malloc call\n");

    something_pointer = malloc(sizeof(test)); //Ditto with wrong pointer
    something = *somethingPtr;
                
   // somethingelse.a = 471.20f;
    //somethingelse.b = 474.35f;
   // somethingelse.c = 36.66f;
    
    printf("somethingPtr = %p\n", *somethingPtr);
    printf("something_pointer = %p\n\n", *something_pointer);
                
    printf("something.a = %i\n", something.a);
    printf("something.b = %i\n", something.b);
    printf("something.c = %i\n\n", something.c);
   // printf("somethingelse.a = %.2f\n", somethingelse.a);
  //  printf("somethingelse.b = %.2f\n", somethingelse.b);
  //  printf("somethingelse.c = %.2f\n", somethingelse.c);
    
    free(something_pointer);
   
    puts("\nthird malloc call\n");

    something_pointer = (test *)malloc(sizeof(test)); //with typecast
    something = *somethingPtr;
   //somethingelse = *something_pointer;
      
   printf("somethingPtr = %p\n", somethingPtr);
    printf("something_pointer = %p\n\n", something_pointer);
   
    printf("something.a = %i\n", something.a);
    printf("something.b = %i\n", something.b);
    printf("something.c = %i\n", something.c);
    
    free(something_pointer);

    return 0;
}
Code:
james@jamespc: practice> ./malloc_test                      
somethingPtr = (nil)
something_pointer = 0x7ffc942eba60

first malloc call

somethingPtr = 0xfa1670
something_pointer = 0x7ffc942eba60

something.a = 100
something.b = 200
something.c = 300

second malloc call

somethingPtr = (nil)
something_pointer = 0xfa1260

something.a = 0
something.b = 0
something.c = 0


third malloc call

somethingPtr = 0xfa1670
something_pointer = 0xfa1670

something.a = 0
something.b = 0
something.c = 0
In regards to the bolded statement. It's copying the value of dereferenced somethingPtr (struct test) to something, so it has no effect on the memory you've allocated for somethingPtr. You're filling the bytes with whatever happens to be in the memory you've allocated with malloc. For somethingPtr there happens to be zeroes and you're copying those zeroes to something with the bolded statement.

After the second malloc, your printf isn't printing the addresses of somethingPtr and something_pointer, you're dereferencing them, so it's printing the value. The value could be anything at this point: you've never set them. This is why it prints nil, all the memory in somethingPtr is filled with zeroes
 
1 members found this post helpful.
Old 10-04-2019, 11:41 AM   #42
Katemonster
LQ Newbie
 
Registered: Aug 2019
Posts: 12

Rep: Reputation: Disabled
Quote:
Originally Posted by jsbjsb001 View Post
Basically if I comment out the line below highlighted in bold (and same for the third malloc() call),

Code:
puts("\nsecond malloc call\n");

    something_pointer = malloc(sizeof(test)); //Ditto with wrong pointer
    something = *somethingPtr;
it will give me the correct values for my "something" structure like this;

Code:
something.a = 100
something.b = 200
something.c = 300
bearing in mind that somethingPtr has already been freed by the free() function. So I'm wondering if I do that, that my "something" structure isn't therefore dynamically allocated - because the something_pointer pointer is pointing to a different structure, and not my "something" structure?

Also, and while this maybe a little off-topic; I've been trying to understand what a "symbol" actually means in the programming context, but I'm not sure I do. I tried reading the Wikipedia article here about it, but it doesn't make any sense to me. Does anyone have a simple explanation with a simple example? I'd be really grateful - I did try looking at other hits from Google, but it just confused me even more.
Your something structure is not dynamically allocated. It's allocated "on the stack" as we say. When you enter your main, the memory for everything you've declared without an asterisk is automatically allocated. Your code doesn't allocate or deallocate any of something's memory. You are merely copying the memory from where you allocated it, in somethingPtr and something_pointer, to the memory of something.
 
1 members found this post helpful.
Old 10-05-2019, 05:11 AM   #43
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Many thanks Katemonster, other than my "symbol question", your posts as far as I see see explain and answer what I was wondering with my first two questions you've replied to. Thanks a lot again!

I would be tempted to say I'll mark the thread as [SOLVED] now, but since I already have, saying that would be rather moot

Thanks again for your posts!

PS: I do get the point GazL was making about using proper variable names, and I wasn't ignoring that advice either - which was why I marked their post as "helpful" before.

Last edited by jsbjsb001; 10-05-2019 at 06:09 AM. Reason: addition and forgot "to"
 
  


Reply

Tags
c programming, pointers, structures



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
Need help with pointers to structures schmitta Programming 2 01-21-2014 12:33 PM
[SOLVED] array of structures in c, and function pointers. I need a structured approach. karinier Linux - Newbie 1 08-31-2013 01:16 PM
[SOLVED] Could I get some pointers on pointers please? theKbStockpiler Programming 46 05-18-2010 12:30 AM
Pointers Pointers Pointers urzumph Programming 9 03-11-2004 09:49 AM

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

All times are GMT -5. The time now is 12:43 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