Question about memory/allocation/pointers

This is a discussion on Question about memory/allocation/pointers within the C++ Programming forums, part of the General Programming Boards category; I have a program that looks something like this: Code: unsigned char* Data; unsigned char* TempData; TempData=new unsigned char[256]; if(TempData!=NULL) ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Question about memory/allocation/pointers

    I have a program that looks something like this:

    Code:
    
    unsigned char* Data;
    unsigned char* TempData;
    
    TempData=new unsigned char[256];
    if(TempData!=NULL)
    {
      Data=&TempData;
      TempData=NULL;
    }
    
    if(Data!=NULL)
    {
      delete[] Data;
    }
    
    It uses a temporary pointer to allocate memory, and if successful makes Data the pointer to this memory.

    I get a suspicious pointer conversation warning when compiling. Is there something wrong with this code? It seems to work fine when running the program.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    When you say it works fine what do you mean, it doesn't crash?

    >>> Data=&TempData;

    This line looks wrong to me. Data and TempData are both char pointers, you set the value of one of these pointers to the address of the char array, and then set the other to the address of the first pointer, not the array. I am a little suprised it compiles.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Oops!

    Sorry, it should be:

    Data=&TempData[0];
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Just use Data=TempData; instead. While your solution is correct, too, the compiler obviously suspects something wrong and for a human reader it looks like a hack, too. The bold version is correct and the easiest way. Anything else just confuses readers.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Ok

    Ok, thanks
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21