473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Is pointer malloc'ed?

Hi all

I was wondering whether there was a way to know whether a pointer (in
my case, I'm using a char pointer) has been allocated memory through
malloc or not, thus needing to be freed at a later stage?

BTW, I thought this would be a common question - so either I'm just
plain ignorant (coming from a C# and Python background) or my google
skills need to improve.

Oh, and Happy new year everyone!

Chris
Jan 1 '08 #1
2 1564
Khookie said:
Hi all

I was wondering whether there was a way to know whether a pointer (in
my case, I'm using a char pointer) has been allocated memory through
malloc or not, thus needing to be freed at a later stage?
Um, yeah, kinda. Ish. Well, no. But yes.

My! Wasn't that helpful! :-)

C doesn't support you in your bid to know this, but it doesn't forbid you
from knowing, either. When you allocate memory via malloc, calloc, or
realloc, you know that you are doing so (*but* see below). It is perfectly
possible to store a copy of the pointer thus obtained in some kind of
searchable list, for later lookup.

It is, however, a messy way to deal with memory deallocation, and you have
to remember to remove the copy from the lookup table when you free the
memory.

Also, if you call third-party libraries that allocate memory, well, they
won't know about your super new list, and so they won't know to update it
with the memory they allocate.

So, really, the answer is "no" unless you're prepared to do a *lot* of
work.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 1 '08 #2
On Jan 1, 7:12 am, Khookie <chris.k...@gmail.comwrote:
Hi all

I was wondering whether there was a way to know whether a pointer (in
my case, I'm using a char pointer) has been allocated memory through
malloc or not, thus needing to be freed at a later stage?
I am assuming you don't want to know where the memory for the actual
pointer comes from but you want to know where the memory pointed to by
the pointer comes from .... right?

First of all, given a memory address you don't even know if the
address is to memory that has been allocated at all... let alone from
where it was allocated. If I told you it was allocated... you
wouldn't know how much memory was allocated starting at that point.

So as Richard said, the answer is no, you actually need to keep track
of that your self (or use something fancy like a memory pool) when
coding in C.

Regards,
Ivan Novick
http://www.0x4849.net

Jan 1 '08 #3

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

Similar topics

3
by: Woodster | last post by:
I knoew that free is usually paired with malloc and delete is usually paired with malloc, alloc or similar. Can I use delete with malloc? The main reason I ask is for using the strdup function....
0
by: Simon Nickerson | last post by:
I have a function which looks like this: void rho(matrix_t *out, matrix_t *in) { static int firsttime = 1; static matrix_t *words; /* ... other variables ... */ if (firsttime) { firsttime...
14
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
11
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: ...
33
by: apropo | last post by:
what is wrong with this code? someone told me there is a BAD practice with that strlen in the for loop, but i don't get it exactly. Could anyone explain me in plain english,please? char...
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
0
by: Gabriel de Dietrich | last post by:
Hi, I'm doing my first project on embedding and then extending Python in an application. The idea is to import a set of C++ plug-ins into Python and then be able to run a script that uses these...
3
by: Khookie | last post by:
Hi everyone I'm not sure whether this is good practice or not, so I thought I'll ask. I've got a function that returns a char pointer, which is malloc'ed in the function itself, as per below...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
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 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.