malloc

malloc.c:3074 error?

Hi, When I write a C program, I encountered a problem that is as follows: malloc.c:3074: sYSMALLOc: Assertion (old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct ...

Why does the compiler assume that malloc returns an int?

I'm aware that in C it's best practice to never cast the return value of malloc(). I've read that the compiler assumes that malloc() returns an int if you don't include stdlib.h. Of course it would produce an error if you tried to implicitly assign an int to something that's not an int, but that error could be covered up by an explicit c...

C malloc : inexplicable memory usage

When I compile and run the following code :(using gcc on cygwin) int *a = malloc(1024*1024*100*sizeof(int)); while(1) ; The task manager in Windows XP shows memory usage by this process as 2232K, which according to me should have been around 400000K. When I compile and run the following code :(using gcc on cygwin) int *a = malloc(...

Instruments (Leaks) and NSDateFormatter

When I run my iPhone app with Instruments Leaks and parse a bunch of NSDates using NSDateFormatter my memory goes up about 1mb and stays even though these NSDates should be dealloc'd after the parsing (I just discard them if they aren't new). I thought the malloc (in my heaviest stack trace below) could become part of the NSDate but I a...

Struct initialization problem?

Hi, I'm using a struct like this: define struct _Fragment{ int a; char *seq; }Fragment; I want to initialize the struct, and using the malloc() method return a dynamic memory like this Fragment *frag=malloc(10*sizeof(Fragment)); Then I would using the frag pointer like this: frag->seq="01001"; Then the problem occurs...

Malloc, string pointers, and Valgrind

My program is like this (main.c): #include <stdlib.h> #include <stdio.h> void main(){ char *first="hello "; char *second="world!"; char *seq=(char *)malloc((strlen(first)+1)*sizeof(char)); strcat(strcpy(seq,first),second); printf("%s\n",seq); free(seq); } and I debug with the tool valgrind, it said that($:valgrind --tool=m...

Reading part of a file in C using fread() and fseek()

I'm trying to read a file into a buffer in blocks of size BLOCK_SIZE (currently equal to 1000 unsigned chars). My code first finds the number of blocks it will have to read in order to read the entire file (usually 2-4), then iterates through a for loop reading the file (ignore the "+17+filenamesize" stuff, that is all needed for later i...

How to receive dynamic length data from a message queue?

I have to send and receive dynamic data using a SysV message queue for a university project. The length of the data is transmitted in a separate message, size is therefor already known. And this is how I try to receive the data. I have to admit that I'm not a C++ specialist, especially when it comes to memory allocation. struct { ...

invalid converstion from void* to char**

it has been a while since I messed with C code. I am getting the following error when compiling C code under ubuntu using gcc. command i am using to compile code is(if these errors are because of comiler I am using , please let me know how to make that go away): gcc -o runnable mycode.C error: invalid conversion from ‘void*’ to ‘...

char array vs. char pointer

Hey, When receiving data through a socket using recv, I've noticed that, with: char buffer[4]; memset(buffer, 0, 4); recv(socket, buffer, 4, 0); I receive mesgx�� "mesg" being what I sent, with some random characters appended. If I use char * method = (char *) malloc(4); memset(buffer, 0, 4); recv(socket, buffer, 4, 0); ...

How can I get the size of a memory block allocated using malloc()?

Possible Duplicates: How can I get the size of an array from a pointer in C? Is there any way to determine the size of a C++ array programmatically? And if not, why? I get a pointer to a chunk of allocated memory out of a C style function. Now, it would be really interesting for debugging purposes to know how big the allocate...

What is malloc doing in this code?

Could you explain following code? str = (char *) malloc (sizeof(char) * (num+1)); What's is malloc? Why num + 1? ...

malloc() function in c.

Can anyone please explain this? struct node { int data; struct node * link; } main() { struct node *p, *list, *temp; list = p = temp = NULL; ......................... ......................... } addbeg() { int x; temp=malloc(sizeof(struct node)); scanf("%d", &x); temp->data=x; t...

C: Function returning via void *

Coming from Java I'm confused by the use of Void allowing a return value in the following: void *emalloc(size_t s) { void *result = malloc(s); if (NULL == result) { fprintf(stderr, "MEMORY ALLOCATION FAILURE\n"); exit( EXIT_FAILURE ); } return result; } Is this returning a pointer to a chuck o...

Why do I get a warning everytime I use malloc?

If I use malloc in my code: int *x = malloc(sizeof(int)); I get this warning from gcc: new.c:7: warning: implicit declaration of function ‘malloc’ new.c:7: warning: incompatible implicit declaration of built-in function ‘malloc’ I'm new to C. Am I doing something wrong? ...

C : Why do you specify the size when using malloc?

Take the following code : int *p = malloc(2 * sizeof *p); p[0] = 10; //Using the two spaces I p[1] = 20; //allocated with malloc before. p[2] = 30; //Using another space that I didn't allocate for. printf("%d", *(p+1)); //Correctly prints 20 printf("%d", *(p+2)); //Also, correctly prints 30 //although I did...

Is there different about the following memory allocation?

There are four ways to dynamic allocate memory, is there differences among the four ways? first like this: char *seq=(char *)malloc(100*sizeof(char)); void exam(char *seq){ // using 'seq' } second like this: char *seq; void exam(char *seq){ seq=(char *)malloc(100*sizeof(char)); // using 'seq' } third like this: char *s...

How do you know how much space to allocate with malloc()?

I'm a total C newbie, I come from C#. I've been learning about memory management and the malloc() function. I've also came across this code: char *a_persons_name = malloc(sizeof(char) + 2); What I don't understand is how much space this is allocating for a_persons_name. Is it allocating 2 characters (eg. AB) or something else? I als...

How do you allow spaces to be entered using scanf?

Using the following code: char *name = malloc(sizeof(char) + 256); printf("What is your name? "); scanf("%s", name); printf("Hello %s. Nice to meet you.\n", name); A user can enter their name but when they enter a name with a space like Lucas Aardvark, scanf() just cuts off everything after Lucas. How do I make scanf() allow spaces...

Memory leak question in C after moving pointer (What exactly is deallocated?)

Hi! I realize the code sample below is something you should never do. My question is just one of interest. If you allocate a block of memory, and then move the pointer (a no-no), when you deallocate the memory, what is the size of the block that is deallocated, and where is it in memory? Here's the contrived code snippet: #include <std...