Can't write into a text file with for loop in c

I have an issue with writing strings into a txt file. My lines get overwritten every time. I use gcc -Wall -o filename filename.c to compile and ./filename 10 Berlin cat resultat.txt to execute. The txt file always has just one line (the last one) h...
more »

2017-10-29 22:10 (1) Answers

How to implement calloc

I'm trying to rewrite malloc and calloc, my question is about the implementation of calloc, not how to use it. One should always use calloc() instead of malloc()+memset(), because it could take advantage of copy-on-write (COW). Some calloc's are im...
more »

2017-10-20 18:10 (1) Answers

How to tell if malloc failed in swift on iOS?

How can I detect if malloc fails in swift? The end goal is to simply allocate the required amount of space, and if ios can not allocate it, report this elegantly to the user (instead of being terminated). When I try the code below, the pointer is n...
more »

2017-09-16 20:09 (2) Answers

Can still print a string after I freed it?

I am learning and testing memory allocation in C and I want to test what happens if free() is called. I expected there could be a segmentation fault or pointer is NULL after I run the program below. However, I can still successfully print the strin...
more »

2017-07-19 20:07 (2) Answers

Heap exploit fails: Memory corruption

Currently learning heap exploitation but there is something weird I don't understand: This is the next chunk to be allocated by a call to malloc(0x80): 0x602090: 0x0000000000000000 0x0000000000000091 0x6020a0: 0x00007ffff7dd1b78 0x00000000006...
more »

2017-07-03 19:07 (0) Answers

Confusion about malloc and calloc function in C

Declaration of malloc function: void *malloc(size_t size); Here, malloc returns void pointer. So, A void function returns nothing, then Why we assign malloc(function call) value to pointer? For example: int *ptr; ptr = malloc(10 * sizeof (*pt...
more »

2017-07-03 12:07 (3) Answers

Output struct data from allocated memory

I have a question, I have created a struct: struct ipOut { unsigned int ipVer; unsigned int headerDWORDS; unsigned int headerBytes; unsigned int typeOfService; unsigned int ipLength; unsigned int ident; unsigned int ttl; ...
more »

2017-06-21 17:06 (0) Answers

Malloc vs New for Primitives

I understand the benefits of using new against malloc in C++. But for specific cases such as primitive data types (non array) - int, float etc, is it faster to use malloc than new? Although, it is always advisable to use 'new' even for primitives, i...
more »

2017-06-16 13:06 (4) Answers

heap overflow affecting other programs

I was trying to create the condition for malloc to return a NULL pointer. In the below program, though I can see malloc returning NULL, once the program is forcebly terminated, I see that all other programs are becoming slow and finally I had to rebo...
more »

2017-06-07 06:06 (4) Answers

Why can't I use pointer addition instead of malloc

I was wondering why I have to use malloc to dynamically create memories, while I already have pointer addition to freely manipulate the memories. So I created this code, but this code corrupts, saying that 'stack around the variable 'a' was corrupted...
more »

2017-06-03 04:06 (4) Answers

What does allocating memory of size char * do?

char *str; str = malloc(sizeof(char) * 5); This code allocates 5 consecutive slots of memory to the variable str, which is of type char *. char *str; str = malloc(sizeof(char *) * 5); This is supposed to allocate 5 times the memory of an a...
more »

2017-05-25 15:05 (5) Answers

2d dynamic array in C fault

I have to write a program with dynamic 2d array but I've got a problem. Everything works fine until I try to print the array with more than 4 rows. Up to 4 rows all looks good, but with 5th row the length of last row expands to 7631472 which cause a ...
more »

2017-04-09 16:04 (2) Answers

Memcpy takes the same time as memset

I want to measure memory bandwidth using memcpy. I modified the code from this answer:why vectorizing the loop does not have performance improvement which used memset to measure the bandwidth. The problem is that memcpy is only slighly slower than me...
more »

2017-04-02 14:04 (2) Answers

C array of pointers to double

I'm getting a memory problem in c++ code, for sure I'm wrong but I don't get the problem. I've declare an array of three pointer to double and allocate memory. double *myDoubles[3]; for(int i=0;i<3;i++) { myDoubles[i]= (double *) malloc(1000*...
more »

2017-04-02 13:04 (3) Answers

Interview about malloc in function

I faced a question in an interview of a company, and I cannot figure out the reason about the answer. void newArray(int* local, int size) { local = (int*) malloc( size * sizeof(int) ); } int main() { int* ptr; newArray(ptr, 10); } ...
more »

2017-03-22 17:03 (5) Answers

Double-asterisks and `malloc` in C

I've been studying pointers for a while and I can't seem to wrap my head around it fully. There seems to be a gap that isn't explained when you jump from tutorials explaining pointers to actual functions and code that assume you know about them. The ...
more »

2017-03-22 04:03 (5) Answers

Interview questions about string and char

I faced two questions from a company. The two questions confused me. Can anyone help explain the reason of the answers? Write the result. void Test(void){ char *str = (char *) malloc(100); strcpy(str, “hello”); free(str); if(str != NUL...
more »

2017-03-20 10:03 (3) Answers

Why Can I Trust Memory Allocation?

I'm currently taking an introductory CS course that uses C. My textbook seems to imply that a pointer variable still holds the address for memory previously allocated to it, even after free() was called on it -- assume I previously used malloc(). Doe...
more »

2017-03-19 08:03 (5) Answers