ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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 ?
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))
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).
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 ?
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)
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.
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.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.