![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2005
Posts: 45
Rep Power: 0
![]() |
Correct use of malloc?
The exact uses of malloc and realloc are confusing me a bit. I know they need to be used when you don't know how much space you're going to need, e.g. storing a user entered string, but I'm still not 100% sure I'm using them correctly.
Is the following program ok? The original example declared an array called buffer, of size 20, and printed an error if the output string was longer than 20. So I decided to use malloc so that any size string could be output. At the start I used malloc to allocate enough space for 1 char, and then on each successive loop I used realloc to allocate space for another char. The program basically converts a given decimal number, into the base given. The function hexDigit(int n) converts 10 to A, 11 to B etc if the given base is base 16. Thanks in advance. c Syntax (Toggle Plain Text)
EDIT: I also don't understand examples like this: http://www.captain.at/howto-c-code-malloc-struct.php Why is malloc needed to allocate memory to those structs? They don't have any character pointers so I don't see why malloc is needed. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Re: Correct use of malloc?
This code
buffer = (char *) realloc(buffer, sizeof(buffer) + sizeof(char)); Instead of using sizeof(buffer), you could use the counter you already have (i), you are about to assign into buffer[i], so buffer needs to be at least i+1 in size. so you could use buffer = (char *) realloc(buffer, (i + 1) * sizeof(char)); Note that reallocating your buffer each time round the loop is probably not the most efficient way of doing things. Another way would be to allocate a reasonable sized buffer initially (say 20 bytes), and then increase it if you need to (e.g. if i becomes 20). This may make your code a bit more complicated, and isn't really necessary for a program of this size - you won't notice any difference in speed. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
Re: Correct use of malloc?
what is anyone's opinion of
c Syntax (Toggle Plain Text)
c Syntax (Toggle Plain Text)
|
|
|
|
|
|
#4 | |
|
Programmer
Join Date: Feb 2005
Posts: 45
Rep Power: 0
![]() |
Re: Correct use of malloc?
Thank you The Dark.
For anyone else who may have run into similar problems in the past, I found the following method is good for checking that enough memory has been allocated. Every time you malloc or realloc, put printf("Memory is now: %d bytes\n", <allocated memory here>); That way you can double check the right amount of memory is being allocated. Quote:
|
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2005
Posts: 45
Rep Power: 0
![]() |
Re: Correct use of malloc?
I have now edited my program so that it uses a buffer of 5 chars, and when the buffer gets full, it malloc/reallocs memory to store the buffer so far and then starts filling the buffer from the start again.
If anyone has any time, could they verify that I am using malloc/realloc properly and also freeing all the memory (i.e. no memory leaks)? Thanks a lot. c Syntax (Toggle Plain Text)
|
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Re: Correct use of malloc?
One problem I can see is that the realloc call might change the location of the allocated memory - it does this if it can't grow the memory in-place.
This will mean that your "dest" variable now doesn't point to allocated memory any more. You will need to point dest back to the appropriate spot in the new "line" variable. The other problem is that the memory management part is very complicated, and overshadows the actual calculations. Unless you have extreme memory limitations, or if you are just practising with mallocs etc, I'd suggest simplifying it. If you want to simplify it, you could work out your biggest buffer requirement for any value, and just set your buffer size to that. If you don't mind assuming an 8 bit byte, you can use sizeof(int) to work out the number of bytes in an int and then multiply that by 8, that should give you the length of the biggest binary number you are likely to encounter. Then just allocate that on the stack. |
|
|
|
|
|
#7 | |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
Re: Correct use of malloc?
Quote:
c Syntax (Toggle Plain Text)
c Syntax (Toggle Plain Text)
>I'd use the first one just because it's easy to remember that >char is a char, and I don't have to check what *s points to. It's difficult to look slightly to the left? Your reason makes more sense when the statement isn't a declaration for s. However, I would claim that if you have no clue what the type of *s is and you need to know, your problems are more than stylistic. I honestly can't think of a place where I would need to know the type and can't easily figure it out without introducing a maintenance problem.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A Malloc question | metsfan | C | 3 | Apr 20th, 2006 11:37 AM |
| Question regarding Malloc() and Type casting | sparda | C | 6 | Sep 29th, 2005 4:12 AM |
| malloc and new | brkstf | C++ | 6 | Apr 21st, 2005 11:03 AM |
| how to use malloc() and free() | FarAway | C++ | 5 | Feb 16th, 2005 11:08 AM |
| C++ programmer needs some info on malloc() | uman | C++ | 2 | Jan 25th, 2005 7:10 PM |