free

C resizing a dynamic array

那年仲夏 提交于 2019-12-11 11:37:56
问题 I'm creating a dynamic array data structure for a C class. I have most everything implemented and now I'm testing it out. It's currently getting stuck during a resize. I've used printf's to follow the issue. It looks like the resize is working, but when I go to add my next item post resize, it's stopping there. I think it may have to do with my memory allocation or the way I'm pointing and freeing arrays during the resize. I'm new to C so these are my sticking points. #include <assert.h>

malloc, free, and memmove inside a subfunction

≯℡__Kan透↙ 提交于 2019-12-11 10:37:39
问题 I want to use a subfunction to copy a char array. it is like this: void NSV_String_Copy (char *Source, char *Destination) { int len = strlen(Source); if (*Destination != NULL) free(Destination); Destination = malloc(len + 1); memmove(*Destination, Source, len); Destination[len] = '\0'; //null terminate } that way, I can call it from the main function and perform the operation this way: char *MySource = "abcd"; char *MyDestination; NSV_String_Copy (MySource, MyDestination); However, it does

How to use malloc and free in 64-bit NASM?

故事扮演 提交于 2019-12-11 09:54:55
问题 In 64-bit NASM, I'm allocating a memory block of 8000 bytes using malloc() from the C library, and when I'm finished with it, I deallocate it by calling free(). My research has come up with a lot of conflicting information about how to do this in 64-bit NASM, and much of the information is 32-bit, where the calling convention is different, or it's C or C++, not NASM. I think I have the malloc part right, but I'm not sure about the free part. I'm posting this question because I don't want to

Can you mix free and constructor in C++? [duplicate]

北城以北 提交于 2019-12-11 06:15:25
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Is there any danger in calling free() or delete instead of delete[]? I was reading this question: In what cases do I use malloc vs new? Someone raised that one reason to use malloc was if you were going to use free. I was wondering: Is it valid to mix a free call and a constructor initialization in C++? i.e. Can I say: my_type *ptr = new my_type; free(my_type); Is that somehow invalid or worse than: my_type *ptr

Invalid Write — Valgrind

我只是一个虾纸丫 提交于 2019-12-11 04:28:41
问题 Hi I'm running into an munmap_chunk(): invalid pointer: error in my c program. The main problem is...I'm not even sure what all of the ways a pointer can become invalid are. I've checked all over my code for strings not being calloced with enough space, but found nothing that looks like it'll run over bounds! The relevant code is below (what I THINK is the relevant code anyway) //Takes in a username and suggests friends of friends who are the opposite sex as friends of username 395 void

Is there a way to know size of a pointer passed to a __free_hook in Linux?

半城伤御伤魂 提交于 2019-12-11 03:43:32
问题 I want to track how much memory is currently allocated by a large application. I found that I can install hooks around malloc/free/realloc in order to intercept memory allocation calls: http://man7.org/linux/man-pages/man3/malloc_hook.3.html So what I want to track is total bytes allocated - total bytes freed. Now the problem is that free only takes a pointer and not a size. In can create my own map or hashmap in my malloc hook that tracks how much memory was allocated for that pointer but

Calling free( ) in C

こ雲淡風輕ζ 提交于 2019-12-11 03:35:02
问题 What exactly is happening when I call free(a) ? I know it is freeing the memory used, but does this also mean that the data stored in array a can no longer be accessed (therefore, not being able to call copyArray )? int *a = (int *) malloc(sizeof(int) * numberOfInts); for (i = 0; i < numberOfInts; i++) { a[i] = random(); printf("%d\n", a[i]); } free(a); copyArray(*a++, numberOfInts); 回答1: What exactly is happening when I call free(a)? This is implementation specific. As an example, with Linux

glibc detected - double free or corruption

試著忘記壹切 提交于 2019-12-11 02:49:48
问题 I get the following error messages when I submit the code (pasted below) to an online gcc compiler. * glibc detected /run-1326102706-2046832693/solution: double free or corruption (!prev): 0x091901a8 ** ======= The code is as follows: # include <iostream> # include <string> # include <list> # include <cstring> using namespace std; int main() { int test_cases, i, score, str_len; string str; char first_char, current_char; list <int> strlist; list <int> :: iterator it; cin>>test_cases; char

*glibc detected double free or corruption() * message!

£可爱£侵袭症+ 提交于 2019-12-10 23:34:41
问题 The following deleteNode function when I run the program gets these: * glibc detected free(): invalid next size (normal): 0x000000000103dd90 ** Even i make the ' free(here); ' a comment,i get the above message. I dont think that the other 'free' calls provokes a problem like that. But I cant see why this would be wrong. :/ struct List *deleteNode(int Code,int i,char* Number) { struct List *here; here=Head; for (here; here!=Tail; here=here->next) { if ( (here->number==Number) && (here->code=

First element offset

你。 提交于 2019-12-10 21:07:59
问题 Is it a WARRANTY, that offset of first element of structure is 0? To be more accurate, lets consider struct foo { int a; double b; }; struct foo *ptr=malloc(sizeof(struct foo)); int *int_ptr = &ptr->a; free(int_ptr) Is it garantied, that it is valid always, under any os or any other factors? 回答1: Yes it is guaranteed. Will get you a standard quote, let me lookup. C99 Standard: §6.7.2.1 Para 12 Within a structure object, the non-bit-field members and the units in which bit-fields reside have