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.
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.
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.
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! :-)
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.