Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Platform/API C++

Reply
 
LinkBack Thread Tools Display Modes
Old 04-15-2005, 06:35 PM   #1 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
DJMaze is on a distinguished road
empty pointers are not empty using 'new' operator

I have a structure and when i use the new operator to create it somehow the pointers inside it are not empty.
Instead i must force each to be NULL.

PHP Code:
typedef struct {
  
TCHAR         *FileName;
  
int            Index;
  
int            Frequency;
my_struct;

my_struct *test = new my_struct;

if (
test->FileName != NULL) {
  print(
"Odd pointer found");
  return -
1;

Could someone explain this to me ?
I even tried to use malloc() instead.
DJMaze is offline   Reply With Quote
Old 04-15-2005, 06:41 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
Nothing in the ANSI C/C++ state that memory allocated by new or malloc will be filled with NULL, all it states is that the memory will be reserved for your use..
What you want to check for when creating it, is if malloc returns NULL, then an error has happened, same thing goes for new.. but nothing ever said that it would initialy set the memory allocated to NULL.

edit... woohoo, my number 700 post... yay me...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-15-2005, 07:43 PM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
DJMaze is on a distinguished road
Hmm i thought it was, shame on my bad thoughts.

Now i must go weed in all my code where i use the delete operator and be shure they are NULL or do have something

Thanks red
DJMaze is offline   Reply With Quote
Old 04-16-2005, 12:11 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
You could make it C++ like:
Code:
 class my_struct{
  public:
    TCHAR    *FileName;
    int            Index;
    int            Frequency;
    mystruct(){ 
        Filename = NULL; 
        Index = 0;
        Frequency = 0;
     };
};

my_struct *test = new my_struct;

if (test->FileName != NULL) {
  print("Odd pointer found");
  return -1;
}
Or make it the way real C programmers do
Code:
 typedef struct {
  TCHAR         *FileName;
  int            Index;
  int            Frequency;
} my_struct;

my_struct *test = (my_struct*)malloc (sizeof(my_struct));
if(!test)
{
    perror("Not enough space on the heap\n");
    return -1;
}
memset(test, 0, sizeof(my_struct));
if (test->FileName != NULL) {
  print("Odd pointer found");
  return -1;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataReader empty watherton MS Technologies ( ASP, VB, C#, .NET ) 3 06-15-2004 11:58 AM
A day late and pointers short saline Standard C, C++ 2 10-04-2003 10:33 AM
pointers .. sde Standard C, C++ 2 06-24-2002 12:54 PM


All times are GMT -8. The time now is 12:08 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting