# Thread: Dynamic Memory Allocation Problems

1. ## Dynamic Memory Allocation Problems

Good evening all! I am currently in the middle of a program for my class and have run into quite an odd issue... Essentially, our program is supposed to create a word search algorithm that goes through a puzzle and searches for words horizontally, vertically, and diagonally (backwards and forwards).
Everything was going fine - I had created two of my functions, one of which dynamically allocates a sufficient amount of space for the words in a given dictionary and populates an array of strings, and another which dynamically allocates space for the word search grid and populates it - until I got to the function for reversing the strings.
I know how to create a function that reverses the order of a string (and I am also aware of strrev; however, for our program we are not to use strrev). I will provide the code below this paragraph and explain what the issue is after the code in order to make the question succinct.

Code:
```int stringManipulation(const char *word) {
int i, j, len;
char* backwards;

len = strlen(word);

backwards = malloc(sizeof(char)*len + 1);

printf("%d\n", len);

for(i = 0, j = (len - 1); i < len && j >= 0; i++, j--) {
backwards[i] = word[j];
}

printf("size of backwards = %d, string length = %d, word in backwards: %s\n", sizeof(backwards), strlen(backwards), backwards);

return 1;
}```
Everything runs fine... Except for the dynamic memory allocation portion. The word I am passing into my function is the second word in my sample dictionary, "be", which has a length of 2. My printf statement verifies that. However, when I print strlen(len) it prints 2, when I print sizeof(backwards) it prints 4, when I print strlen(backwards) it prints 8, and when I print the word in backwards it prints eb (backwards of "be") plus some other bs (since I only have it reading in values to the first two elements in the array). I have tried re-writing the malloc statement as malloc(sizeof(char)*500), malloc(5000), malloc(len), etc. No matter what I do, the numbers I provided above all remain the same.
I feel like I am making some huge noob mistake, but I am at my wit's end with this problem, haha. Any tips or advice would honestly be greatly appreciated

2. I don't see why you need to use malloc.

Code:
```void strman( char* word )
{

char* lpword;

char temp;

unsigned int len,
rindex;

lpword = word;

len = rindex = strlen( word );

while ( rindex-- > len / 2 )
{
temp = *word;

*word++ = *(lpword + rindex);

*(lpword + rindex) = temp;
}

*(lpword + len) = '\0';

return;
}```

3. Originally Posted by Ktulu
I don't see why you need to use malloc.
Depends on where word comes from. I notice that the original version of stringManipulation had const char * as the type of the first argument. That is a type compatible with string literals. Yet your version is different, and actually takes away this feature. Calling strman is not safe if word is a string literal. The resulting behavior is undefined.

4. Hey, I'm sorry, I should have been more specific about the purpose of my function.
The reason that I am using malloc is that I want to keep the list of backward words once I exit back into main.

5. I'm reading the words in from a dynamically allocated 2D array of words

6. Yes you are right, you should use strman as shown below when using string literals.

Code:
```char* word = "something";

unsigned int len = strlen( word );

char* revword = malloc( sizeof(char) * len + 1 );

strcpy( revword, word );

strman( revword );```

7. Originally Posted by Andres Posadas
Good evening all! I am currently in the middle of a program for my class and have run into quite an odd issue...

Code:
```char * stringManipulation(const char *word) {
int i, j, len;
char* backwards;
len = strlen(word);
backwards = malloc(sizeof(char)*len + 1);

for(i = 0, j = len ; i <= len; ) {
backwards[i++] = word[j--];
}
backwards[i] = '\0'

return backwards;
}```
Try something more simple, unless I misunderstood the question!.... no compiler on hand to test the code.

8. Originally Posted by Andres Posadas
when I print strlen(backwards) it prints 8, and when I print the word in backwards it prints eb (backwards of "be") plus some other bs (since I only have it reading in values to the first two elements in the array).
sizeof(backwards) gives you the size of a char pointer, not the length of a string. Strings are terminated with a null character which is what strlen() will be looking for when it calculates the length so you need this at the end of your word in backwards.

9. Originally Posted by Andres Posadas
Code:
`backwards = malloc(sizeof(char)*len + 1);`
should be:

Code:
`backwards = malloc(sizeof(char)*(len + 1));`
The difference is that in your code, due to operator precedence, it adds one byte to the amount allocated, when it should add sizeof(char), because sizeof(char) might be greater than one byte.

10. The standard requires sizeof(char) to be one.

11. Originally Posted by Golf7
The standard requires sizeof(char) to be one.
Incorrect.

Originally Posted by C11 Standard 6.2.5
An object declared as type char is large enough to store any member of the basic
execution character set. If a member of the basic execution character set is stored in a
char object, its value is guaranteed to be nonnegative. If any other character is stored in
a char object, the resulting value is implementation-defined but shall be within the range
of values that can be represented in that type.
I see absolutely no reference to exactly one byte.

12. Originally Posted by Elkvis
I see absolutely no reference to exactly one byte.
That is because you looked in the wrong place:
Originally Posted by C11 Clause 6.5.3.4 Paragraph 4a
When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

13. I see absolutely no reference to exactly one byte.
Perhaps you're looking at the wrong section of the standard.

6.5.3.4 The sizeof and alignof operators
Constraints

1. The sizeof operator shall not be applied to an expression that has function type or an
incomplete type, to the parenthesized name of such a type, or to an expression that
designates a bit-field member. The alignof operator shall not be applied to a function
type or an incomplete type.

Semantics

2. The sizeof operator yields the size (in bytes) of its operand, which may be an
expression or the parenthesized name of a type. The size is determined from the type of
the operand. The result is an integer. If the type of the operand is a variable length array
type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
integer constant.

3. The alignof operator yields the alignment requirement of its operand type. The result
is an integer constant. When applied to an array type, the result is the alignment
requirement of the element type.

4. When sizeof is applied to an operand that has type char, unsigned char, or
signed char, (or a qualified version thereof) the result is 1.
When applied to an
operand that has array type, the result is the total number of bytes in the array.102) When
applied to an operand that has structure or union type, the result is the total number of
bytes in such an object, including internal and trailing padding.
Jim

14. First, I want to thank everyone for taking the time to respond to my question

With that being said, I know that sizeof doesn't print the string length, but strlen does. The issue is that, no matter what I set the malloc function to, when I print out the numbers they are all the same - regardless of the malloc statement. D:

15. Originally Posted by Elkvis
should be:

Code:
`backwards = malloc(sizeof(char)*(len + 1));`
The difference is that in your code, due to operator precedence, it adds one byte to the amount allocated, when it should add sizeof(char), because sizeof(char) might be greater than one byte.
I have tried this as well, and the results are all the same. I have tried compiling and running the code on DEVC++, Codeblocks, and on a Linux system, and I get the same result over and over :/

*EDIT: sizeof(backwards) always results in 8, strlen(backwards) always results in 3, but strlen(word) is always 2 (the correct size of the word).

Popular pages Recent additions