473,769 Members | 3,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer

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

//WARNING: The function's signature should be: f( int ** )
void f(int *ip) {
static int dummy = 5;
*ip = &dummy;
}

int main(){
int *ip;
f(&ip);
printf("%d",*ip );
return 0;
}

The function's signature should be: f( int ** ). However, f( int *) seems to
produce the same results, even though I am
warned by the compiler (Borland_bcc_5. 5):
Warning W8069 solution.c 5: Nonportable pointer conversion in function f
Warning W8075 solution.c 12: Suspicious pointer conversion in function main

What can go wrong when you pass the address of a pointer to a function that
does not recieve a pointer to a pointer?

TIA
jimjim
Mar 25 '06 #1
16 3165
jimjim wrote:
What can go wrong when you pass
the address of a pointer to a function that
does not recieve a pointer to a pointer?


Whatever might be caused by the function translation
not taking into consideration that you may have
passed in the wrong type.

The philosophy of the newsgroup,
is that it is worth knowing how to write defined code,
and not worth knowing just exactly
what might happen with undefined code.

--
pete
Mar 25 '06 #2
jimjim schrieb:
#include <stdio.h>
#include <stdlib.h>

//WARNING: The function's signature should be: f( int ** )
void f(int *ip) {
static int dummy = 5;
*ip = &dummy;
}

int main(){
int *ip;
f(&ip);
printf("%d",*ip );
return 0;
}

The function's signature should be: f( int ** ). However, f( int *) seems to
produce the same results, even though I am
warned by the compiler (Borland_bcc_5. 5):
Warning W8069 solution.c 5: Nonportable pointer conversion in function f
Warning W8075 solution.c 12: Suspicious pointer conversion in function main

What can go wrong when you pass the address of a pointer to a function that
does not recieve a pointer to a pointer?


The above seems to "work" by pure, dumb luck or bad luck.
It works, if
1) the representation of an int** is the same as for an int*
2) the conversion address->int->address yields the original
address
3) an int* can be stored in an int (this can hold if the size
of an int* is the same as the size of an int).
This is just a rough description, not by the very word of the
standard. This means, it can easily break or sometimes break
if sizeof (int*) != sizeof (int) which is not that unlikely
in the future if you think of 64 bit architectures.

Just do not do that.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 25 '06 #3
On Sat, 25 Mar 2006 11:37:40 GMT, "jimjim" <ne*****@blueyo nder.co.uk>
wrote:
#include <stdio.h>
#include <stdlib.h>

//WARNING: The function's signature should be: f( int ** )
void f(int *ip) {
static int dummy = 5;
*ip = &dummy;
As a result of the faulty calling statement, this statement invokes
undefined behavior. One of the more insidious manifestations of
undefined behavior is to "appear to work as intended." Just bad luck.
Not the worst luck (it could have formatted your hard drive) or good
luck (an easily diagnosed software failure).
}

int main(){
int *ip;
f(&ip);
printf("%d",*i p);
return 0;
}

The function's signature should be: f( int ** ). However, f( int *) seems to
produce the same results, even though I am
warned by the compiler (Borland_bcc_5. 5):
Warning W8069 solution.c 5: Nonportable pointer conversion in function f
Warning W8075 solution.c 12: Suspicious pointer conversion in function main
Ignore the warnings if you want but you really need to understand ALL
the repercussions.

What can go wrong when you pass the address of a pointer to a function that
does not recieve a pointer to a pointer?

Let's just consider your code and a system that requires an int to be
aligned on a four-byte boundary. When you return from f, ip contains
the address 5. Your printf statement says go to address 5 and extract
the value of the int that is there. What will the hardware do when it
attempts to extract an int that is not aligned properly.

What about systems where an int and an address do not share the same
representation. If you code something like
int *ptr;
ptr = (int*)dummy;
the compiler knows you are converting an int to a pointer and can
generate the code to to perform the conversion properly. In your
code, the compiler believes you assigning an int value to an int and
sees no need for any conversion. The value in ip might be a trap
representation (that would be good luck).
Remove del for email
Mar 25 '06 #4
# What can go wrong when you pass the address of a pointer to a function that
# does not recieve a pointer to a pointer?

You can use any type T for int* such that sizeof(T)=sizeo f(int*). If you cast,
you will avoid compiler warnings.

On most implementations sizeof(int)=siz eof(T*) and in the early days of
unix many interfaces were based on that. If you need to do this, and this
is not just stupid C pet tricks, be sure to test to sizeof assumption in
main() start up or in the installer.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Wow. A sailboat.
Mar 26 '06 #5
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> writes:
# What can go wrong when you pass the address of a pointer to a function
# that does not recieve a pointer to a pointer?
The question was posted by "jimjim" <ne*****@blueyo nder.co.uk>.
Please don't snip attributions. And *please* stop using your stupid
'#' quoting character. You've been told repeatedly that it causes
problems for some newsreaders; your stubbornness on this point is why
I usually ignore anything you post.
You can use any type T for int* such that sizeof(T)=sizeo f(int*). If
you cast, you will avoid compiler warnings.

On most implementations sizeof(int)=siz eof(T*) and in the early days of
unix many interfaces were based on that. If you need to do this, and this
is not just stupid C pet tricks, be sure to test to sizeof assumption in
main() start up or in the installer.


Casting to suppress compiler warnings is dangerous and foolish.
Parameters of different types can be passed by different mechanisms,
even if they're the same size. For example, some systems might use
different sets of registers for pointers vs. integers, or integers
vs. floating-point types. If both types are pointers, there's
probably less risk of something going visibly wrong, but the behavior
is still undefined. Don't waste time figuring out how it can go
wrong; just fix the code.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 26 '06 #6
Keith Thompson wrote:
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> writes:
# What can go wrong when you pass the address of a pointer to a
# function that does not recieve a pointer to a pointer?


The question was posted by "jimjim" <ne*****@blueyo nder.co.uk>.
Please don't snip attributions. And *please* stop using your
stupid '#' quoting character. You've been told repeatedly that
it causes problems for some newsreaders; your stubbornness on
this point is why I usually ignore anything you post.


Many of us no longer notice him, we just let the plonk file work.

--
Some informative links:
news:news.annou nce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Mar 26 '06 #7
>> What can go wrong when you pass the address of a pointer to a function
that does not recieve a pointer to a pointer?


The above seems to "work" by pure, dumb luck or bad luck.
It works, if
1) the representation of an int** is the same as for an int*
2) the conversion address->int->address yields the original
address
3) an int* can be stored in an int (this can hold if the size
of an int* is the same as the size of an int).
This is just a rough description, not by the very word of the
standard. This means, it can easily break or sometimes break
if sizeof (int*) != sizeof (int) which is not that unlikely
in the future if you think of 64 bit architectures.

Hi..thx to all for your replies!

As far as your (1) point is concerned, I understand it.
Now, your (2) point hints that theres a relationship btw pointer
representation of an address and int(s). Can you elaborate on that, please?
Regarding your (3) point, how does an int* and an int relates to my code's
use of an int* as an int**? Can you elaborate, please?

TIA
Mar 26 '06 #8
jimjim opined:
What can go wrong when you pass the address of a pointer to a
function that does not recieve a pointer to a pointer?
The above seems to "work" by pure, dumb luck or bad luck.
It works, if
1) the representation of an int** is the same as for an int*
2) the conversion address->int->address yields the original
address
3) an int* can be stored in an int (this can hold if the size
of an int* is the same as the size of an int).
This is just a rough description, not by the very word of the
standard. This means, it can easily break or sometimes break
if sizeof (int*) != sizeof (int) which is not that unlikely
in the future if you think of 64 bit architectures.

Hi..thx to all for your replies!

As far as your (1) point is concerned, I understand it.
Now, your (2) point hints that theres a relationship btw pointer
representation of an address and int(s). Can you elaborate on that,


2) says that your code will work if converting from the pointer to
`int` and back to the pointer, yields the same pointer you started
with. This is going to be implementation-specific, so you can't really
count on that being true.
please? Regarding your (3) point, how does an int* and an int relates
to my code's use of an int* as an int**? Can you elaborate, please?
It would be possible to reply to that, but you did not quote the code
in question, and I can't see your OP. You also snipped attribution
lines (the ones saying "jimjim opined:" or similar), soI don't really
know who you're replying to.
TIA


These have been slated recently, and for good reasons, IMHO.

--
BR, Vladimir

Theory is gray, but the golden tree of life is green.
-- Goethe

Mar 26 '06 #9
Vladimir S. Oka wrote:
jimjim opined:
please? Regarding your (3) point, how does an int* and an int relates
to my code's use of an int* as an int**? Can you elaborate, please?
It would be possible to reply to that, but you did not quote the code
in question, and I can't see your OP. You also snipped attribution
lines (the ones saying "jimjim opined:" or similar), soI don't really
know who you're replying to.


Here's a reconstruction of the thread, as far as I can see:
jimjim wrote:
Michael Mair wrote:
jimjim schrieb:
#include <stdio.h>
#include <stdlib.h>

//WARNING: The function's signature should be: f( int ** )
void f(int *ip) {
static int dummy = 5;
*ip = &dummy;
}

int main(){
int *ip;
f(&ip);
printf("%d",*ip );
return 0;
}

The function's signature should be: f( int ** ). However, f( int *) seems to
produce the same results, even though I am
warned by the compiler (Borland_bcc_5. 5):
Warning W8069 solution.c 5: Nonportable pointer conversion in function f
Warning W8075 solution.c 12: Suspicious pointer conversion in function main

What can go wrong when you pass the address of a pointer to a function that
does not recieve a pointer to a pointer?


The above seems to "work" by pure, dumb luck or bad luck.
It works, if
1) the representation of an int** is the same as for an int*
2) the conversion address->int->address yields the original
address
3) an int* can be stored in an int (this can hold if the size
of an int* is the same as the size of an int).
This is just a rough description, not by the very word of the
standard. This means, it can easily break or sometimes break
if sizeof (int*) != sizeof (int) which is not that unlikely
in the future if you think of 64 bit architectures.

Just do not do that.

Hi..thx to all for your replies!

As far as your (1) point is concerned, I understand it.
Now, your (2) point hints that theres a relationship btw pointer
representation of an address and int(s). Can you elaborate on that, please?
Regarding your (3) point, how does an int* and an int relates to my code's
use of an int* as an int**? Can you elaborate, please?

TIA


Mar 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9376
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
9
5298
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer from integer of different size. Now I thought that when it was a void I could pass anything? Thing is it works when I use an int, but in this case I wanted to use a char. It wouldnt be hard to work around it, but it annoys me because I've heard...
17
3603
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
3
5997
by: kevin | last post by:
This is a repost with an update Does any one know how to pass a function pointer as a function parameter from VB .NET to a C dll? Currently I'm passing it this way Public Delegate Sub DSCUserInterruptFunction()
2
2226
by: mark.e.nelson | last post by:
Hi, I am trying to pass a pointer to a struct to a function that uses the data in the struct, and also happens to use ncurses. I always get a segmentation violation when the program exits. I have experimented with passing a pointer to a struct as an argument to a function that does not use ncurses and that seems to work fine. Can anyone provide any advice? I apologise if this is the wrong place to ask - I am just coming back to C...
15
4661
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
5
2408
by: mshaaban | last post by:
Hello, In my code I have a large static 2D arrays defined as: code: #define LONMAX 1440 #define LATMAX60 480 void main (int argc, char *argv)
0
1401
by: Philip Semanchuk | last post by:
On Oct 22, 2008, at 8:33 PM, Robert Kern wrote: I agree. To deny users of this module access to this param of shmat() would be design arrogance on my part. To do so would be to claim that I know better than everyone who might want to use my module. Using values other than NULL might be unwise (the man page I'm looking at states that clearly) but isn't one of the core design philosophies of Python "we're all consenting adults"?
5
1833
by: miner | last post by:
Hi all I'm doing my first steps in C and I need some help plz. I have 2 structs, a node* and a stack*. I want to write a push() function that accepts to arguments, the node to insert and the pointer to the the top of the stack. This way it works without problems: push(node *theNode, stack *theStack) The call looks like that: push(newNode, newStack) and inside the function I can access the pointer with newStack->head. I tried doing...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10208
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10038
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.