473,386 Members | 1,710 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,386 software developers and data experts.

Question regarding malloc

ashitpro
542 Expert 512MB
consider following function

f(char *p)
{
if p has allocated the memory previously then
free p;
else
return;
}

I guess You understood the problem.
How do I know that any pointer has allocated the memory?
Oct 6 '07 #1
7 1124
Firecore
114 100+
wouldnt something like

[code]
if(pointer != NULL)
{
do stuff;
}

work?

Im a noob @ C++ but give it a go.
Cuz i think if a pointer points to null its not pointin at anything.
Oct 6 '07 #2
JosAH
11,448 Expert 8TB
consider following function

f(char *p)
{
if p has allocated the memory previously then
free p;
else
return;
}

I guess You understood the problem.
How do I know that any pointer has allocated the memory?
You can't do that in an implementation independent way. You could fiddle diddle
a bit with the brk() or sbrk() system calls (if present) but that's about it.

kind regards,

Jos
Oct 6 '07 #3
ashitpro
542 Expert 512MB
You can't do that in an implementation independent way. You could fiddle diddle
a bit with the brk() or sbrk() system calls (if present) but that's about it.

kind regards,

Jos
How can I use these system calls in this context?
example?
Oct 6 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
When you have a pointer in a function, keep in mind:

1) if you free it you may crash because the pointer was not allocated using malloc().
2) if it was allocated by malloc() and you don't free it, then the memory never gets released and you have a memory leak
3) it it was allocated by malloc() and you free it, then if there is another copy of the pointer elswhere in the program, then you will crash over there trying yo use the pointer to deleted memory.

Memory management requires tools far beyond simple malloc() and free() calls.
Oct 6 '07 #5
Firecore
114 100+
Memory management requires tools far beyond simple malloc() and free() calls.
Could you elaborate on these tools?
Oct 7 '07 #6
JosAH
11,448 Expert 8TB
How can I use these system calls in this context?
example?
You might try to use this:

Expand|Select|Wrap|Line Numbers
  1. char* lowm= sbrk(0); // low water mark;
  2.  
  3. void* anyPointer= ... // pointer value to be checked.
  4.  
  5. if ((char*)anyPointer >= lowm) // it's an allocated value
  6.  
kind regards,

Jos
Oct 7 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
Could you elaborate on these tools?
In C++ you use handle classes. See the article about this in the C/C++ Articles section.

In C you would write a function to create a count on the heap. Then you would make a copy of the pointer (like a function call), you boos this count and when a copy disappers to delete this count. If the count is zero, you free the pointer.

In C++ this is automatic. In C this is a manual operation and just one slip and the count is off. This is very hard to to in C and is always unrelaible.

Issues also occur with multi-threading where the pointers are copied on separate threads. More code in C but in C++ critical sections can be used in the handle class where the counts are bieng adjusted.
Oct 7 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: porschberg | last post by:
Hi, I have question regarding the pool library. In my application I read a lot of data records from a database into a small data class and therefor I thought object_pool could help me to reduce...
5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
19
by: amanayin | last post by:
Why does this program run in the ddd debugger but when i try to run the program in a konsole i get a segmentation fault. I just can't work it out the OS i use is suse 8.2 pro program below: /*...
36
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program...
19
by: lawtrevor | last post by:
I have a question regarding the use of free() based on some code I need to decipher: { struct A {<A fields>}; struct B {<A fields+ <Bfields>}; typedef struct A Aobj; typedef struct B Bobj;
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
6
by: mattmao | last post by:
emmm, this is taken from the exam I just finished this morning: Which one is correct: A) double *ptr = (double *) malloc(100 * sizeof(double)); B) double *ptr = (double *) malloc(100 *...
10
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned...
7
by: somenath | last post by:
Hi All , As a process of my C language learning I was trying to learn how malloc can be implemented from K&R2 .But I am not able to understand few points . It would be very much helpful if...
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
0
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,...
0
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...

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.