[SOLVED] compiler claims memory that was already malloc-ed was not malloc-ed
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.
.../arrays$ ls -l string_simple.c printStr.h strIsEmpty.h
-rw-rw-r-- 1 a a 77 Jun 6 02:52 printStr.h
-rw-rw-r-- 1 a a 580 Jun 6 14:17 string_simple.c
-rw-rw-r-- 1 a a 98 Jun 6 12:14 strIsEmpty.h
I run the above in gdb, but even though I step through main line by line, still nothing can be seen. The only thing that I can see is that only after the very last line of the code:
Code:
free(string);
do I get the above error.
Why do I get this error? Aren't we supposed to free anything malloc-ed?
Why do I get this error? Aren't we supposed to free anything malloc-ed?
You allocated space for the string but then pointed it at argv[1]. This is memory that is allocated elsewhere. What you want to do is copy the string that argv[1] points to and this is done with strcpy or strncpy (see your manpages for strcpy).
Thinking of my past viewing of Jacob Sorber's tutorial on how strcpy can be hacked, I decided on strncpy.
After reading the below manpages for strncpy:
Code:
20 The strncpy() function is similar, except that at most n bytes of src
21 are copied. Warning: If there is no null byte among the first n bytes
22 of src, the string placed in dest will not be null-terminated.
23
24 If the length of src is less than n, strncpy() writes additional null
25 bytes to dest to ensure that a total of n bytes are written.
26
27 A simple implementation of strncpy() might be:
28
29 char *
30 strncpy(char *dest, const char *src, size_t n)
31 {
32 size_t i;
33
34 for (i = 0; i < n && src[i] != '\0'; i++)
35 dest[i] = src[i];
36 for ( ; i < n; i++)
37 dest[i] = '\0';
38
39 return dest;
40 }
I wasn't sure about what number to choose for "n", so I had to experiment first with
allowing for strncpy's padding function to not get cut off, thus my final solution for this post:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "printStr.h"
#include "strIsEmpty.h"
int main(int argc, char ** argv)
{
//Verify User input non-empty string
if(strIsEmpty(argv[1]))
{
printf("String NOT detected..\n Please rerun program and enter a string\n");
exit(EXIT_FAILURE);
}
int length = strlen(argv[1]);
//Allocate memory for array in heap
char* string = (char*)malloc((length + 1) * sizeof(char));
//Point 'string' variable at argv[1];
strncpy(string,argv[1],(length+1)); //strncpy will only insert padding on end if "n" is one more than the true length of the string
//Print out char array
printStr(string);
//Free
free(string);
}
Nota Bene: It seemed strange at first how you have to feed length + 1 in the strncpy function, but since I'm already used to "length + 1" whenever mallocing strings, all I have to remember is that strncpy is parallel with malloc to ensure padding.
Nota Bene: It seemed strange at first how you have to feed length + 1 in the strncpy function, but since I'm already used to "length + 1" whenever mallocing strings, all I have to remember is that strncpy is parallel with malloc to ensure padding.
strlen gives you the number of char in the string, up to but not including the nul byte (string terminator). When you copy a string you want the string to be terminated so you also want to copy that byte (hence +1). If you don't copy it you can assign it after copying but before you use the string.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.