LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-17-2014, 10:09 AM   #16
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10

Quote:
Originally Posted by psionl0 View Post
Hmmm, it compiles just fine when I do it. Why don't you show us what you wrote? There is bound to be some typos there.

p = malloc(sizeof(float)); reserves enough memory in the heap to store a floating point number and stores the address of that memory in p.

*p = (float) y; casts (converts) y into a floating point number and stores the result in the memory location pointed to by p.
doesn't the pointer variable p hold the right amount of memory for a float? I'm still trying to figure out pointers but logic tells me that the malloc statement is redundant
 
Old 08-17-2014, 10:15 AM   #17
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by mhogomchungu View Post
p = (float*)y;

The above line reads the value of y and then casts it to (float *) and then writes the result to p.
It is equivalent to:

p = (float*)120;

and i dont think thats what you meant,you probably meant to do:

p = (float*)&y;

The above reads the memory address of variable y and then casts it (float*) and then writes the result to p.
This is probably what you wanted.Use this and it will not segfault anymore.
This makes sense to me but doesn' tdo as expected. Here is the program w/ the output
Code:
#include <stdio.h>
#include <stdlib.h>

int
main()
{
    float *p;
    //void *p;
    int y = 120;
    //void *x;

    //x = &y;
    //p = malloc(sizeof(float));
    p = (float*)&y;
    printf("p is %f\n", *p);

    return 0;
}
The output is this:
Code:
p is 0.000000
 
Old 08-17-2014, 11:25 AM   #18
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by doughyi8u View Post
compiling this gives the following error:
test1.c:13:26: error: invalid conversion from ‘void*’ to ‘float*’ [-fpermissive]
The main difference between our codes is that I included <malloc.h> instead of <stdlib.h>

Quote:
Originally Posted by doughyi8u View Post
doesn't the pointer variable p hold the right amount of memory for a float? I'm still trying to figure out pointers but logic tells me that the malloc statement is redundant
No, p is just a pointer and doesn't hold any memory (other than that required to store a memory address). It has to be initialized to point to a valid memory address before it can be de-referenced. malloc() reserves memory for p to point to.

As mhogomchungu pointed out, p can share the same memory address as y but the bit patterns for integer format and float format are different. If 120 (0x78) is read by a program that interprets it as a floating representation of a number then it will see something with a value of the order of 2^-128 which the printf statement would round off to zero.
 
Old 08-17-2014, 11:33 AM   #19
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Original Poster
Rep: Reputation: 10
that helps a lot. I'm still a little confused though. How would I make a float print what was typecasted from an integer. i.e:
Code:
#include <stdio.h>
#include <stdlib.h>

int
main()
{
    int i, *j;
    float k;
    float *fp;
    void *vp;

    i = 40;

    fp = (float*)&i;
    printf("%f\n", (float)*fp);

    return 0;
}
I want the variable "fp" to print the value copied from the variable "i". Is there another solution?
 
Old 08-17-2014, 11:38 AM   #20
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Try
Code:
   k = (float) i;
   fp = &k;
   printf("%f\n", *fp); // *fp is already float so type conversion not required
 
Old 08-17-2014, 12:28 PM   #21
mhogomchungu
LQ Newbie
 
Registered: Mar 2014
Posts: 20

Rep: Reputation: Disabled
Quote:
Originally Posted by doughyi8u View Post
[code]
compiling this gives the following error:
test1.c:13:26: error: invalid conversion from ‘void*’ to ‘float*’ [-fpermissive]
C language can convert a pointer of type (void*) to a pointer of any other type.

C++ can not and this is one of the differences between C and C++.

What you wrote is a valid C code but invalid C++ code.

You got the error because you are compiling your code using a C++ compiler.

The error will go away if:
1. You compile using a C compiler.
2. You continue using a C++ compiler but cast malloc() to a pointer type of resulting object.

Last edited by mhogomchungu; 08-17-2014 at 12:35 PM.
 
Old 08-17-2014, 12:58 PM   #22
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by doughyi8u View Post
I want the variable "fp" to print the value copied from the variable "i". Is there another solution?
You are not making a copy of the value. You have declared an int object on the stack, and assigned the value at such location to 40.

You then initialize the float pointer to reference the address of where this int object is stored.

Finally, you attempt to treat the value at this location as a IEEE 754 floating-point value when you call printf(). Perhaps not obvious to you, but the value of 40 in IEEE 754 format is not going to yield the number 40.0.

In conclusion, what you are doing is silly. Yes, you may be learning something on your journey to become a proficient C/C++ developer, but seriously, there is no practical application to what you are attempting to accomplish.

Last edited by dwhitney67; 08-17-2014 at 01:01 PM.
 
Old 08-17-2014, 01:00 PM   #23
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by mhogomchungu View Post
C language can convert a pointer of type (void*) to a pointer of any other type.

C++ can not and this is one of the differences between C and C++.

What you wrote is a valid C code but invalid C++ code.

You got the error because you are compiling your code using a C++ compiler.

The error will go away if:
1. You compile using a C compiler.
2. You continue using a C++ compiler but cast malloc() to a pointer type of resulting object.
Not entirely true; in C++, you can cast a void* to a pointer of another pointer type. You can use a C-style cast, or rely on reinterpret_cast.

In C++, one should NEVER use malloc(). Use the new operator instead.
 
Old 08-17-2014, 01:12 PM   #24
mhogomchungu
LQ Newbie
 
Registered: Mar 2014
Posts: 20

Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
Not entirely true; in C++, you can cast a void* to a pointer of another pointer type. You can use a C-style cast, or rely on reinterpret_cast.
To be precise with my wording.

C language implicitly converts a pointer of type (void*) to a pointer of any other type,no casting is necessary.
C++ requires explicit casting from (void*) to a pointer of any other type,casting is necessary.

The code is valid C and invalid C++ because it expects the casting to be performed implicitly,something C will do but C++ will not.

Last edited by mhogomchungu; 08-17-2014 at 01:14 PM.
 
Old 08-18-2014, 12:32 AM   #25
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
It would be great if the OP told whan they actually wanted to achieve with this. Maybe nothing more than getting invalid page-faults -- that suceeed all right.
 
Old 08-18-2014, 12:49 AM   #26
mhogomchungu
LQ Newbie
 
Registered: Mar 2014
Posts: 20

Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
It would be great if the OP told whan they actually wanted to achieve with this. Maybe nothing more than getting invalid page-faults -- that suceeed all right.
They gave their reason here[1],so its an academic exercise aimed at getting a better understanding of how pointers work.

[1] http://www.linuxquestions.org/questi...0/#post5222197
 
Old 08-18-2014, 12:52 AM   #27
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by NevemTeve View Post
It would be great if the OP told whan they actually wanted to achieve with this. Maybe nothing more than getting invalid page-faults -- that suceeed all right.
The OP already stated what he was trying to achieve:
Quote:
Originally Posted by doughyi8u View Post
I'm trying to *fully* understand pointers
Pointers are the most powerful aspect of C/C++ programming. In fact, languages like Java evolved specifically to reduce the programmer's power in this regard (it's hard to write viruses in Java).

However, getting a grip on the nature of pointers is not an easy thing to do. It doesn't matter much if the exercise is pointless. If it helps the OP in his objective then more power to him.

Last edited by psionl0; 08-18-2014 at 12:53 AM.
 
Old 08-18-2014, 01:35 AM   #28
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Well, if assigning random literals to pointers and getting invalid page-faults is a way towards fully understanding pointers, then the mission is complete.
Other (perhaps more fruitful) possibility would be mastering data-structures like lists and trees.
 
Old 08-18-2014, 03:27 AM   #29
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by NevemTeve View Post
Well, if assigning random literals to pointers and getting invalid page-faults is a way towards fully understanding pointers, then the mission is complete.
Other (perhaps more fruitful) possibility would be mastering data-structures like lists and trees.
Somebody who has yet to understand the role of malloc() is not ready to move on to linked lists.
 
Old 08-18-2014, 06:06 AM   #30
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
The first problem I found in the very first post: what should (float *)120 mean? Where did this number come?
 
  


Reply



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
C++ pointer cast from object to function PatrickNew Programming 27 08-25-2008 01:30 PM
pointer cast c++ santana Programming 10 10-19-2007 07:55 AM
makes pointer from integer without a cast ? hubabuba Programming 2 01-28-2005 05:28 PM
pointer from integer without a cast bcf2 Programming 7 12-30-2004 02:04 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM

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

All times are GMT -5. The time now is 12:55 AM.

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