I'm trying to write a function that does some realloc-style
modification of a pointer. The function itself works, but I'm having
some problems with the prototype. This is a simple example of the sort
of thing I want to do (not the complete code, that works but is
technically UB).
#include <stdlib.h>
void indirectmalloc(void** ptr, size_t s)
{
*ptr=malloc(s);
return;
}
int main(void)
{
char* a;
int* b;
indirectmalloc(&a);
indirectmalloc(&b);
free(a);
free(b);
return 0;
}
but although all pointers are freely castable to and from void*, not
all pointers-to-pointers are freely castable to and from void**, so
this is UB.
One compiler I use allows &(void*)a, but this is nonstandard. Is there
a portable way of doing this without relying on temporary void*s?
Should I use a #define macro instead? Although there are obviously
easier ways of writing the above code, I'd like to do something like
this (my actual code is more complicated; I'm not posting it because it
works, just invokes UB which happens to not be a problem on my
compiler). 6 1819
ais523 wrote: ais523 wrote:
I'm trying to write a function that does some realloc-style modification of a pointer. The function itself works, but I'm having some problems with the prototype. This is a simple example of the sort of thing I want to do (not the complete code, that works but is technically UB).
It's in the FAQ http://c-faq.com/ptrs/genericpp.html
If you want to do realloc type stuff, do it the way realloc does it.
#include <stdlib.h>
void indirectmalloc(void** ptr, size_t s) { *ptr=malloc(s); return; }
Why not return the pointer as malloc does? There is no need in this
example to pass in a pointer to a pointer.
int main(void) { char* a; int* b; #if 0 indirectmalloc(&a); indirectmalloc(&b); Sorry, that should have been #endif indirectmalloc(&a, sizeof *a); indirectmalloc(&b, sizeof *b);
This is why you should always copy and paste the code you actually
compile from your editor. free(a); free(b); return 0; }
but although all pointers are freely castable to and from void*, not all pointers-to-pointers are freely castable to and from void**, so this is UB. One compiler I use allows &(void*)a, but this is nonstandard. Is there a portable way of doing this without relying on temporary void*s?
No.
Should I use a #define macro instead? Although there are obviously easier ways of writing the above code, I'd like to do something like this (my actual code is more complicated; I'm not posting it because it works, just invokes UB which happens to not be a problem on my compiler).
Change the code so it takes a void* (if it needs one which your
indirectmalloc does not) and returns a void*, then the problem goes
away. realloc was written the way it was for a reason!
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc
On Thu, 13 Apr 2006 08:36:09 -0700, ais523 wrote: ais523 wrote:
I'm trying to write a function that does some realloc-style modification of a pointer. The function itself works, but I'm having some problems with the prototype. This is a simple example of the sort of thing I want to do (not the complete code, that works but is technically UB).
#include <stdlib.h>
void indirectmalloc(void** ptr, size_t s) { *ptr=malloc(s); return; }
int main(void) { char* a; int* b; #if 0 indirectmalloc(&a); indirectmalloc(&b); Sorry, that should have been #endif indirectmalloc(&a, sizeof *a); indirectmalloc(&b, sizeof *b); free(a); free(b); return 0; }
but although all pointers are freely castable to and from void*, not all pointers-to-pointers are freely castable to and from void**, so this is UB. One compiler I use allows &(void*)a, but this is nonstandard. Is there a portable way of doing this without relying on temporary void*s? Should I use a #define macro instead? Although there are obviously easier ways of writing the above code, I'd like to do something like this (my actual code is more complicated; I'm not posting it because it works, just invokes UB which happens to not be a problem on my compiler).
Why not pass the pointer to the function and return the new one
eg
void* indirectmalloc(void* ptr, size_t s)
{
return malloc(s);
}
and call it as in
b = indirectmalloc( b, sizeof *b);
Duncan
In article <pa****************************@this.address>,
Duncan Muirhead <no***@this.address> wrote: Why not pass the pointer to the function and return the new one eg void* indirectmalloc(void* ptr, size_t s) { return malloc(s); } and call it as in b = indirectmalloc( b, sizeof *b);
What is the purpose of the first parameter there? You never use it
in indirectmalloc() and never will in that form.
If you drop it from the function definition and the call, then you
essentially just end up with a useless wrapper around malloc() -- one
which would certainly not seem to be in keeping with the spirit of
naming the function "indirectmalloc".
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
On Thu, 13 Apr 2006 21:21:13 +0000, Walter Roberson wrote: In article <pa****************************@this.address>, Duncan Muirhead <no***@this.address> wrote:
Why not pass the pointer to the function and return the new one eg void* indirectmalloc(void* ptr, size_t s) { return malloc(s); } and call it as in b = indirectmalloc( b, sizeof *b);
What is the purpose of the first parameter there? You never use it in indirectmalloc() and never will in that form.
If you drop it from the function definition and the call, then you essentially just end up with a useless wrapper around malloc() -- one which would certainly not seem to be in keeping with the spirit of naming the function "indirectmalloc".
My assumption was that in the OP's real code there was other stuff going
on in indirectmalloc -- it was stated that this was not the complete
code.
Flash Gordon wrote: ais523 wrote: ais523 wrote:
I'm trying to write a function that does some realloc-style modification of a pointer. The function itself works, but I'm having some problems with the prototype. This is a simple example of the sort of thing I want to do (not the complete code, that works but is technically UB). It's in the FAQ http://c-faq.com/ptrs/genericpp.html
Thanks for pointing that out. I have read the FAQ, but obviously not
thoroughly enough, although I was aware of many of the points raised.
If you want to do realloc type stuff, do it the way realloc does it. #include <stdlib.h>
void indirectmalloc(void** ptr, size_t s) { *ptr=malloc(s); return; } Why not return the pointer as malloc does? There is no need in this example to pass in a pointer to a pointer.
I was trying to make a minimal example of the sort of thing I was
doing. The actual function was designed to expand the amount of memory
allocated in ptr realloc-style, but determining the amount itself, and
with error handling if things went wrong (it would make several
attempts and return the amount it actually managed to allocate). int main(void) { char* a; int* b; #if 0 indirectmalloc(&a); indirectmalloc(&b); Sorry, that should have been #endif indirectmalloc(&a, sizeof *a); indirectmalloc(&b, sizeof *b);
This is why you should always copy and paste the code you actually compile from your editor.
I'll try to do that more in future. (I've copied-and-pasted in the
past, but for some reason I didn't this time). free(a); free(b); return 0; }
but although all pointers are freely castable to and from void*, not all pointers-to-pointers are freely castable to and from void**, so this is UB. One compiler I use allows &(void*)a, but this is nonstandard. Is there a portable way of doing this without relying on temporary void*s? No. Should I use a #define macro instead? Although there are obviously easier ways of writing the above code, I'd like to do something like this (my actual code is more complicated; I'm not posting it because it works, just invokes UB which happens to not be a problem on my compiler).
Change the code so it takes a void* (if it needs one which your indirectmalloc does not) and returns a void*, then the problem goes away. realloc was written the way it was for a reason!
Yes, thinking about it I realise that I can make the pointer the return
value and use an argument to pass back what would have been the return
value.
On 2006-04-16, ais523 <ai****@bham.ac.uk> wrote: Flash Gordon wrote:
ais523 wrote: > ais523 wrote: > >> I'm trying to write a function that does some realloc-style >> modification of a pointer. The function itself works, but I'm having >> some problems with the prototype. This is a simple example of the sort >> of thing I want to do (not the complete code, that works but is >> technically UB). It's in the FAQ http://c-faq.com/ptrs/genericpp.html Thanks for pointing that out. I have read the FAQ, but obviously not thoroughly enough, although I was aware of many of the points raised.
If you want to do realloc type stuff, do it the way realloc does it.
>> #include <stdlib.h> >> >> void indirectmalloc(void** ptr, size_t s) >> { >> *ptr=malloc(s); >> return; >> }
Why not return the pointer as malloc does? There is no need in this example to pass in a pointer to a pointer. I was trying to make a minimal example of the sort of thing I was doing. The actual function was designed to expand the amount of memory allocated in ptr realloc-style, but determining the amount itself, and with error handling if things went wrong (it would make several attempts and return the amount it actually managed to allocate).
There is still no need to pointer to pointer. Just pass the already
malloced pointer to your "indirect" function and return NULL for
failure with the stipulation that the caller is responsible for
free'ing the original block. >> int main(void) >> { >> char* a; >> int* b; > #if 0 >> indirectmalloc(&a); >> indirectmalloc(&b); > Sorry, that should have been > #endif > indirectmalloc(&a, sizeof *a); > indirectmalloc(&b, sizeof *b);
This is why you should always copy and paste the code you actually compile from your editor.
I'll try to do that more in future. (I've copied-and-pasted in the past, but for some reason I didn't this time). >> free(a); >> free(b); >> return 0; >> } >> >> but although all pointers are freely castable to and from void*, not >> all pointers-to-pointers are freely castable to and from void**, so >> this is UB. >> One compiler I use allows &(void*)a, but this is nonstandard. Is there >> a portable way of doing this without relying on temporary void*s?
No.
>> Should I use a #define macro instead? Although there are obviously >> easier ways of writing the above code, I'd like to do something like >> this (my actual code is more complicated; I'm not posting it because it >> works, just invokes UB which happens to not be a problem on my >> compiler).
Change the code so it takes a void* (if it needs one which your indirectmalloc does not) and returns a void*, then the problem goes away. realloc was written the way it was for a reason!
Yes, thinking about it I realise that I can make the pointer the return value and use an argument to pass back what would have been the return value.
I dont think you need to do that. Read the manpage for realloc :
although fair enough if you envisage a host of possible return values
from your indirectmalloc function and wish to analyse these later. IMO
one return value is sufficient : the pointer itself. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul Paterson |
last post by:
I am trying to find a way to mimic by-reference argument passing for
immutables in Python. I need to do this because I am writing an
automated VB to Python converter.
Here's an example of the VB...
|
by: Jakob Vesterstrom |
last post by:
Hi all
I have question regarding function passing.
I have class A
class A {
public:
...
void attach(double f(double in)) {
|
by: Együd Csaba |
last post by:
Hi,
I've a problem with some of my stored procs. My config is: RH7.1, Postgres
7.3.2
I had converted a few fields of a few tables from one type to another and
after this I made all the...
|
by: Együd Csaba |
last post by:
> Hi,
> I've a problem with some of my stored procs. My config is: RH7.1, Postgres
> 7.3.2
>
> I had converted a few fields of a few tables from one type to another and
> after this I made all...
|
by: Alfonso Morra |
last post by:
I have come accross some code where function pointers are being cast
from one type to another (admittedly, they all have the same return
type). eg. you may have func ptrs declr as ff:
typedef...
|
by: Martin Jørgensen |
last post by:
Hi,
I'm relatively new with C-programming and even though I've read about
pointers and arrays many times, it's a topic that is a little confusing
to me - at least at this moment:
----
1)...
|
by: MotoK |
last post by:
Hi Experts,
I've just joined this group and want to know something:
Is there something similar to smart pointers in C or something to
prevent memory leakages in C programs.
Regards
MotoK
|
by: streamkid |
last post by:
i'm a learning newbie at c++... and i have the following question...
reading some source code, i saw this:
int function(const void * one, const void * two)
{
int var1, var2;
var1 =...
|
by: Piotrek |
last post by:
Hi,
Like almost all of beginners I have problem understanding pointers.
Please, look at this piece of code, and please explain me why myswap
function doesn't work as it's supposed to do, whereas...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |