-4

I need to find the mistakes in the following code and I'm not sure I found all of them.
1)

char *str;
str=(int*)malloc(10);

I'm not sure if its allowed to allocate Integer and convert it to char but the first mistake I see is that the memory was not freed.
same with :
2)

 char *str;
 str=(char*)malloc(10);     
 free();

The memory was not freed right - should be ( free(str);). and if it's in the same code with the previous one then we allocated another memory and didn't free the previous one.

Am I right ? I'm not sure if you can allocate integer and convert to char tho.
Thank you in advance.

Roach87
  • 129
  • 1
  • You are right about allocation and freeing. Regarding converting the type of the pointer returned by `malloc`, there is no problem because `malloc` does not care about the type of data stored in the allocated memory. You just have to be sure that you have allocated enough bytes for your purposes. – zaadeh Dec 29 '20 at 08:10
  • 2
    A compiler will probably give you the best answer. ;) – Roberto Caboni Dec 29 '20 at 08:11
  • `malloc(10)` will allocate ten bytes. The `malloc` function have no concept or knowledge of what those bytes are to be used for. – Some programmer dude Dec 29 '20 at 08:11
  • Also see [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/Do-I-cast-the-result-of-malloc) – Some programmer dude Dec 29 '20 at 08:12
  • @zaadeh: `malloc` "doesn't care", but the assignment does. Casting the result of malloc to a type that's different to the tipe of the destination variable will result at least in a warning. – Roberto Caboni Dec 29 '20 at 08:13
  • Please have a good read of the [malloc](https://man7.org/linux/man-pages/man3/malloc.3.html) and [free](https://man7.org/linux/man-pages/man1/free.1.html) man pages. Also, review some [good resources on C](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – costaparas Dec 29 '20 at 08:13
  • The style of the question makes me want to make sure that you are aware of this discussion https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Yunnosch Dec 29 '20 at 09:27
  • Please provide a [mre] including the shown code. It will make explaining contextual issues much easier. Also, creating an MRE will probably make you aware of some of the issues, especially if you use strict warning for compiling ( `-Wall` ). – Yunnosch Dec 29 '20 at 09:30

2 Answers2

0

First of all, you don't type-cast the result of malloc at all.

Second, type-casting a char pointer to int pointer is just plain undefined behavior in C. You have a char sized region of memory from which you will be reading an int; the result is plain undefined.

Lastly, you need to pass your pointer inside free as argument to release the allocated memory associated with it. See free's reference. free is not a keyword in C and needs to be imported from stdlib.h. free is not intelligent enough like a garbage collector (you can't just call it without an argument and have all un-used resources freed, which the syntax won't allow anyway) which will automatically find weak references (like in Java) and garbage collect it. There is no automatic garbage collection in C.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • This " First of all, you don't type-cast the result of malloc at all." ijs a false statement. To make code more readable and without bugs it is sometimes useful to cast the return type of malloc. For example if you see such a code like p = malloc( n ); can you say whether this code is correct and what type of data is allocated? – Vlad from Moscow Dec 29 '20 at 08:51
  • Ok so basically we just declare on memory allocation , if we convert it to Integer and then convert it to Char then we will have an error as you said. and after using the memory we need to free it but in both codes it was not freed properly/ not freed at all – Roach87 Dec 29 '20 at 09:06
0

I can find five potential problems with the code in your first example:

  1. The functions malloc (and free) are declared in stdlib.h so you need to #include <stdlib.h>.
  2. To keep track of the length of an allocated buffer it's a good idea to introduce a length variable together with the character pointer str. I always use the suffix Len, so then the name is strLen.
  3. Casting the result of malloc is not needed or recommended in C.
  4. The result of malloc can be NULL so we need to check if that's the case. A good approach is to use a memory allocation macro so we can do the check in one place.
  5. When we are done with str we need to free it with free(str);

Here is the code with all suggested changes:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NEW_ARRAY(pointer, length) \
    (pointer) = malloc((length) * sizeof (pointer)[0]); \
    if ((pointer) == NULL) { \
        fprintf(stderr, "Memory allocation failed: %s\n", strerror(errno)); \
        exit(EXIT_FAILURE); \
    }

int main(void)
{
    const char strLen = 10;
    char *str;

    NEW_ARRAY(str, strLen);
    /*...*/
    free(str);
    return 0;
}
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60