Why is C++ allocating such a large space in memory for my dynamic array?

Craig Lafferty

readFruit.name is initialized to NULL before I try to initialize it as a char array. I included size to see if that was the culprit but it is exactly what it should be depending on my input. No matter what length tempString is, readFruit.name is allocated around 25 "characters" in memory and they're all garbage. Why isn't it being allocated a space the size of tempString.length() and how can I fix it?

relevant CPP

std::istream & operator>>(std::istream &is, Fruit &readFruit)
{

string tempString;
is >> tempString;
int size = tempString.length();
readFruit.name = new char[tempString.length()];
for(int i = 0; i < (int)tempString.length(); i++)
{
    readFruit.name[i] = tempString[i];
}
for(int i =0; i < CODE_LEN; i++)
{
    is >> readFruit.code[i];
}
return is;
}

Relevant H file (constructor)

#ifndef _FRUIT_H
#define _FRUIT_H
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
enum { CODE_LEN = 4 }; 
enum { MAX_NAME_LEN = 30 };
class Fruit
{
private:
    char *name;
    char code[CODE_LEN];
public:
    Fruit(const Fruit &temp);
    Fruit(){name = NULL;};
    bool operator<(const Fruit& tempFruit);
    friend std::ostream & operator<<(std::ostream &os, const Fruit& printFruit);
    bool operator==(const Fruit& other){return *name == *other.name;};
    bool operator!=(const Fruit& other){return *name != *other.name;};
    friend std::istream & operator>>(std::istream& is, Fruit& readFruit);
};
#endif
P0W

If you're trying to print readFruit.name, it will display garbage value until it finds a null termination, that's what I assume you saying 25 characters "all garbage"

Allocated memory like this :

readFruit.name = new char[tempString.length()+1];

And after for loop do:

readFruit.name[i] ='\0'; // C strings are null terminated

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ allocating large array on heap gives "out of memory exception"

From Dev

In C, memory allocating fails, why?

From Dev

Allocating memory to array of strings in c

From Dev

C allocating dynamic memory in a function - same results

From Dev

Allocating memory for one dimensional array in C

From Dev

Allocating memory to hold an array of structs in C

From Dev

C: Array not allocating more memory correctly

From Dev

C not allocating memory for array of structs correctly?

From Dev

Memory allocating array of structure pointers in c

From Dev

Allocating space for an array

From Dev

Allocating large arrays in memory with Python

From Dev

Allocating disk space as memory temporarily

From Dev

allocating memory to an array of string

From Dev

C - error when allocating dynamic memory for linked list node

From Dev

Allocating Memory for string in c?

From Dev

Allocating memory (C)

From Dev

Dynamic memory in a class (Allocating memory and accessing to the elements)

From Dev

C++ - safety of allocating memory for an array, then returning a pointer to be deleted externally

From Dev

Allocating memory for a array of linked lists

From Dev

Allocating memory for global multidimensional array

From Dev

Allocating memory for array of struct in fgets

From Dev

Passing a pointer to an array and allocating memory

From Dev

Why does allocating large chunks of memory fail when reallocing small chunks doesn't

From Dev

Segment fault when writing to a large 4G dynamic allocating array

From Dev

Segmentation fault with large, contiguous 2D array (with dynamic memory allocation) in C

From Dev

dynamically allocating my 2d array in c

From Dev

Dynamic Array in C for memory mapping

From Dev

Why does my C dynamic array give me access violation?

From Dev

Problems with allocating memory for a matrix in c

Related Related

  1. 1

    C++ allocating large array on heap gives "out of memory exception"

  2. 2

    In C, memory allocating fails, why?

  3. 3

    Allocating memory to array of strings in c

  4. 4

    C allocating dynamic memory in a function - same results

  5. 5

    Allocating memory for one dimensional array in C

  6. 6

    Allocating memory to hold an array of structs in C

  7. 7

    C: Array not allocating more memory correctly

  8. 8

    C not allocating memory for array of structs correctly?

  9. 9

    Memory allocating array of structure pointers in c

  10. 10

    Allocating space for an array

  11. 11

    Allocating large arrays in memory with Python

  12. 12

    Allocating disk space as memory temporarily

  13. 13

    allocating memory to an array of string

  14. 14

    C - error when allocating dynamic memory for linked list node

  15. 15

    Allocating Memory for string in c?

  16. 16

    Allocating memory (C)

  17. 17

    Dynamic memory in a class (Allocating memory and accessing to the elements)

  18. 18

    C++ - safety of allocating memory for an array, then returning a pointer to be deleted externally

  19. 19

    Allocating memory for a array of linked lists

  20. 20

    Allocating memory for global multidimensional array

  21. 21

    Allocating memory for array of struct in fgets

  22. 22

    Passing a pointer to an array and allocating memory

  23. 23

    Why does allocating large chunks of memory fail when reallocing small chunks doesn't

  24. 24

    Segment fault when writing to a large 4G dynamic allocating array

  25. 25

    Segmentation fault with large, contiguous 2D array (with dynamic memory allocation) in C

  26. 26

    dynamically allocating my 2d array in c

  27. 27

    Dynamic Array in C for memory mapping

  28. 28

    Why does my C dynamic array give me access violation?

  29. 29

    Problems with allocating memory for a matrix in c

HotTag

Archive