Hi,
Is there a standard way to validate if a pointer points to memory that was
allocated by malloc?
TIA 21 2307
Bryan Bullard wrote: Is there a standard way to validate if a pointer points to memory that was allocated by malloc?
No.
--
Best regards,
Andrey Tarasevich
> Is there a standard way to validate if a pointer points to memory that was allocated by malloc?
You would have to write your own wrapper for malloc for that one - that
might be fun. :)
Kristofer
Bryan Bullard wrote: Is there a standard way
No.
to validate if a pointer points to memory that was allocated by malloc?
cat onStack.c
#include <stdio.h>
#include <stdlib.h>
int onStack(void* p) {
return (p > (void*)&p);
}
int main(int argc, char* argv[]) {
char c;
char* p = &c;
//char* p = (char*)malloc(s izeof(char));
if (onStack(p)) {
fprintf(stdout,
"p probably points to a character in automatic storage.\n");
}
else {
fprintf(stdout,
"p probably points to static data or free storage.\n");
}
return 0;
}
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cp******** **@nntp1.jpl.na sa.gov... Bryan Bullard wrote:
Is there a standard way
No.
to validate if a pointer points to memory that was allocated by malloc?
> cat onStack.c #include <stdio.h> #include <stdlib.h>
int onStack(void* p) { return (p > (void*)&p); }
int main(int argc, char* argv[]) { char c; char* p = &c; //char* p = (char*)malloc(s izeof(char)); if (onStack(p)) { fprintf(stdout, "p probably points to a character in automatic storage.\n"); } else { fprintf(stdout, "p probably points to static data or free storage.\n"); } return 0; }
Actually, what I'm looking for is a way to determine if a pointer points to
a block that is "safe" to read.
Bryan Bullard wrote: Bryan Bullard wrote:
Is there a standard way [...]
Actually, what I'm looking for is a way to determine if a pointer points to a block that is "safe" to read.
And you've been told that there's no standard way, which
is what you asked for. I've got a counter-question: If your
code is handed a junk pointer, what corrective action do you
expect it to take?
-- Er*********@sun .com
On Fri, 10 Dec 2004 22:08:57 GMT, in comp.lang.c , "Bryan Bullard"
<re****@to.grou p.com> wrote: Actually, what I'm looking for is a way to determine if a pointer points to a block that is "safe" to read.
There is still no standard way to do this. If the block points to memory
you didn't allocate, thats a coding error. If it points to memory you
deallocated, thats also a coding error. If it points to corrupted memory,
your programme is already dead, so its too late to worry...
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
"Eric Sosman" <er*********@su n.com> wrote in message
news:cp******** **@news1brm.Cen tral.Sun.COM... I've got a counter-question: If your code is handed a junk pointer, what corrective action do you expect it to take?
Something more graceful then core dump.
Thank you to everyone that posted.
On Fri, 10 Dec 2004 22:41:41 GMT, in comp.lang.c , "Bryan Bullard"
<re****@to.grou p.com> wrote: "Eric Sosman" <er*********@su n.com> wrote in message news:cp******* ***@news1brm.Ce ntral.Sun.COM.. .
I've got a counter-question: If your code is handed a junk pointer, what corrective action do you expect it to take?
Something more graceful then core dump.
Then you're out of luck. There's no standard way to detect memory
corruption.
The question you should probably be asking is "why would I ever get a bad
pointer, if I wrote my code carefully?"
Of course from a system-specific point of view there might be either a
coding hack of some sort or a 3rd party library which trapped bad memory
and handled it. That would be a topic for a unix or windows or whatever
programming group.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt > This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Yossarian |
last post by:
Hi,
I'm a bit confused about something, hopefully someone can put me
straight.
I'd like to be able to call a function which takes a pointer to
pointer, have that function allocate memory and return the size. I
can't get it to work and I would like to know why this code outputs
wierd values (the first mem address is ok i think):
|
by: joe |
last post by:
Hi all! I just have quick, possibly stupid question....
is it possible to do the following:
int func(){
int *pointer;
foo(pointer);
}
int foo(int *pointer){
|
by: junky_fellow |
last post by:
According to Section A6.6 Pointers and Integers (k & R)
" A pointer to one type may be converted to a pointer to another type.
The resulting pointer may cause
addressing exceptions if the subject pointer does not refer to an
object suitably aligned in storage. It is
guaranteed that a pointer to an object may be converted to a pointer to
an object whose type requires less
or equally strict storage alignment and back again without change;...
|
by: junky_fellow |
last post by:
Consider an implementation that doesn't use all bits 0 to represent
a NULL pointer. Let the NULL pointer is represented by 0x12345678.
On such an implementation, if the value of NULL pointer is printed
will it be all 0's or 0x12345678
int main(void)
{
char *ptr;
ptr = 0;
|
by: Mirko |
last post by:
Hello, I'm new to this list and to Usenet in general,
so please forgive (and advice) me, if I do something wrong.
Anyway.
I am a bit confused, because I always thought one
_should_ explicitly cast the void* pointer returned by malloc.
Now I read postings saying that this is dangerous.
Could somebody please clearify this?
| |
by: yezi |
last post by:
Hi, all:
I want to record some memory pointer returned from malloc, is possible
the code like below?
int memo_index;
int i,j;
char *tmp;
for (i=0;i<10;i++){
|
by: Johs32 |
last post by:
I have a struct "my_struct" and a function that as argument takes a pointer
to this struct:
struct my_struct{
struct my_struct *new;
};
void my_func(struct my_struct *new);
I have read that there is no difference between giving this function a
|
by: mkaushik |
last post by:
Hi everyone,
Im just starting out with C++, and am curious to know how "delete
<pointer>", knows about the number of memory locations to free.
I read somewhere that delete frees up space assigned to <pointerby
"new". Does "new" create a list of pointer names and the size of the
memory array they point to? I also read that some compilers may store
the number of consec mem locations a pointer points to, just before the
first data...
|
by: svata |
last post by:
Hello there,
after some time of pondering I come to some solution which would suit
me best. Please correct, if I am wrong.
Function has two parameters. A string array, better said a pointer to
it, char *p_buf and int size.
int read_name( char *p_buf, int size) {
char *p_item_name_1;
|
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,...
|
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...
| |
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |