LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2006, 04:36 AM   #1
simopal6
Member
 
Registered: Jun 2006
Location: Italy
Distribution: Slackware 13.1
Posts: 230

Rep: Reputation: 30
Segmentation fault with char* array


Hi to all! I'm new to the forum, and i need urgent help!
If i try to run this program:

#include <stdio.h>
int main()
{
char *str[3];
int i;
for(i=0; i<3; i++)
{
printf("i: %d\n", i);
sprintf(str[i], "string_%d", i);
}
}


i get segmentation fault when i=1 after sprintf.
What might cause this problem?
Thank you!
 
Old 06-15-2006, 05:29 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
This part: char *str[3]; creates an array pointers to chars. You haven't created strings for them to point to or reserved memory for the strings. The pointers may be NUL also.
 
Old 06-15-2006, 06:15 AM   #3
simopal6
Member
 
Registered: Jun 2006
Location: Italy
Distribution: Slackware 13.1
Posts: 230

Original Poster
Rep: Reputation: 30
Mmm..How do you think i can solve this problem...
I need an array of strings to which assign values dynamically.
Thank you!
 
Old 06-15-2006, 07:28 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 06-15-2006, 07:35 AM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Quote:
Originally Posted by simopal6
Mmm..How do you think i can solve this problem...
I need an array of strings to which assign values dynamically.
Thank you!
Your going to have to allocate space for the strings before you assign them.

I'd start by assigning all the pointers to NULL at the declaration.

Code:
char *str[3];
for (i = 0; i < 3; i++) {
     str[i] = NULL;
}
Then I would allocate memory for the str[i] in the loop before assigning it. What I've done below allows it to take in a string of 20 characters or less. There are probably some more elegant solutions, but I'll start with a simple example and we will see where it goes.

You'll also notice I used snprintf instead of sprintf. If you are dealing with cstrings (aka arrays of characters) it is always wise to use snprintf to ensure you don't try and copy more data then the string can hold into it. Not doing this can lead to wierd behavor as a result of buffer overflows.

Code:
if (str[i] == NULL) {
     str[i] = malloc (sizeof(char) * 20);
}
snprintf(str[i] ,20, "string_%d", i);
 
Old 06-15-2006, 09:20 AM   #6
simopal6
Member
 
Registered: Jun 2006
Location: Italy
Distribution: Slackware 13.1
Posts: 230

Original Poster
Rep: Reputation: 30
Yes, if i set the pointers to NULL and malloc enough memory for them, it works! I didn't use snprintf but i was careful to not put more data then the amount i malloc'd.
So, if i've understood the situation, the problem was that i put data in memory starting from pointers i had not allocated memory to, so the characters i wrote went out of the space allocated for the process, is this correct??
Thank you very much, also for answering so fast!! And please forgive my english, hope i didn't make too many mistakes! :-)
 
Old 06-15-2006, 09:34 AM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Quote:
Originally Posted by simopal6
So, if i've understood the situation, the problem was that i put data in memory starting from pointers i had not allocated memory to, so the characters i wrote went out of the space allocated for the process, is this correct??
Sort of. When you declare a variable in C it does not initialize it to any particular value. In the case of a pointer this means it could theoretically be pointed anywhere. When you write to it either you'll get a segmentation fault because you attempt to write to memory you don't have permissions to write too or if you get lucky (or unlucky as the case might be) it will appear to succeed and write to where ever it happens to be pointing too. In the second case you have potentially overwritten something important like other variables in your program.
 
Old 06-16-2006, 08:15 AM   #8
simopal6
Member
 
Registered: Jun 2006
Location: Italy
Distribution: Slackware 13.1
Posts: 230

Original Poster
Rep: Reputation: 30
Ok..Got it! Thanks very much for you explanation!
 
Old 06-16-2006, 01:05 PM   #9
elyk1212
Member
 
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
Yeah, always initialize pointers to null if you are not going to malloc them on the next line, or you will run the risk of strange behavior when you forget. It can be a debugging nightmare that can be avoided with proper planning.

The inverse of this is, always check that the pointer is not null before writing to it. Then, don't forget to free the memory when you are done with the heap pointer.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how big can be an array to give segmentation fault!!! mlaich Programming 7 01-21-2006 07:58 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault after declaring a large array. oulevon Programming 6 11-08-2005 02:41 AM
search in char array xxfunkxx Programming 2 12-12-2004 11:23 PM
segmentation fault when array size exceed 1GB ymei Programming 14 11-11-2003 10:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration