[SOLVED] Some questions about C structures and pointers
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.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
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.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Quote:
Originally Posted by NevemTeve
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:
@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.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
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
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.
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.
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() ?
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
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.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.