Code: |
#include <stdio.h> #include <stdlib.h> char *string; main () { string = malloc(5); char *string = "Matt"; printf("%s rocks!!\n",string); return 0; } |
MattA wrote: |
Is this the correct use of Malloc? It's just i noticed it works just as well without the string = malloc(5) section. |
Code: |
string = malloc(5); |
Code: |
char *string = "Matt"; |
Code: |
printf("%s rocks!!\n",string); |
Code: |
char *string = malloc( 5 );
snprintf( string, 4, "%s", "Matt" ); printf( "%s rocks\n", string); free( string ); |
hugo wrote: |
There you are assigning a new variable 'string', where basically the compiler does some typecasting and actually does this:
the pointer that string points to shall be pointed to the address of the character of the string 'Matt' |
hugo wrote: | ||
Then,
prints said string, but the malloc( ) memory is gone, you have no reference to it anymore. |
hugo wrote: | ||
|
Code: |
char *tmp; int i=0; while(i<=3000){ if((tmp=(char*)malloc(sizeof(char)*strlen("TEST")))==NULL){ perror("malloc"); exit(1); } bzero(tmp,strlen(temp)); strncpy(tmp,"TEST",strlen("TEST"); fprintf(stdout,"%s\n",tmp); free(tmp); } |
Code: |
char *tmp; int i=0; while(i<=3000){ if((tmp=(char*)malloc(sizeof(char)*strlen("TEST")))==NULL){ perror("malloc"); exit(1); } bzero(tmp,strlen(temp)); strncpy(tmp,"TEST",strlen("TEST"); fprintf(stdout,"%s\n",tmp); } free(tmp); |
Zarnick wrote: | ||||
Going allong with the free, wich one of these two codes are more acurate?
Or this:
Where should I put the free?On the first one, after It runs a couple of time, it throws me an invalid free pointer error, and the other works fine, but shouldn't i use realloc instead of malloc for the second version??? Thanks a lot. Zarnick |
Code: |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <sys/types.h> #include <sys/stat.h> void usage(char *pname){ fprintf(stdout,"Usage:%s <dir>\n",pname); exit(0); } int main(int argc, char *argv[]){ DIR *dir=NULL; struct dirent *entry=NULL; struct stat info; char *tmpPath=NULL; int tmpSize=0; int fsize=0; int dsize=0; if(argc!=2){ usage(argv[0]); } if((dir=opendir(argv[1]))==NULL){ perror("opendir"); exit(1); } while((entry=readdir(dir))!=NULL){ fprintf(stdout,"[*]Name=%s\n",entry->d_name); tmpSize=strlen(argv[1])+strlen(entry->d_name)+1; if((tmpPath=(char*)malloc(sizeof(char)*tmpSize))==NULL){ perror("malloc"); exit(2); } bzero(tmpPath,tmpSize); snprintf(tmpPath,tmpSize,"%s%s",argv[1],entry->d_name); if(tmpPath[tmpSize]!='\0'){ tmpPath[tmpSize]='\0'; } lstat(tmpPath,&info); if(S_ISREG(info.st_mode)){ fsize=info.st_size; dsize+=fsize; fprintf(stdout,"[*]File Size=%d\n",fsize); } } fprintf(stdout,"[*]Dir Size=%d\n",dsize); closedir(dir); free(tmpPath); return 0; } |
Quote: |
. .(strip) . [*]Name=gconfsharp-schemagen [*]File Size=63 [*]Name=ginstall [*]Name=Mail [*]Name=boot-admin [*]File Size=90236 *** glibc detected *** free(): invalid next size (fast): 0x0804a018 *** Aborted |
Zarnick wrote: |
Going allong with the free, wich one of these two codes are more acurate? |
Code: |
char *tmp = (char*)malloc(sizeof(char)*strlen("TEST")); int i=0; if(tmp==NULL){ perror("malloc"); exit(1); } while(i<=3000){ bzero(tmp,strlen(temp)); strncpy(tmp,"TEST",strlen("TEST"); fprintf(stdout,"%s\n",tmp); } free(tmp); |
Code: |
char *tmp; int i=0; while(i<=3000){ if((tmp=(char*)malloc(sizeof(char)*strlen("TEST")))==NULL){ perror("malloc"); exit(1); } bzero(tmp,strlen(temp)); strncpy(tmp,"TEST",strlen("TEST"); fprintf(stdout,"%s\n",tmp); free(tmp); } |
Zarnick wrote: |
Ok, so it did help, what I've meant with the example was that I was having a similar problem(as you can see on my program that reads a directory), after that I've tried to create the example in my pc and it worked fine, both versions(of course, the second one had a balantly memory leak), which makes me think that the error I'm getting may not be because of malloc or free, however if it isn't this, I really don't know what it is, one thing I've tried was to substitute tmpSize, to 1000(hard coded), and it worked fine, no free corruption...however this is just not elegant, and may cause to a lot of problems(for example, a complete directory with more then 1000 chars).
Any clue on what must be the problem??? As you can see I'm running Linux...(these behavior was seen on both Slackware and Gentoo, and even FreeBSD, wich makes me think that it IS a programer(that's me) error)... |
Zarnick wrote: |
Yeap, however if I put it inside the while, I get an free corruption error. :O
It seems that fixing the size to 255 does work, and may be assumed as a workaroung, I can put the free inside the while and not leaking memory, however this is just nonsense, if I fix in 500 I can read only one item inside the directory If this makes sense to any of you, please do enlight me.... |
Code: |
#include <stdio.h>
#include <stdlib.h> #include <string.h> #include <dirent.h> #include <sys/types.h> #include <sys/stat.h> #include <unix.h> void usage(char *pname){ fprintf(stdout,"Usage:%s <dir>\n",pname); exit(0); } int main(int argc, char** argv){ DIR *dir=NULL; struct dirent *entry=NULL; struct stat info; char *tmpPath=NULL; char *tmp=NULL; int tmpSize=0; int fsize=0; int dsize=0; if(argc!=2){ usage(argv[0]); } if((dir=opendir(argv[1]))==NULL){ perror("opendir"); exit(1); } while((entry=readdir(dir))!=NULL){ fprintf(stdout,"[*]Name=%s\n",entry->d_name); tmpSize=strlen(argv[1])+strlen(entry->d_name)+1; if(tmpPath == NULL) { if((tmpPath=malloc(sizeof(char) * tmpSize))==NULL) { perror("malloc"); exit(2); } } else { if((tmp=realloc(tmpPath,sizeof(char) * tmpSize))==NULL) { printf("Memory Allocation Failed\n"); free(tmpPath); exit(2); } tmpPath=tmp; } bzero(tmpPath,tmpSize); snprintf(tmpPath,tmpSize,"%s%s",argv[1],entry->d_name); if(tmpPath[tmpSize]!='\0'){ tmpPath[tmpSize]='\0'; } lstat(tmpPath,&info); if(S_ISREG(info.st_mode)){ fsize=info.st_size; dsize+=fsize; fprintf(stdout,"[*]File Size=%d\n",fsize); } } fprintf(stdout,"[*]Dir Size=%d\n",dsize); free(tmpPath); closedir(dir); return 0; } |
Code: |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> void usage(char *pname){ fprintf(stdout,"Usage:%s <dir>\n",pname); exit(0); } int main(int argc, char *argv[]){ DIR *dir=NULL; struct dirent *entry=NULL; struct stat info; char *tmpPath=NULL; int tmpSize=0; int fsize=0; int dsize=0; if(argc!=2){ usage(argv[0]); } if((dir=opendir(argv[1]))==NULL){ perror("opendir"); exit(1); } while((entry=readdir(dir))!=NULL){ sleep(0.5); fprintf(stdout,"[*]Name=%s\n",entry->d_name); tmpSize=strlen(argv[1])+strlen(entry->d_name); if((tmpPath=(char*)malloc(sizeof(char)*tmpSize))==NULL){ perror("malloc"); exit(2); } bzero(tmpPath,tmpSize); strncpy(tmpPath,argv[1],strlen(argv[1])); strncat(tmpPath,entry->d_name,strlen(entry->d_name)); if(tmpPath[tmpSize]!='\0'){ tmpPath[tmpSize]='\0'; } lstat(tmpPath,&info); if(S_ISREG(info.st_mode)){ fsize=info.st_size; dsize+=fsize; } bzero(tmpPath,strlen(tmpPath)); free(tmpPath); } fprintf(stdout,"[*]Dir Size=%d\n",dsize); closedir(dir); //free(tmpPath); return 0; } |
Quote: |
*** glibc detected *** free(): invalid next size (fast): 0x0804b028 *** |
Quote: |
*** glibc detected *** realloc(): invalid next size: 0x0804a008 *** |
output generated using printer-friendly topic mod, All times are GMT + 2 Hours