malloc

Return an allocated variable

I know we should free any variable allocated with malloc, but what if I return it in a function? Something like this: char *somefunction(int somearg){ char *str; str=(char *)malloc(sizeof(char *)); //some code return str; } Should I free str? How could I do that? ...

Globally override malloc in visual c++

Hi! I'm trying to figure out a way to globally override malloc and related functions in visual c++ (2005). My setup is a dll with statically linked runtime library that consists of both my own c++ code, external c++ and c code. What I want to accomplish is to allow a user of the dll to set their own implementations of the memory allocat...

Does going out of scope like this free the associated memory?

I was just wondering, in the following scenarion, is the memory used by 'stringvar' freed after method1 is done executing? // Just some method void method2(char* str) { // Allocate 10 characters for str str = malloc(10 * sizeof(char)); } // Just another method void method1() { char* stringvar; method2(stringvar); // Is the m...

Memory allocation and deallocation across dll boundaries

I understand that memory allocations made in one dll then subsequently free'd in another can cause all sort of problems, especially regarding the CRT. These sorts of problems are especially problematic when it comes to exporting STL containers. We've experienced these sorts of problems before (when writing custom Adobe plugins that lin...

How to initialze an array after dynamic memory allocation?

I have a function that returns an array of different lengths based on a table lookup. I am malloc'ing required memory for it inside the function but then how can I fill the array from its pointer? The compiler is throwing same error for both of my tries (commented lines). Please help! int lookup(const char *name, float *factors) { i...

Overriding "new" and Logging data about the caller

I'm trying to write a memory profiler and so far have been able to get my custom functions to work for malloc, free, new and delete. I tried using __FILE__ and __LINE__ to log the originator inside the overloaded new method, but (as expected) it just gives the details of where the overloaded function is. Is there a way to get the details...

How can I allocate memory and return it (via a pointer-parameter) to the calling function?

I have some code in a couple of different functions that looks something like this: void someFunction (int *data) { data = (int *) malloc (sizeof (data)); } void useData (int *data) { printf ("%p", data); } int main () { int *data = NULL; someFunction (data); useData (data); return 0; } someFunction () and useData () ...

Giving an instance of a class a pointer to a struct

I am trying to get SSE functionality in my vector class (I've rewritten it three times so far. :\) and I'm doing the following: #ifndef _POINT_FINAL_H_ #define _POINT_FINAL_H_ #include "math.h" namespace Vector3D { #define SSE_VERSION 3 #if SSE_VERSION >= 2 #include <emmintrin.h> // SSE2 #if SSE_VERSION >= 3 #inc...

iPhone - UIImage imageScaledToSize Memory Issue

I have done research and tried several times to release the UIImage memory and have been unsuccessful. I saw one other post on the internet where someone else was having this same issue. Everytime imageScaledToSize is called, the ObjectAlloc continues to climb. In the following code I am pulling a local image from my resource directory...

Any function to query the size of an allocated block?

I realize that any such function is likely to be non standard, but that's ok for my use case. Basically, I need a method (even if it's only exposed through glibc's syscall() interface) that I can pass a pointer to (a pointer that was returned by a previous call to malloc()) that returns the size of the block the pointer points at. Does s...

iPhone - Multiple CGBitmapContextCreateImage Calls - ObjectAlloc climbing

Has anyone else come across this problem? ObjectAlloc climbs as a result of the CGBitmapContextCreateImage. Does Apple's software not fully releasing the objectalloc? I am resizing images 12 times a second with a NSTimer. During resizing of the image I am also adding a photoshop like Gaussian blur effect by including interpolationQuali...

What is the correct way to handle "out of memory"?

Recently, I work on a video player program on Windows for a CCTV program. As the program has to decode and play many videos streams at the same time, I think it might meet the situation that malloc will fail and I add checking after every malloc. But genrally speaking, in these code of open source programs that I've read in open sourc...

How can malloc() cause a SIGSEGV?

I have an odd bug in my program, it appears to me that malloc() is causing a SIGSEGV, which as far as my understanding goes does not make any sense. I am using a library called simclist for dynamic lists. Here is a struct that is referenced later: typedef struct { int msgid; int status; void* udata; list_t queue; } msg_...

iPhone - Instruments ObjectAlloc GeneralBlock

Okay, I have been trying to days to lower the Net Bytes on GeneralBlock 16, I understand that a GeneralBlock is created by the iPhone's OS when creating its own object. Is it possible to lower this alloced memory? I have read in some places that GeneralBlock is something that you shouldn't worry about. True? ...

Can I use a memory zone to kill iPhone leaks?

I have a C++ class that I'm using from my Objective-C++ controller in my iPhone app. The C++ class does some calculations on some data, returns a result, and then is done -- but it leaks like crazy. I'm wondering if I can somehow make use of Memory Zones (aka malloc zones aka allocWithZone) to solve this. My idea is to allocate the ObjC+...

passing a block of memory from a function

I'm trying to figure out how to allocate a block of memory in a function and pass back a pointer to that block through one of the arguments. This is a C program. I seem to be having some trouble. Here's the code: void foo(char *ptr) { if (!(ptr = malloc(size))) printf("error"); /* code here */ printf("buffer a...

strtok and memory leaks

hello, I wrote a simple url parser using strtok(). here's the code #include <stdio.h> #include <stdlib.h> typedef struct { char *protocol; char *host; int port; char *path; } aUrl; void parse_url(char *url, aUrl *ret) { printf("Parsing %s\n", url); char *tmp = (char *)_strdup(url); //char *protocol, *host...

C can someone tell me what is going on here?

I can not figure out what the heck is happening here. What I expect is that the output should say that there is only 1 element in keys, it's saying there are 7 when I have allocated only the 0 position with 120 bytes. void add2(char **b, char *i) { if (!i) { b[0] = (char*) malloc(120); sprintf(b[0], "%s", "hithere");...

newbie questions about malloc and sizeof

Can someone explain to me why my call to malloc with a string size of 6 returns a sizeof of 4 bytes? In fact, any integer argument I give malloc I get a sizeof of 4. Next, I am trying to copy two strings. Why is my ouput of the copied string (NULL)? Following is my code: int main() { char * str = "string"; char * copy = malloc(s...

Potential problem with C standard malloc'ing chars.

When answering a comment to another answer of mine here, I found what I think may be a hole in the C standard (c1x, I haven't checked the earlier ones and yes, I know it's incredibly unlikely that I alone among all the planet's inhabitants have found a bug in the standard). Information follows: Section 6.5.3.4 ("The sizeof operator") p...