LinuxQuestions.org
Help answer threads with 0 replies.
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 08-07-2012, 01:30 AM   #1
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Rep: Reputation: Disabled
Array behavior


Hello everyone,
I am confused with output of this program, Array doesn't have enough memory to store the string, but still it is working without segfault.

Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
    char *source_str = "Some message here\n";
    char str[strlen(source_str)*2];
    int i;
    printf("%d | %d\n",strlen(source_str),sizeof(str)); 
    strcpy(str, source_str);
    for(i=0; i<3; i++)
        strcat(str, source_str);
    printf("%s\n", str);
    printf("%d | Size : %d | Length : %d\n",strlen(source_str),sizeof(str),strlen(str));
    return 0;
}
 
Old 08-07-2012, 01:40 AM   #2
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Original Poster
Rep: Reputation: Disabled
One more thing I noticed, if I replace
Code:
char str[strlen(source_str)*2];
with
Code:
char str[37];
now it generates segmentation. It means, this behavior is the result how memory is allocated. What is the difference in these two allocations.
When I enable -pedantic flag, Compiler warned me for it, still I want to know what expert says about this.
 
Old 08-07-2012, 02:15 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
char str[strlen(source_str)*2];
There is no such thing in C (only in some non-portable extensions). Use this:

Code:
char *str= malloc (2*strlen (source_str));
 
Old 08-07-2012, 02:27 AM   #4
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Original Poster
Rep: Reputation: Disabled
Yes ! I read the same for C that Variable are not allowed while declaring an array, Value must be a constant, but gcc allowed me to do so. without -pedantic, it didn't warn me.
 
Old 08-07-2012, 05:06 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The following bug should also be addressed:
Code:
    for(i=0; i<3; i++)
        strcat(str, source_str);
Here you are appending (3 times!) the entire source_str to the contents of str, of which btw, already has a copy of source_str.

Thus str will have a total of 4 copies of source_str... if the system permits it. If not, a segfault will occur because you have only allocated two times the length of source_str for str.

When copying strings from a buffer to a char pointer, make sure the latter has enough space. This implies the length of the source string (or strings) and an extra character for the NULL-terminating byte. The length returned by strlen() does not include the NULL-terminating byte.
 
Old 08-07-2012, 05:14 AM   #6
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
if the system permits it. If not, a segfault will occur because you have only allocated two times the length of source_str for str.
I think as you. but What it means, system permits. don't you think it might lead us to an undefined behavior.

One more think when It was 37 in place of (strlen(source_str)*2+1), I got segfault. That should be. But what is changing with strlen, why not segfault. Is it related to memory from stack/heap ?
 
Old 08-07-2012, 05:22 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, it is a typical example of undefined behavior.
If you like to get segfaults, link your programs with Electric Fence (-lefence at linkage, whence you have installed the package (eg in Debian: apt-get install electric-fence))
 
Old 08-07-2012, 09:13 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by NevemTeve View Post
Code:
char str[strlen(source_str)*2];
There is no such thing in C (only in some non-portable extensions).
It's allowed in C99.
 
Old 08-07-2012, 10:56 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, but it is a non-portable extension too
The world is full with old compilers that don't support it. Okay, if it were something big and revolutionary, then I'd say 'okay, let's use it', but it isn't the case, it's practically a change for the sake of the change (another example is allowing //-style comments in C).
 
Old 08-08-2012, 12:42 AM   #10
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Original Poster
Rep: Reputation: Disabled
Why we need to allocate dynamic memory from heap ? can't we use stack for the same ? it might be a bad question, but what is the difference between the memories either allocated from heap or stack. Will both work with same behavior ?
 
Old 08-08-2012, 02:53 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
If the array's scope is the actual procedure, then it doesn't matter. But if, for example, it should be returned from the function, it cannot be a stack-variable.

Note: there is an alloca function to allocate variable amount of memory on the stack which will automagically released when function terminates, but it doesn't exist in every platform (meaning: don't use in non-trivial programs)

Last edited by NevemTeve; 08-08-2012 at 02:55 AM.
 
Old 08-08-2012, 03:11 AM   #12
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Original Poster
Rep: Reputation: Disabled
Code:
void funcToTestDynamicMemory()
{
char *t; //Memory from stack
t=malloc(1024); //Memory from heap
}
int main()
{
funcToTestDynamicMemory();
return 0;
}
What will happen to this allocated space ? I started to Google but people some people says that it will remain as it is, because when a function ends, stack (temporary memory) is cleared only, heap (permanent memory) still exist.
 
Old 08-08-2012, 03:21 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
True, you will lose 1024 bytes (+overhead) memory every time you call this function in your program. (Of course when the program terminates, the kernel releases its whole data segment, so you don't have to reboot your computer to solve this.)
 
  


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
[SOLVED] mdadm: only give one device per ARRAY line: /dev/md/:raid and array laughing_man77 Linux - Hardware 4 03-23-2012 04:05 PM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM

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

All times are GMT -5. The time now is 08:05 PM.

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