Search Results

Search found 756 results on 31 pages for 'malloc'.

Page 5/31 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • I have a Segmentation fault (core dumped) when using strcpy, malloc, and struct

    - by malsh002
    Okay, when I run this code, I have a segmentation fault #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 64 struct example { char *name; }; int main() { struct example *s = malloc (MAX); strcpy(s->name ,"Hello World!!"); return !printf("%s\n", s->name); } the terminal output: alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ make q1 cc -Wall -g q1.c -o q1 alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ ./q1 Segmentation fault (core dumped) alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ gedit q1.c Can someone explain what's going on? thanks.

    Read the article

  • Generating 2-dimensional vla ends in segmentation fault

    - by Framester
    Hi, further developing the code from yesterday (seg fault caused by malloc and sscanf in a function), I tried with the help of some tutorials I found on the net to generate a 2-dim vla. But I get a segmentation fault at (*data)[i][j]=atof(p);. The program is supposed to read a matrix out of a text file and load it into a 2d array (cols 1-9) and a 1D array (col 10) [Example code] #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> const int LENGTH = 1024; void read_data(float ***data, int **classes, int *nrow,int *ncol, char *filename){ FILE *pfile = NULL; char line[LENGTH]; if(!( pfile=fopen(filename,"r"))){ printf("Error opening %s.", filename); exit(1); } int numlines=0; int numcols=0; char *p; fgets(line,LENGTH,pfile); p = strtok (line," "); while (p != NULL){ p = strtok (NULL, ", "); numcols++; } while(fgets(line,LENGTH,pfile)){ numlines++; } rewind(pfile); int numfeats=numcols-1; *data=(float**) malloc(numlines*sizeof(float*)); *classes=(int *)malloc(numlines*sizeof(int)); if(*classes == NULL){ printf("\nOut of memory."); exit(1); } int i=0; while(fgets(line,LENGTH,pfile)){ p = strtok (line," "); for(int j=0;j<numfeats;j++) { (data)[i]=malloc(numfeats*sizeof(float)); printf("%i ",i); (*data)[i][j]=atof(p); p = strtok (NULL, ", "); } (*classes)[i]=atoi(p); i++; } fclose(pfile); *nrow=numlines; *ncol=numfeats; } int main() { char *filename="somedatafile.txt"; float **data2; int *classes2; int r,c; read_data(&data2,&classes2, &r, &c,filename) ; for(int i=0;i<r;i++){ printf("\n"); for(int j=0;j<c;j++){ printf("%f",data2[i][j]); } } return 1; } [Content of somedatafile.txt] 50 21 77 0 28 0 27 48 22 2 55 0 92 0 0 26 36 92 56 4 53 0 82 0 52 -5 29 30 2 1 37 0 76 0 28 18 40 48 8 1 37 0 79 0 34 -26 43 46 2 1 85 0 88 -4 6 1 3 83 80 5 56 0 81 0 -4 11 25 86 62 4 55 -1 95 -3 54 -4 40 41 2 1 53 8 77 0 28 0 23 48 24 4 37 0 101 -7 28 0 64 73 8 1 ...

    Read the article

  • malloc:mmap(size=XX) failed (error code=12)

    - by Michel
    I have a memory problem in an iPhone app, giving me a hard time. Here is the error message I get: malloc: * mmap(size=9281536) failed (error code=12) * error: can't allocate region I am using ARC for this app, in case that might be useful information. The code (below) is just using a file in the Bundle in order to load a core data entity. The strange thing is the crash happens only after more than 90 loops; while it seems to mee that since the size of the "contents" in getting smaller and smaller, the memory request should also get smaller and smaller. Here is the code, if any one can see a flaw please let me know. NSString *path,*contents,*lineBuffer; path=[[NSBundle mainBundle] pathForResource:@"myFileName" ofType:@"txt"]; contents=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; int counter=0; while (counter<10000) { lineBuffer=[contents substringToIndex:[contents rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location]; contents=[contents substringFromIndex:[lineBuffer length]+1]; newItem=[NSEntityDescription insertNewObjectForEntityForName:@"myEntityName" inManagedObjectContext:context]; [newItem setValue:lineBuffer forKey:@"name"]; request=[[NSFetchRequest alloc] init]; [request setEntity: [NSEntityDescription entityForName:@"myEntityName" inManagedObjectContext:context]]; error=nil; [context save:&error]; counter++; }

    Read the article

  • pointer, malloc and char in C

    - by user2534078
    im trying to copy a const char array to some place in the memory and point to it . lets say im defining this var under the main prog : char *p = NULL; and sending it to a function with a string : myFunc(&p, "Hello"); now i want that at the end of this function the pointer will point to the letter H but if i puts() it, it will print Hello . here is what i tried to do : void myFunc(char** ptr , const char strng[] ) { *ptr=(char *) malloc(sizeof(strng)); char * tmp=*ptr; int i=0; while (1) { *ptr[i]=strng[i]; if (strng[i]=='\0') break; i++; } *ptr=tmp; } i know its a rubbish now, but i would like to understand how to do it right, my idea was to allocate the needed memory, copy a char and move forward with the pointer, etc.. also i tried to make the ptr argument byreferenec (like &ptr) but with no success due to a problem with the lvalue and rvalue . the only thing is changeable for me is the function, and i would like not to use strings, but chars as this is and exercise . thanks for any help in advance.

    Read the article

  • C-array to NSData and back

    - by Thor Frølich
    I'm attempting to save a c-style array of Vertex3D structs to an NSData object and get them back when reloading the app: NSData *vert = [NSData dataWithBytes:&vertices length:(sizeof(Vertex3D) * NUM_OF_VERTICES)]; This data is then saved and attempted to be read back into my c-array thusly: vertices = malloc(sizeof(Vertex3D) * NUM_OF_VERTICES); [vert getBytes:&vertices length:(sizeof(Vertex3D) * NUM_OF_VERTICES)]; The above results in “EXC_BAD_ACCESS” followed by: malloc: * error for object 0x48423c0: pointer being freed was not allocated I'm very new to programming so there's probably some fundamental memory management principle I'm unaware of. I have verified that the loaded NSData is identical to the saved one, but it's clear that the transition from c-array to NSData (and back) is not as I intended.

    Read the article

  • Using fscanf with dynamically allocated buffer.

    - by ryyst
    Hi, I got the following code: char buffer[2047]; int charsRead; do { if(fscanf(file, "%2047[^\n]%n%*c", buffer, &charsRead) == 1) { // Do something } } while (charsRead == 2047); I wanted to convert this code to use dynamically allocated variables so that when calling this code often I won't get heavy memory leakage. Thus, I tried this: char *buffer = malloc(sizeof(char) * 2047); int *charsRead = malloc(sizeof(int)); do { if(fscanf(file, "%2047[^\n]%n%*c", *buffer, charsRead) == 1) { // Do something } } while (*charsRead == 2047); Unfortunately, this does not work. I always get “EXC_BAD_ACCESS” errors, just before the if-statement with the fscanf call. What am I doing wrong? Thanks for any help! -- Ry

    Read the article

  • Pointer initialization doubt

    - by Jestin Joy
    We could initialize a character pointer like this in C. char *c="test"; Where c points to the first character(t). But when I gave code like below. It gives segmentation fault. #include<stdio.h> #include<stdlib.h> main() { int *i=0; printf("%d",*i); } Also when I give #include<stdio.h> #include<stdlib.h> main() { int *i; i=(int *)malloc(2); printf("%d",*i); } It worked(gave output 0). When I gave malloc(0), it worked(gave output 0). Please tell what is happening

    Read the article

  • C++ Memory Allocation & Linked List Implementation

    - by pws5068
    I'm writing software to simulate the "first-fit" memory allocation schema. Basically, I allocate a large X megabyte chunk of memory and subdivide it into blocks when chunks are requested according to the schema. I'm using a linked list called "node" as a header for each block of memory (so that we can find the next block without tediously looping through every address value. head_ptr = (char*) malloc(total_size + sizeof(node)); if(head_ptr == NULL) return -1; // Malloc Error .. :-( node* head_node = new node; // Build block header head_node->next = NULL; head_node->previous = NULL; // Header points to next block (which doesn't exist yet) memset(head_ptr,head_node, sizeof(node)); ` But this last line returns: error: invalid conversion from 'node*' to 'int' I understand why this is invalid.. but how can I place my node into the pointer location of my newly allocated memory?

    Read the article

  • Another dynamic memory allocation bug.

    - by m4design
    I'm trying to allocate memory for a multidimensional array (8 rows, 3 columns). Here's the code for the allocation (I'm sure the error is clear for you) char **ptr = (char **) malloc( sizeof(char) * 8); for (i = 0; i < 3; i++) ptr[i] = (char *) malloc( sizeof(char) * 3); The crash happens when I reference this: ptr[3][0]; Unhandled exception at 0x0135144d in xxxx.exe: 0xC0000005: Access violation writing location 0xabababab. Are there any recommended references/readings for this kind of subject? Thanks.

    Read the article

  • "Use of uninitialised value" despite of memset

    - by Framester
    Hi there, I allocate a 2d array and use memset to fill it with zeros. #include<stdio.h> #include<string.h> #include<stdlib.h> void main() { int m=10; int n =10; int **array_2d; array_2d = (int**) malloc(m*sizeof(int*)); if(array_2d==NULL) { printf("\n Could not malloc 2d array \n"); exit(1); } for(int i=0;i<m;i++) { ((array_2d)[i])=malloc(n*sizeof(int)); memset(((array_2d)[i]),0,sizeof(n*sizeof(int))); } for(int i=0; i<10;i++){ for(int j=0; j<10;j++){ printf("(%i,%i)=",i,j); fflush(stdout); printf("%i ", array_2d[i][j]); } printf("\n"); } } Afterwards I use valgrind [1] to check for memory errors. I get following error: Conditional jump or move depends on uninitialised value(s) for line 24 (printf("%i ", array_2d[i][j]);). I always thought memset is the function to initialize arrays. How can I get rid off this error? Thanks! Valgrind output: ==3485== Memcheck, a memory error detector ==3485== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al. ==3485== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info ==3485== Command: ./a.out ==3485== (0,0)=0 (0,1)===3485== Use of uninitialised value of size 4 ==3485== at 0x409E186: _itoa_word (_itoa.c:195) ==3485== by 0x40A1AD1: vfprintf (vfprintf.c:1613) ==3485== by 0x40A8FFF: printf (printf.c:35) ==3485== by 0x8048724: main (playing_with_valgrind.c:39) ==3485== ==3485== ==3485== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- ==3485== Conditional jump or move depends on uninitialised value(s) ==3485== at 0x409E18E: _itoa_word (_itoa.c:195) ==3485== by 0x40A1AD1: vfprintf (vfprintf.c:1613) ==3485== by 0x40A8FFF: printf (printf.c:35) ==3485== by 0x8048724: main (playing_with_valgrind.c:39) [1] valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes --db-attach=yes ./a.out [gcc-cmd] gcc -std=c99 -lm -Wall -g3 playing_with_valgrind.c

    Read the article

  • Address of array vs. address of array[0] - C language

    - by user324994
    My question is why does the address of an array differ from the address of its first position? I'm trying to write my own malloc, but to start out I'm just allocating a chunk of memory and playing around with the addresses. My code looks roughly like this: #define BUFF_SIZE 1024 static char *mallocbuff; int main(){ mallocbuff = malloc(BUFF_SIZE); printf("The address of mallocbuff is %d\n", &mallocbuff); printf("The address of mallocbuff[0] is %d\n", &mallocbuff[0]); } &mallocbuff is the same address every time I run it. &mallocbuff[0] is some random address every time. I was expecting the addresses to match each other. Can anyone explain why this isn't the case?

    Read the article

  • Difficulty understanding behavior of free()

    - by Rasmi Ranjan Nayak
    int main() { int *ptr, **ptr1; ptr = (int*)malloc(sizeof(int)); ptr1 = (int**)malloc(sizeof(int)); free(ptr); *ptr = 12345; ptr1 = &ptr; //free(ptr); //**ptr1 = 23456; printf("%d \n", **ptr1); system("pause"); return 0; } How does *ptr store the value 12345, when the memory has already been freed? So, now ptr should be pointing to garbage. Why is this happening?

    Read the article

  • UIImageWriteToSavedPhotosAlbum with malloc_error

    - by lbalves
    I have an NIB file with a button. When I click this button, the setWallpaper: selector is called. Everything works as expected (the image is saved), excepte by the error thrown by malloc. malloc: *** error for object 0x184d000: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug I've set a breakpoint at malloc_error_break, but I don't understand anything from the debugger. I couldn't even find the object 0x184d000. Does anyone know why is this happening? I had also tried to retain the UIImage before sending it to UIImageWriteToSavedPhotosAlbum, but without success. My code is below: - (IBAction)setWallpaper:(id)sender { UIImage *image = [UIImage imageNamed:@"wallpaper_01.png"]; UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Galo!!!",@"Saved image message: title") message:NSLocalizedString(@"Now, check your \"saved photos\" group at \"photos\" app in your iPhone and select the actions menu > set as wallpaper.",@"Saved image message") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"OK Button") otherButtonTitles:nil]; [alertView show]; [alertView release]; }

    Read the article

  • Instruments (Leaks) and NSDateFormatter

    - by Cal
    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 also thought it could be memory that only used during some intermediate step in parsing. Does anyone know which one it is or how to find out? Also, is there a way to put a breakpoint on NSDate dealloc to see if that memory is really being reclaimed? Here's what my date formatter looks like for parsing these dates: df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"EEE, d MMM yyyy H:m:s z"]; Here's the Heaviest Stack trace when the memory bumps up and stays there: 0 libSystem.B.dylib 208.80 Kb malloc 1 libicucore.A.dylib 868.19 Kb icu::ZoneMeta::getSingleCountry(icu::UnicodeString const&, icu::UnicodeString&) 2 libicucore.A.dylib 868.66 Kb icu::ZoneMeta::getSingleCountry(icu::UnicodeString const&, icu::UnicodeString&) 3 libicucore.A.dylib 868.67 Kb icu::ZoneMeta::getSingleCountry(icu::UnicodeString const&, icu::UnicodeString&) 4 libicucore.A.dylib 868.67 Kb icu::DateFormatSymbols::initZoneStringFormat() 5 libicucore.A.dylib 868.67 Kb icu::DateFormatSymbols::getZoneStringFormat() const 6 libicucore.A.dylib 868.67 Kb icu::SimpleDateFormat::subParse(icu::UnicodeString const&, int&, unsigned short, int, signed char, signed char, signed char*, icu::Calendar&) const 7 libicucore.A.dylib 868.67 Kb icu::SimpleDateFormat::parse(icu::UnicodeString const&, icu::Calendar&, icu::ParsePosition&) const 8 libicucore.A.dylib 868.67 Kb icu::DateFormat::parse(icu::UnicodeString const&, icu::ParsePosition&) const 9 libicucore.A.dylib 868.67 Kb udat_parse 10 CoreFoundation 868.67 Kb CFDateFormatterGetAbsoluteTimeFromString 11 CoreFoundation 868.67 Kb CFDateFormatterCreateDateFromString 12 Foundation 868.67 Kb -[NSDateFormatter getObjectValue:forString:range:error:] 13 Foundation 868.75 Kb -[NSDateFormatter getObjectValue:forString:errorDescription:] 14 Foundation 868.75 Kb -[NSDateFormatter dateFromString:] Thanks!

    Read the article

  • why pointer to pointer is needed to allocate memory in function

    - by skydoor
    Hi I have a segmentation fault in the code below, but after I changed it to pointer to pointer, it is fine. Could anybody give me any reason? void memory(int * p, int size) { try{ p = (int *) malloc(size*sizeof(int)); } catch( exception& e) { cout<<e.what()<<endl; } } it does not work in the main function as blow int *p = 0; memory(p, 10); for(int i = 0 ; i < 10; i++) p[i] = i; however, it works like this . void memory(int ** p, int size) { `//pointer to pointer` try{ *p = (int *) malloc(size*sizeof(int)); } catch( exception& e) { cout<<e.what()<<endl; } } int main() { int *p = 0; memory(&p, 10); //get the address of the pointer for(int i = 0 ; i < 10; i++) p[i] = i; for(int i = 0 ; i < 10; i++) cout<<*(p+i)<<" "; return 0; }

    Read the article

  • How can I declare and initialize an array of pointers to a structure in C?

    - by worlds-apart89
    I have a small assignment in C. I am trying to create an array of pointers to a structure. My question is how can I initialize each pointer to NULL? Also, after I allocate memory for a member of the array, I can not assign values to the structure to which the array element points. #include <stdio.h> #include <stdlib.h> typedef struct list_node list_node_t; struct list_node { char *key; int value; list_node_t *next; }; int main() { list_node_t *ptr = (list_node_t*) malloc(sizeof(list_node_t)); ptr->key = "Hello There"; ptr->value = 1; ptr->next = NULL; // Above works fine // Below is erroneous list_node_t **array[10] = {NULL}; *array[0] = (list_node_t*) malloc(sizeof(list_node_t)); array[0]->key = "Hello world!"; //request for member ‘key’ in something not a structure or union array[0]->value = 22; //request for member ‘value’ in something not a structure or union array[0]->next = NULL; //request for member ‘next’ in something not a structure or union // Do something with the data at hand // Deallocate memory using function free return 0; }

    Read the article

  • Can someone tell me why I'm seg faulting in this simple C program?

    - by user299648
    I keep on getting seg faulted, and for the life of me I dont why. The file I'm scanning is just 18 strings in 18 lines. I thinks the problem is the way I'm mallocing the double pointer called picks, but I dont know exactly why. I'm am only trying to scanf strings that are less than 15 chars long, so I don't see the problem. Can someone please help. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LENGTH 100 int main( int argc,char *argv[] ) { char* string = malloc( sizeof(char) ); char** picks = malloc(15*sizeof(char)); FILE* pick_file = fopen( argv[l], "r" ); int num_picks; for( num_picks=0 ; fgets( string, MAX_LENGTH, pick_file ) != NULL ; num_picks++ ) { printf("pick a/an %s ", string ); scanf( "%s", picks+num_picks ); } int x; for(x=0; x<num_picks;x++) printf("s\n", picks+x); }

    Read the article

  • Can some tell me why I am seg faulting in this simple C program?

    - by user299648
    I keep on getting seg faulted after I end my first for loop, and for the life of me I don't why. The file I'm scanning is just 18 strings in 18 lines. I thinks the problem is the way I'm mallocing the double pointer called picks, but I don't know exactly why. I'm am only trying to scanf strings that are less than 15 chars long, so I don't see the problem. Can someone please help. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LENGTH 100 int main( int argc,char *argv[] ) { char* string = malloc( 15*sizeof(char) ); char** picks = malloc(15*sizeof(char*)); FILE* pick_file = fopen( argv[l], "r" ); int num_picks; for( num_picks=0 ; fgets( string, MAX_LENGTH, pick_file ) != NULL ; num_picks++ ) { scanf( "%s", picks+num_picks ); } //this is where i seg fault int x; for(x=0; x<num_picks;x++) printf("s\n", picks+x); }

    Read the article

  • How can i create an n-dimensional array in c

    - by shortCircuit
    I was thinking of making a function that would accept the size of array as a parameter and create a n dimensional array. My room-mate took the liberty of making it complex. He said lets write a function that takes n parameters and returns an n-dimensional array using those parameters as the dimensions. Now i realize an one-day and d array is easy to implement with pointers. For 2d array the snippet would be something like (standard way) : int** x; int* temp; x = (int**)malloc(m * sizeof(int*)); temp = (int*)malloc(m*n * sizeof(int)); for (int i = 0; i < m; i++) { x[i] = temp + (i * n); } where the array is of size m*n; But the problem lies how do we find the nested loop parameters for a n-dimensional array? Is there any way to optimize the code?

    Read the article

  • glibc detected ./.a.out: free(): invalid pointer

    - by ExtremeBlue
    typedef struct _PERSON { size_t age; unsigned char* name; }PERSON; int init(PERSON** person) { (* person) = (PERSON *) malloc(sizeof(struct _PERSON)); (* person)->age = 1; (* person)->name = (unsigned char *) malloc(sizeof(4)); (* person)->name = "NAME"; return 0; } void close(PERSON** person) { (* person)->age = 0; if((* person)->name != NULL) { free((* person)->name); } if((* person) != NULL) { free((* person)); } } int main(int argc, char* argv[]) { PERSON* p; init(&p); printf("%d\t%s\n", (int) p->age, p->name); close(&p); return 0; } 1 NAME *** glibc detected *** ./a.out: free(): invalid pointer: 0x000000000040079c *** ======= Backtrace: ========= /lib/libc.so.6(+0x774b6)[0x7fa9027054b6] /lib/libc.so.6(cfree+0x73)[0x7fa90270bc83] ./a.out(close+0x3d)[0x400651] ./a.out[0x40069f] /lib/libc.so.6(__libc_start_main+0xfe)[0x7fa9026acd8e] ./a.out[0x4004f9] ... 7fa8fc000000-7fa8fc021000 rw-p 00000000 00:00 0 7fa8fc021000-7fa900000000 ---p 00000000 00:00 0 7fa902478000-7fa90248d000 r-xp 00000000 08:12 23068732 /lib/libgcc_s.so.1 7fa90248d000-7fa90268c000 ---p 00015000 08:12 23068732 /lib/libgcc_s.so.1 7fa90268c000-7fa90268d000 r--p 00014000 08:12 23068732 /lib/libgcc_s.so.1 7fa90268d000-7fa90268e000 rw-p 00015000 08:12 23068732 /lib/libgcc_s.so.1 7fa90268e000-7fa902808000 r-xp 00000000 08:12 23068970 /lib/libc-2.12.1.so 7fa902808000-7fa902a07000 ---p 0017a000 08:12 23068970 /lib/libc-2.12.1.so 7fa902a07000-7fa902a0b000 r--p 00179000 08:12 23068970 /lib/libc-2.12.1.so 7fa902a0b000-7fa902a0c000 rw-p 0017d000 08:12 23068970 /lib/libc-2.12.1.so 7fa902a0c000-7fa902a11000 rw-p 00000000 00:00 0 7fa902a11000-7fa902a31000 r-xp 00000000 08:12 23068966 /lib/ld-2.12.1.so 7fa902c25000-7fa902c28000 rw-p 00000000 00:00 0 7fa902c2e000-7fa902c31000 rw-p 00000000 00:00 0 7fa902c31000-7fa902c32000 r--p 00020000 08:12 23068966 /lib/ld-2.12.1.so 7fa902c32000-7fa902c33000 rw-p 00021000 08:12 23068966 /lib/ld-2.12.1.so 7fa902c33000-7fa902c34000 rw-p 00000000 00:00 0 7fff442d5000-7fff442f6000 rw-p 00000000 00:00 0 [stack] 7fff44308000-7fff44309000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] Aborted

    Read the article

  • Using malloc/free in Objective-C object

    - by Itamar Katz
    I have a class AudioManager with a member of type AudioBufferList *. (This is a struct declared in the CoreAudio framework). Since AudioBufferList is not a NSObject, I cannot retain it, so I have to alloc/free it (correct me if I'm wrong). My question is, where is the 'right' place to free it? Currently I am doing it in the dealloc method of AudioManager. If I understand correctly, this method is invoked automatically once the release message is sent to the instance of AudioManager --- is that true? Is there any other recommended practice regarding using alloc/free on non-objects members of Objective-C objects? Edit: From Apples documentation: Subclasses must implement their own versions of dealloc to allow the release of any additional memory consumed by the object—such as dynamically allocated storage for data or object instance variables owned by the deallocated object. After performing the class-specific deallocation, the subclass method should incorporate superclass versions of dealloc through a message to super: Which makes things a little bit clearer - but more insights are appreciated.

    Read the article

  • How to find the leaky faucet that loads into Malloc 32kb

    - by Rob
    I have been messing around with Leaks trying to find which function is not being deallocated (I am still new to this) and could really use some experienced insight. I have this bit of code that seems to be the culprit. Every time I press the button that calls this code, 32kb of memory is additionally allocated to memory and when the button is released that memory does not get deallocated. What I found was that everytime that AVAudioPlayer is called to play an m4a file, the final function to parse the m4a file is MP4BoxParser::Initialize() and this in turn allocates 32kb of memory through Cached_DataSource::ReadBytes My question is, how do I go about deallocating that after it is finished so that it doesn't keep allocating 32kb every time the button is pressed? Any help you could provide is greatly appreciated! - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //stop playing theAudio.stop; // cancel any pending handleSingleTap messages [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTap) object:nil]; UITouch* touch = [[event allTouches] anyObject]; NSString* filename = [g_AppsList objectAtIndex: [touch view].tag]; NSString *path = [[NSBundle mainBundle] pathForResource: filename ofType:@"m4a"]; theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; theAudio.delegate = self; [theAudio prepareToPlay]; [theAudio setNumberOfLoops:-1]; [theAudio setVolume: g_Volume]; [theAudio play]; }

    Read the article

  • freeing malloc and checkin it is empty or not

    - by gcc
    char *p; p="kjkjk"; . .//there are codes which are checking another command . if(.....)//i used pointer p in only that area free(p); . . //there are codes which are checking another command . if(p==NULL) //i check whether is empty .... if(p==-1) //can we use "EOF==p " in if statement ... //are there any usage like that EOF==p else .... I think there is big error , but where?

    Read the article

  • command&pointer&malloc [closed]

    - by gcc
    input 23 3 4 4 42 n 23 0 9 9 n n n 3 9 9 x //according to input,i should create int pointer arrays. pointer arrays starting from 1 (that is initial arrays is arrays[1].when program sees n ,it must be jumb to arrays 2 expected output arrays[1] 3 4 5 42 arrays[2] 23 0 9 9 arrays[5] 3 9 9 x is stopper n is comman to create new pointer array i am new in this site anyone help me how can i write

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >