473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why do I have a core dump ?

QQ
I can sense that I get wrong with char *name,
however, how to fix it ?

Thanks a lot!
int Roamer_Create(const char *dbname)
{
char *name;
printf("dbname is %s\n",dbname);
sprintf(name,"CREATE DATABASE %s",dbname);
printf("command is %s\n",name);
return 1;
}
main()
{
Roamer_Create("Roamer");
}

Nov 14 '05 #1
1 1383
QQ <ju****@yahoo.com> wrote:
I can sense that I get wrong with char *name,
however, how to fix it ? int Roamer_Create(const char *dbname)
{
char *name;
Now you have a pointer that points to some random place in memory.
If you're lucky it points to a place that you have no permissions
to access.
printf("dbname is %s\n",dbname);
sprintf(name,"CREATE DATABASE %s",dbname);
Now you try to write to that random place in memory. You're lucky
that the program crashed instead of blissfully overwriting some
important data of your program, making it look like everything is
ok before crashing horribly (and unexplainably) half an hour later...

To get rid of the problem you need to make 'name' point to some
memory you own, large enough to hold the string you want to put
there (including the the terminating '\0' character.

You could do use e.g.

if ( ( name = malloc( strlen( "CREATE DATABASE " ) +
strlen( dbname ) + 1 ) ) == NULL ) {
fprintf( stderr, "Running out of memory!\n" );
exit( EXIT_FAILURE );
}

before you call sprintf(). Don't forget to include <stdlib.h>, it's
needed for both the definition of malloc() and the define for the
macro EXIT_FAILURE.
printf("command is %s\n",name);
return 1;
} main()
This should be

int main( void )

since main() is always supposed to return an int and unless you declare
it to do so you'll get into trouble with C99 compilers.
{
Roamer_Create("Roamer");
}


And since main() must return an int you also should have a return
statement at the end.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick Craig-Wood | last post by:
I've just discovered that my python (Python 2.3.4 from debian package 2.3.4-1 running on debian testing x86 + linux 2.4.26) core dumps when I set recursionlimit very high and do lots of recursion....
1
by: Martin | last post by:
I use dbx and i got the following error: Reading GL_CliConnMgr core file header read successfully Reading ld.so.1 dbx: core file read error: address 0xff3e6000 not available dbx: core file...
10
by: ken | last post by:
hello, i'm writing a c program on a linux system. i'm debugging a segmentation fault but i don't want it to dump a core file because the memory footprint of the program is over 300Mb and i don't...
1
by: invincible | last post by:
hi, assume my program is running , is there a way I can dump its core in solaris, in linux i think u can use gcore Mohan
10
by: John Liu | last post by:
We upgraded from 7.2 to 7.4, it looks like everything working, but when I issue a query such as select * from tab (tab has about 2-3 million records), it causes core dump. I tuned some the...
3
by: John Liu | last post by:
AIX pg version 7.4 Select * from document2 core dump. Did a few more experiments with select * from document2 limit... I limit to 500000 it works, 600000 it exits but says "calloc:...
4
by: madhusudan.hv | last post by:
hi, Can you tell me when an application might take long time to dump core? The linux o.s is indicating that a process is dumping core, and only after 40 mins i am seeing the core file. What might...
10
by: wong_powah | last post by:
I want to find out where (which line) my C program core dump. How to do that? Is there a web site describing the procedure? One approach is to use stack trace of the mdb debugger, but I does not...
14
by: Sheldon | last post by:
Hi, I have a python script that uses a C extention. I keep getting a recurring problem that causes a core dump a few lines after the C extention return data back tp python. I tried using pbd and...
5
by: johnericaturnbull | last post by:
Hi - I am very new to python. I get this random core dump and am looking for a good way to catch the error. I know the function my core dump occurs. Is there any error catching/handling that I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.