Free() - Program getting aborted.


 
Thread Tools Search this Thread
Top Forums Programming Free() - Program getting aborted.
# 1  
Old 03-11-2014
Free() - Program getting aborted.

I am trying to learn C and while trying out some code, the program is getting aborted while I am calling free().

Here is the code:
Code:
#include <stdlib.h>
#include <stdio.h>

void print_by_ptr(char **str) {
    printf("<%s>\n",*str);
    printf("Now I am modifying the str.\n");
    *str = "Hello there is modified by the function";
    printf("Printing inside the function\n");
    printf("<%s>\n",*str);
}

int main() {
    char *mystr = malloc(100 * sizeof(char));
    mystr = "Hello There!!";
    print_by_ptr(&mystr);
    printf("Printing in main()\n");
    printf("<%s>\n",mystr);
    //mystr = NULL;
    free(mystr);
    printf("Now exit\n");
    return 0;

}

and here is the output Im getting:
Code:
<Hello There!!>
Now I am modifying the str.
Printing inside the function
<Hello there is modified by the function>
Printing in main()
<Hello there is modified by the function>
Aborted

Can anyone tell me why is it getting aborted?
# 2  
Old 03-11-2014
char initialization like what you did back there can only be done along with delcaration.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void print_by_ptr(char **str) {
    printf("<%s>\n",*str);
    printf("Now I am modifying the str.\n");
    strcpy(*str, "Hello there is modified by the function");
    printf("Printing inside the function\n");
    printf("<%s>\n",*str);
}

int main() {
    char *mystr = malloc(100 * sizeof(char));
    strcpy(mystr, "Hello There!!");
    print_by_ptr(&mystr);
    printf("Printing in main()\n");
    printf("<%s>\n",mystr);
    //mystr = NULL;
    free(mystr);
    printf("Now exit\n");
    return 0;
}

# 3  
Old 03-11-2014
Thanks ahamed101, now it is working fine.

Could you pls explain why that error was coming?
# 4  
Old 03-11-2014
mystr is basically having the address. And when you do mystr = "Hello..."; you are altering the address rather than modifying the content in that address.
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Need a FTP SERVER Program FREE

I need a FTP SERVER program for using in ubuntu, Do you know any for free? ps: there will be no internet connection so, vsftp will not work. need a tool (like filezilla but filezilla has no server version for unix ubuntu) that has to be installation package to extract in it. (6 Replies)
Discussion started by: F@NTOM
6 Replies

2. Solaris

backup aborted

hi all I am getting following error while taking backup using the command ufsdump 0ubf 512 /dev/rmt/0cbn /database/backup2/rman_backup/level0 >> /database/backup2/backup_tape/level0_rman_06sep12 2>&1; from the log i got the error bash# tail -f level0_rman_06sep12 DUMP: Date of... (3 Replies)
Discussion started by: nikhil kasar
3 Replies

3. Programming

Why do I receive Program received signal SIGABRT, Aborted?

Im using gdb and when a user disconnects from my server I receive a message Program received signal SIGABRT, Aborted. 0x7ffe0304 in ?? () I was hoping someone here might have a explination for this message in gdb (26 Replies)
Discussion started by: Errigour
26 Replies

4. AIX

HPOV : coda is aborted

the service coda is aborted in HPOV the OS is AIX 6.1 , i reinstall it but its give me the same message , i remove coda files under /var/opt/OV/datafiles and i restart the service but its aborted agine . ./ovc -status ovcd OV Control CORE (44302556) Running agtrep OV Discovery Agent... (8 Replies)
Discussion started by: thecobra151
8 Replies

5. Cybersecurity

cvs [login aborted]

Hi, I installed CVSNT on PC and trying to checkout files from/on linux box. Logging in to :pserver:user1@16.145.134.210:22/DBMONITOR/CVSROOT CVS password: cvs : unrecognized auth response from 16.145.134.210: SSH-2.0-OpenSSH_5.4 ssh is ok. $ ssh user1@16.145.134.210... (1 Reply)
Discussion started by: zam
1 Replies

6. Programming

How to get free disk space size in C/C++ program( Solaris system)

How to get free disk space size in C/C++ program( Solaris system)? Is there any standard function or system function? Just like "df" or "getdfree" in Linux. (4 Replies)
Discussion started by: yidu
4 Replies

7. Shell Programming and Scripting

aborted due to compilation error

I wanted to edit the time for a cron job that cleans out the quarantine in mailscanner. etc/cron.daily/clean.quarantine I edited the file #!/usr/bin/perl # # IMPORTANT NOTE: # # Change the next line to 0 instead of 1 to enable this script. # By default it will be disabled and... (2 Replies)
Discussion started by: mcraul
2 Replies

8. Programming

Program received signal SIGABRT, Aborted.

I ran degugger in C++ and the followings are the message I got: Program received signal SIGABRT, Aborted. 0x002a57a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2 (gdb) info s #0 0x002a57a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2 #1 0x002e97f5 in raise () from /lib/tls/libc.so.6... (1 Reply)
Discussion started by: napapanbkk
1 Replies

9. UNIX for Advanced & Expert Users

BEGIN failed--compilation aborted

i m trying to run a command from diffrent login ,where i get following messages Empty compile time value given to use lib at /svm_wl1/billing/atai/rel/server/util/ataprocs line 73 Empty compile time value given to use lib at /svm_wl1/billing/atai/rel/server/util/ataprocs line 73 Empty... (1 Reply)
Discussion started by: abhijeetkul
1 Replies
Login or Register to Ask a Question