LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-27-2019, 10:09 AM   #16
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930

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;
 
Old 09-27-2019, 10:26 AM   #17
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,786

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
what is really strange here: int is a type (variable name is val) and int * is another type, variable name is ptr.
 
Old 09-28-2019, 12:05 AM   #18
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'm sorry Hazel, but I'm just not clear about what you're saying in post #15. I tried to write something to do what you said in post #15, but it either works anyway, or I just get a stack of error messages if I uncomment the calls to free(). While I've posted the code I wrote below; would you be able to give a full example of what you're saying so I can actually see exactly what you mean? Because I just can't visualize what you're saying properly.

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;
Yes RT, but that's what is confusing me, in that; my pointer is declared as an int, and I'm trying to point it to an int. So would it be right to say; if I include the asterisk like "*ptr1 = &v1", then that's saying put the address of v1 into v1's value? But if I write "ptr1 = &v1" it's saying put the address of v1 into ptr1's value?

Here's my test case for trying to pick the wrong data type re post #15;

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

int main(void) {

    int *somethingPtr;
    char *something_pointer;

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

    struct test something;

    something.a = 1;
    something.b = 2;
    something.c = 3;

    puts("first malloc call");

    somethingPtr = malloc(sizeof(something));

    printf("something.a = %i\n", something.a);
    printf("something.b = %i\n", something.b);
    printf("something.c = %i\n",something.c);

    //free(something);

    something_pointer = malloc(sizeof(something));

    puts("second malloc call"); 

    printf("something.a = %i\n", something.a);
    printf("something.b = %i\n", something.b);
    printf("something.c = %i\n",something.c);

  // free(something);

   return 0;
}
 
Old 09-28-2019, 01:58 AM   #19
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You're not using 'somethingPtr' at all, so the malloc is pointless too.
 
Old 09-28-2019, 05:40 AM   #20
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
You're not using 'somethingPtr' at all, so the malloc is pointless too.
Beyond confirming what I was suspecting anyway; this isn't helpful with all respect. As I'm still not sure how I'm supposed to write something so I can see what Hazel means by declaring the wrong pointer type, and gcc still compiling it anyway - but having a program that crashes because of that. That's what I was trying to do, so I can understand and SEE what Hazel was talking about in post #15.
 
Old 09-28-2019, 05:44 AM   #21
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,559
Blog Entries: 19

Rep: Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445
OK, here is an amended version of your program. I perhaps did not make it clear in post 15 that the problem referred to only raises its head when both types are structures. If one is a structure and one an integer (as in your program) gcc can see it doesn't make sense and just refuses to compile it. As you have already noticed. So I have defined both types of variable involved to be structures. I have used typedef statements to do it to avoid having to repeat the word "struct" all the time.
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void) {


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

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

    test something;
    test *somethingPtr;
    test1 *something_pointer;


    puts("first malloc call"); //This reserves space but doesn't typecast it

    somethingPtr = malloc(sizeof(test));
    something = *somethingPtr;
 
    something.a = 100;
    something.b = 200;
    something.c = 300;

    printf("something.a = %i\n", something.a);
    printf("something.b = %i\n", something.b);
    printf("something.c = %i\n", something.c);

    free(somethingPtr);

    something_pointer = malloc(sizeof(test)); //Ditto with wrong pointer
    something = *somethingPtr;

    puts("second malloc call"); 

    printf("something.a = %i\n", something.a);
    printf("something.b = %i\n", something.b);
    printf("something.c = %i\n", something.c);

   free(something_pointer);


   something_pointer = (test *)malloc(sizeof(test)); //with typecast
    something = *somethingPtr;

   puts("third malloc call");

    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;
}
Now if you compile that, you will notice that the third malloc call gives you a warning and that both the second and the third give you wrong answers.
 
2 members found this post helpful.
Old 09-28-2019, 06:41 AM   #22
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,895

Rep: Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015
Small suggestion. When you're trying to do these little test programs to aid in your understanding, don't use confusingly similar names like 'something_pointer' and 'somethingPtr' in the same program. It's very hard to keep that sort of thing straight in your head. Something like 'wrongTypePointer' would have been far more appropriate in this particular example.

Think about your variable names, and use things that actually help explain the code better.

Last edited by GazL; 09-28-2019 at 06:43 AM.
 
3 members found this post helpful.
Old 09-28-2019, 07:14 AM   #23
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,559
Blog Entries: 19

Rep: Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445
Quote:
Originally Posted by GazL View Post
Small suggestion. When you're trying to do these little test programs to aid in your understanding, don't use confusingly similar names like 'something_pointer' and 'somethingPtr' in the same program. It's very hard to keep that sort of thing straight in your head. Something like 'wrongTypePointer' would have been far more appropriate in this particular example.

Think about your variable names, and use things that actually help explain the code better.
That was actually the point of the example! In my post 15, I recommended explicit typecasting to prevent the result of a malloc call being accidentally assigned to the wrong variable. I have actually done that in the past. jsbjsb was only trying to reconstruct the process by which that could happen so don't blame him.
 
1 members found this post helpful.
Old 09-28-2019, 07:50 AM   #24
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,895

Rep: Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015
Yes, I know. No blame implied.

Simply making a suggestion with regard to naming.


P.S. I'm currently teaching myself C++ so I'm doing a lot of these little 'how the hell does this work!' programs myself.

Last edited by GazL; 09-28-2019 at 07:54 AM.
 
Old 10-01-2019, 07:05 AM   #25
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
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
 
Old 10-01-2019, 07:49 AM   #26
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,786

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Quote:
Originally Posted by jsbjsb001 View Post
Sorry for my late reply.
no need to say sorry, this is your thread. It is completely ok.

Quote:
Originally Posted by jsbjsb001 View Post
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.

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

    something_pointer = (test *)malloc(sizeof(test)); //with typecast
    something = *somethingPtr;
No, here something_pointer is initialized, somethingPtr is still nil, that is ok.

Quote:
Originally Posted by jsbjsb001 View Post
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() ?
I do not really understand what do you mean by that
 
1 members found this post helpful.
Old 10-01-2019, 08:05 AM   #27
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
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.
 
Old 10-01-2019, 09:04 AM   #28
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,786

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
if you mean "variable" (as symbol): for me a variable in c is just a pointer.
When you write int v=5 v itself will point to a memory location, where the value (5) is actually stored. &v will tell you this location.
when you write int *ptr; *ptr = 5 the variable ptr itself will point to a location, which holds the address of another location (where the 5 actually stored).
So a variable itself is a pointer and the type of the variable is actually specifies what is stored on that location.
Obviously you can say "address of" instead of pointer, if it is better for you.

I hope it helps and won't confuse you...
 
Old 10-01-2019, 10:50 AM   #29
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,559
Blog Entries: 19

Rep: Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445
Your structure "something" can be given its value in two ways. You can declare a struct variable of that type and call it "something", in which case space will be reserved for it on the stack. Or you can declare a pointer of that type, reserve space for the structure on the heap by using malloc and store the address in your pointer. But if you use the second method and you want to give a set of field values to "something" explicitly, you need to explain to the compiler that "something" is your name for the structure the pointer points to. That's what the line you query does; it dereferences the pointer and specifies that the resultant data area is to be called "something".
 
Old 10-02-2019, 04:52 AM   #30
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
This is basically my question; since the "something" structure is freed with free() under the first malloc() call, if I comment out something = *somethingPtr; under the second call to malloc(), then I still do get the correct values printed again for a second time. So since the dynamically allocated memory had therefore been cleared after the first malloc() call, and after it prints the values of the "something" structure's member variables the first time; Do I get the correct values because the "something" structure is therefore created on the stack? This is what just isn't making sense to me. I'm also still not clear on why the first line I mentioned above is there under the first call to malloc() - given it makes no difference if I comment it out or leave it uncommented. I get that if I don't use dynamic memory allocation (like malloc) that it gets put on the stack, but if I do dynamically allocate it, be that a structure or whatever else, it gets put on to the heap. But that's about the only thing that's clear to me though.

Also, is the Wikipedia article I linked in post #27 above, that talks about the meaning of the word "symbol" in the programming context, saying that the word "symbol" in the programming context means a data type, like int, char, or whatever other data type? Because I'm still not clear about that.
 
  


Reply

Tags
c programming, pointers, structures


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
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 06:30 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